MAT1和MAT2形状不能乘以(19x1和19x1)
我有一个手工制作的数据集,所有想要做的就是用pytorch设置线性回归模型。 这些是我写的代码:
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
我想这是世界上最简单的pytorch神经网络代码,但是它仍然给出此错误消息:
mat1 and mat2 shapes cannot be multiplied (19x1 and 19x1)
我只是在书中做了所有事情,但仍会给出此错误。你能帮助我吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用的是 b) 作为网络的一部分,输入必须为形状
(n,a)
,并且输出将为形状(n,b,b )
。因此,您需要确保x
在您的情况下具有Shape(N,19)
,因此对其进行修改会解决问题。
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 thatX
has shape(n, 19)
in your case, so modifying it withwould do the trick.