[英]Linear Regression in PyTorch
It's a simple regression problem.这是一个简单的回归问题。 But no matter how much I try, I can't get the answer I want.
但无论我怎么努力,都得不到我想要的答案。 I'm guessing the weight should be 32 (4 * 8) but, the code returns 25. Why is that?
我猜重量应该是 32 (4 * 8) 但是,代码返回 25。这是为什么?
This is my full source code:这是我的完整源代码:
import torch
import torch.nn as nn
import torch.optim as op
X = torch.FloatTensor([[1., 2.],[2., 4.],[3., 6.]])
Y = torch.FloatTensor([[2.],[8.],[18.]])
class TEST(nn.Module):
def __init__(self):
super(TEST,self).__init__()
self.l1 = nn.Linear(2,1)
def forward(self, input):
x = self.l1(input)
return x
epochs = 2000
lr = 0.001
model = TEST()
loss_func = nn.MSELoss()
optimizer = op.SGD(model.parameters(), lr=lr)
for epoch in range(epochs):
optimizer.zero_grad()
output = model(X)
loss = loss_func(output, Y)
loss.backward()
optimizer.step()
if epoch%10 == 0:
print('loss[{}] : {}'.format(epoch, loss))
XX = torch.FloatTensor([[4., 8.]])
print(model(XX))
This is the output of the code:这是代码的输出:
loss[1920] : 0.8891088366508484
loss[1930] : 0.8890921473503113
loss[1940] : 0.8890781402587891
loss[1950] : 0.8890655636787415
loss[1960] : 0.8890505433082581
loss[1970] : 0.8890388011932373
loss[1980] : 0.889029324054718
loss[1990] : 0.8890181183815002
tensor([[25.3124]], grad_fn=<AddmmBackward>)
You are trying to approximate y = x1*x2
but are using a single linear layer ie a purely linear model.您正在尝试近似
y = x1*x2
但使用的是单个线性层,即纯线性模型。 Ultimately, what happens is you are learning weights a
and b
such that y = a*x1 + b*x2
.最终,发生的情况是您正在学习权重
a
和b
,使得y = a*x1 + b*x2
。 However, this model cannot approximate the distribution of x1, x2 -> x1*x2
.但是,此模型无法近似
x1, x2 -> x1*x2
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.