# Stupid Question!

Hi there,
Sorry for the stupid and elementary question!
I am totally new to Python and PyTorch.

My question is that I want to multiply a [128,10,512] tensor by a tensor with the length of 10 such that the final output to be a [128*512] tensor. I know how to do that using for loops. However, I am wondering whether there is a direct PyTorch command in this regard.
Thank you in advance for your kind help How are you reducing along that dimension? sum?

Yes, It is Sum

First thing that comes to mind:

``````# length 10 vector
ten = torch.ones((10))
# 128x10x512 tensor
big = torch.ones((128,10,512))

result = (big*ten.view(1,-1,1)).sum(1).flatten()
``````

We reshape the tensor to be 1x10x1 to align with the other one. Then we sum across that dimension and flatten.

Makes sense?

1 Like

Thank you!
That’s great.
What about if “big” is a tensor shaped [batchsize, num_windows, CNN_features], “ten” is a tensor shaped [batchsize, num_windows], and using “ten” we want to map “big” to the “result” tensor with the shape of [batchsize, CNN_features]? i.e.
big = [128,10,512]
ten = [128,10]
result = [128,512]

sum again? I think it’s the same idea.

``````ten = torch.ones((128,10))
big = torch.ones((128,10,512))

result = (big*ten.view(128,10,1)).sum(1)
``````
1 Like

Thank you again!
I learned a lot!

1 Like

No question is stupid. I know its overrepeated but No Question Is Stupid!

1 Like

btw, use the </> button to add code or three backticks ``` to start and finish the thing. It just makes everything easier and clean formatting. Easier for other people to understand everything.

1 Like