Batch multiplication for scalar and second-order tensor

Hi,

How can I multiply a scaler with a 2D matrix? Please see the example below:

batch_size = 128
a = torch.randn(batch_size, 3, 3)
b = torch.randn(batch_size, 1)

c = torch.bmm(a,b)

Don’t need a mm operation for that, use broadcasting

batch_size = 128
a = torch.randn(batch_size, 3, 3)
b = torch.randn(batch_size, 1)

c = a * b.view((-1, 1, 1))
1 Like