简体   繁体   English

PyTorch线性回归问题

[英]PyTorch Linear Regression Issue

I am trying to implement a simple linear model in PyTorch that can be given x data and y data, and then trained to recognize the equation y = mx + b. 我正在尝试在PyTorch中实现一个简单的线性模型,该模型可以提供x数据和y数据,然后经过训练可以识别方程y = mx + b。 However, whenever I try to test my model after training, it thinks that the equation is y= mx + 2b. 但是,每当我尝试在训练后测试模型时,它都会认为方程为y = mx + 2b。 I'll show my code, and hopefully someone will be able to spot an issue. 我将展示我的代码,希望有人能够发现问题。 Thank you in advance for any help. 预先感谢您的任何帮助。

import torch

D_in = 500
D_out = 500
batch=200
model=torch.nn.Sequential(
     torch.nn.Linear(D_in,D_out),
)

Next I create some data and set a rule. 接下来,我创建一些数据并设置规则。 Let's do 3x+4. 让我们做3x + 4。

x_data=torch.rand(batch,D_in)
y_data=torch.randn(batch,D_out)

for i in range(batch):
    for j in range(D_in):
         y_data[i][j]=3*x_data[i][j]+5 # model thinks y=mx+c -> y=mx+2c?

loss_fn=torch.nn.MSELoss(size_average=False)
optimizer=torch.optim.Adam(model.parameters(),lr=0.001)

Now to training... 现在要训练...

for epoch in range(500):
    y_pred=model(x_data)
    loss=loss_fn(y_pred,y_data)
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()

Then I test my model with a Tensor/matrix of just 1's. 然后,我使用仅为1的张量/矩阵测试模型。

test_data=torch.ones(batch,D_in) 
y_pred=model(test_data)

Now, I'd expect to get 3*1 + 4 = 7, but instead, my model thinks it is 11. 现在,我希望得到3 * 1 + 4 = 7,但是我的模型认为是11。

[[ 10.7286,  11.0499,  10.9448,  ...,  11.0812,  10.9387,
      10.7516],
    [ 10.7286,  11.0499,  10.9448,  ...,  11.0812,  10.9387,
      10.7516],
    [ 10.7286,  11.0499,  10.9448,  ...,  11.0812,  10.9387,
      10.7516],
    ...,
    [ 10.7286,  11.0499,  10.9448,  ...,  11.0812,  10.9387,
      10.7516],
    [ 10.7286,  11.0499,  10.9448,  ...,  11.0812,  10.9387,
      10.7516],
    [ 10.7286,  11.0499,  10.9448,  ...,  11.0812,  10.9387,
      10.7516]])

Similarly, if I change the rule to y=3x+8, my model guesses 19. So, I am not sure what is going on. 同样,如果我将规则更改为y = 3x + 8,我的模型将猜测为19。因此,我不确定发生了什么。 Why is the constant being added twice? 为什么常数要加两次? By the way, if I just set the rule to y=3x, my model correctly infers 3, and for y=mx in general my model correctly infers m. 顺便说一句,如果我只是将规则设置为y = 3x,则我的模型可以正确推断3,而对于y = mx,通常我的模型可以正确推断m。 For some reason, the constant term is throwing it off. 由于某种原因,常数项将其抛弃。 Any help to solve this problem is much appreciated. 非常感谢您为解决该问题提供的帮助。 Thanks! 谢谢!

Your network does not learn long enough. 您的网络学习时间不够长。 It gets a vector with 500 features to describe a single datum. 它获得具有500个特征的矢量来描述单个基准。

Your network has to map the big input of 500 features to an output including 500 values. 您的网络必须将500个要素的大输入映射到包含500个值的输出。 Your trainingdata is randomly created, not like your simple example, so I think you just have to train longer to fit your weights to approximate this function from R^500 to R^500. 您的训练数据是随机创建的,不像您的简单示例那样,因此我认为您只需要训练更长的时间以适合您的权重即可将此函数从R ^ 500近似为R ^ 500。

If I reduce the input and output dimensionality and increase the batch size, learning rate and training steps I get the expected result: 如果减少输入和输出尺寸并增加批次大小,学习率和培训步骤,我将得到预期的结果:

import torch

D_in = 100
D_out = 100
batch = 512

model=torch.nn.Sequential(
     torch.nn.Linear(D_in,D_out),
)

x_data=torch.rand(batch,D_in)
y_data=torch.randn(batch,D_out)
for i in range(batch):
    for j in range(D_in):
         y_data[i][j]=3*x_data[i][j]+4 # model thinks y=mx+c -> y=mx+2c?

loss_fn=torch.nn.MSELoss(size_average=False)
optimizer=torch.optim.Adam(model.parameters(),lr=0.01)

for epoch in range(10000):
    y_pred=model(x_data)
    loss=loss_fn(y_pred,y_data)
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()

test_data=torch.ones(batch,D_in)
y_pred=model(test_data)
print(y_pred)

If you just want to approximate f(x) = 3x + 4 with only one input you could also set D_in and D_out to 1. 如果只想用一个输入来近似f(x) = 3x + 4 ,则也可以将D_inD_out设置为1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM