Broadcast for NDArray

Hi there, I’m new to mxnet and wondering how to multiply a shape=[22,11] tensor A and another tensor with shape=[22,11,100]. The third axe 100 is batch size, I’d like to elementwise multiply these [22,11] tensors, and broadcast the multiplication to batch dimension.

A * B gives error:
elemwise_binary_broadcast_op.h:68: Check failed: l == 1 || r == 1 operands could not be broadcast together with shapes [22,11] [22,11,100]

Any hints? Thank you.

import mxnet as mx
a=mx.nd.random_normal(shape=(22,11))
print(a.shape)
b=mx.nd.random_normal(shape=(22,11,100))
print(b.shape)
new_a=a.reshape((22,11,1))
print(new_a.shape)
result=mx.nd.broadcast_mul(new_a,b)

https://gluon.mxnet.io/chapter01_crashcourse/ndarray.html?highlight=broadcast#Broadcasting

It seems that you need to reshape A to (22, 11, 1). Besides using reshape to explicitly specify the exact shape as mentioned by @kli-nlpr, you may also use expand_dims to create a new axis of size 1:

new_a = a.expand_dims(axis=2)
# new_a.shape : (22, 11, 1)
result = new_a * b # or broadcast_mul(new_a, b)
# (22, 11, 1) * (22, 11, 100)

Note that * would broadcast only for ndarrays, not for symbols.

Thank you for your solutions!

I was a DyNet user, DyNet has a magic parameter batched=True, which can easily turn a calculation into batched version. I really miss that.