MAT1和MAT2形状不能乘以(19x1和19x1)

发布于 2025-01-25 23:19:20 字数 712 浏览 1 评论 0原文

我有一个手工制作的数据集,所有想要做的就是用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

瀟灑尐姊 2025-02-01 23:19:20

如果您使用的是 b) 作为网络的一部分,输入必须为形状(n,a),并且输出将为形状(n,b,b )。因此,您需要确保x在您的情况下具有Shape (N,19),因此对其进行修改

...).view(1, 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 that X has shape (n, 19) in your case, so modifying it with

...).view(1, 19)

would do the trick.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文