fluid.layers.elementwise_mul 不支持多个维度的broadcasting
Created by: yuzhichang
embeddings = fluid.layers.elementwise_mul(
input_reshaped, factors) # (batch_size, N, K)
print('input_reshaped shape:', input_reshaped.shape)
print('factors shape:', param_shape)
print('embeddings shape:', embeddings.shape)
input_reshaped shape: (-1L, 13L, 1L)
factors shape: [1, 13, 10]
embeddings shape: (-1L, 13L, 1L)
我期望embeddings.shape是(-1, 13, 10)。
Numpy, Mxnet 和Tensorflow的element-wise 乘法都支持在多个维度上的broadcasting。例如下面的乘法操作,在维度0和2上都做了broadcasting。
In [29]: from mxnet import nd
In [30]: mx.random.seed(1)
In [31]: a = nd.array(range(12)).reshape(4,3,1)
In [32]: b = nd.array(range(12)).reshape(1,3,4)
In [35]: a.shape
Out[35]: (4L, 3L, 1L)
In [36]: b.shape
Out[36]: (1L, 3L, 4L)
In [38]: (a*b).shape
Out[38]: (4L, 3L, 4L)
A set of arrays is called “broadcastable” to the same shape if the above rules produce a valid result, i.e., one of the following is true:
1. The arrays all have exactly the same shape.
2. The arrays all have the same number of dimensions and the length of each dimensions is either a common length or 1.
3. The arrays that have too few dimensions can have their shapes prepended with a dimension of length 1 to satisfy property 2.