Matrix Vector Basic operations

Hi all,
I just wondered if there’s an easy way to apply, for example an addition to each row of a 2DTensor using the values of an 1D Tensor.

In Tensorflow, it is as simple as :
result= t1+t2 #t1(10,10) and t2(1,10)

I know this might sound trivial/easy and there might be already something, but I’ve looked into the docs and I couldn’t find anything.
I could create a 2D matrix by repeating the 1D Tensor, or iterate over the 2D matrix, but i thought there might be something more optimized.

Thanks a lot !

Hi,

You can do: result = t1 + t2.expand_as(t1)
See http://pytorch.org/docs/tensors.html#torch.Tensor.expand_as for more details.

2 Likes

Great, works like a charm !
I should have looked harder into the docs >.>