Tensor subtraction

Hello all,

I want to subtract tensor1 [1,384] from tensor2 [ 1, 384, 56, 56]. The images of tensor2 should be subtracted by the scalar in tensor1.

result= tensor2 - tensor1
result= at::sub(tensor2-tensor1)

Both failed. I do not want to blow tensor1 to [1,384,56,56].
Many thanks for your help.

tensor1.view(-1) doesn’t work

Tensor tensor1flat= tensor1.view(-1)
Tensor tensor2flat.view{384,56,56})

Doesn’t work.

Try this:

import torch

#starting conditions
a = torch.rand((1,384))
b = torch.rand((1, 384, 56, 56))

#modify
a = a.unsqueeze(-1).unsqueeze(-1)

#operation
c = b - a

Hello J_Johnson,

thank you, works fine.

a= a.unsqueeze(-1).unsqueeze(-1)

changes (1,384) to (1,384,1,1)

1 Like