Reputation: 13
I begin to practice Pytorch ,try to use torch.mm() method。
Below is my code
import torch
import numpy as np
from torch.autograd import Variable
num_x = np.array([[1.0, 2.0]
,[3.0,4.0]])
tensor_x = torch.from_numpy(num_x)
x = Variable(tensor_x,requires_grad = True)
s = Variable(torch.DoubleTensor([0.01,0.02]),requires_grad = True)
print(s)
s = s.mm(x)
print(s)
Unfortunately,there is a runtime error
*RuntimeError Traceback (most recent call last)
<ipython-input-58-e8a58ffb2545> in <module>()
9 s = Variable(torch.DoubleTensor([0.01,0.02]),requires_grad = True)
10 print(s)
---> 11 s = s.mm(x)
12 print(s)
RuntimeError: matrices expected, got 1D, 2D tensors at /pytorch/aten/src/TH/generic/THTensorMath.cpp:131*
How can I fix this problem。 your reply is appreciated
Upvotes: 1
Views: 9682
Reputation: 319
or so:
>>> s @ x
tensor([0.0700, 0.1000], dtype=torch.float64, grad_fn=<SqueezeBackward3>)
Upvotes: 0
Reputation: 7713
try reshape
you need to change shape of s
to (1,2)
to make possible matrix multiplication operation with (2,2)
tensor
>>> s.reshape(1,2).mm(x)
tensor([[0.0700, 0.1000]], dtype=torch.float64, grad_fn=<MmBackward>)
Or give right shape when initializing s
>>> s = Variable(torch.DoubleTensor([[0.01,0.02]]),requires_grad = True)
>>> s.mm(x)
tensor([[0.0700, 0.1000]], dtype=torch.float64, grad_fn=<MmBackward>)
Upvotes: 1