Issue
I have a handmade dataset and all want to do is set a linear regression model with Pytorch. These are the codes I wrote:
from torch.autograd import Variable
train_x = np.asarray([1,2,3,4,5,6,7,8,9,10,5,4,6,8,5,2,1,1,6])
train_y = train_x * 2
X = Variable(torch.from_numpy(train_x).type(torch.FloatTensor), requires_grad = False).view(19, 1)
y = Variable(torch.from_numpy(train_y).type(torch.FloatTensor), requires_grad = False)
from torch import nn
lr = nn.Linear(19, 1)
loss = nn.MSELoss()
optimizer = torch.optim.SGD(lr.parameters(), lr = 0.01)
output = lr(X) #error occurs here
I guess this is the simplest Pytorch neural network code in the world but it's still giving this error message:
mat1 and mat2 shapes cannot be multiplied (19x1 and 19x1)
I just did all the things on the book but it's still giving this error. Can you help me?
Solution
If you are using a torch.nn.Linear(a,b)
as part of a network, then the input must be of shape (n, a)
, and the output will be of shape (n, b)
. Therefore you need to make sure that X
has shape (n, 19)
in your case, so modifying it with
...).view(1, 19)
would do the trick.
Answered By - flawr
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.