Adding a scalar?

Instead of having a number, you should instead have a one-element vector encapsulated in a Variable.
Note that we don’t have yet broadcasting implemented in pytorch, but it will be implemented soon, so for the moment you need to expand the tensor by hand.

x = Variable(torch.from_numpy(np.ones(5)))
y = Variable(torch.Tensor([2]).double()) # numpy is double by default
z = x + y.expand(x.size())

If you need to increase the number of dimensions of the tensor, you can use the unsqueeze function

4 Likes