Adding a scalar?

Dumb question, but how do I make a scalar Variable? I’d like to add a trainable parameter to a vector, but I keep getting size-mismatch problems.

# Works, but I can't make the 2 a 
# parameter, so I can't do gradient descent on it
Variable(torch.from_numpy(np.ones(5))) + 2 

Thanks!

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

Great, thanks for that.