简体   繁体   English

线性回归中的损失值

[英]Loss value in Linear regression

I have done a linear regression problem with boston dataset and I have obtained the next results:我用波士顿数据集做了一个线性回归问题,我得到了下一个结果:

Loss value does not change with increasing number of value.损失值不会随着价值数量的增加而变化。 What is the reason of this mistake?这个错误的原因是什么? Please, help me请帮我

import pandas as pd
import torch
import numpy as np
import torch.nn as nn
from sklearn import preprocessing
training_set=pd.read_csv('boston_data.csv')
training_set=training_set.to_numpy()
test_set=test_set.to_numpy()
inputs=training_set[:,0:13]
inputs=preprocessing.normalize(inputs)
target=training_set[:,13:14]
target=preprocessing.normalize(target)
inputs=torch.from_numpy(inputs)
target=torch.from_numpy(target)
test_set=torch.from_numpy(test_set)
w=torch.randn(13,1,requires_grad=True)
b=torch.randn(404,1,requires_grad=True)
def model(x):
    return x@w+b
pred=model(inputs.float())
def loss_MSE(x,y):
    ras=x-y
    return torch.sum(ras * ras) / ras.numel()
for i in range(100):
    pred=model(inputs.float())
    loss=loss_MSE(target,pred)
    loss.backward()
    with torch.no_grad():
        w -= w.grad * 1e-5
        b -= b.grad * 1e-5
        w.grad.zero_()
        b.grad.zero_()
    print(loss) 

welcome to Stackoverflow欢迎来到 Stackoverflow

Your main loop is fine (you could have made you life much easier however, you should probably read this ), but your learning rate (1e-5) is most likely way too low .你的主循环很好(你本可以让你的生活更轻松,但是你应该读一下这个),但是你的学习率(1e-5)很可能太低了

I tried with a small dummy problem, it was solved very quickly with a learning rate ~1e-2, and would take tremendously longer with 1e-5.我尝试了一个小的虚拟问题,它以大约 1e-2 的学习率很快就解决了,而使用 1e-5 则需要更长的时间。 It does converge anyway though, but after much more than 100 epochs.无论如何,它确实会收敛,但是在 100 多个 epoch 之后。 You mentionned you tried increasing the number of epoch, did not write how with many you actually ran the experiment.您提到您尝试增加 epoch 的数量,但没有写出您实际运行实验的次数。 Please try increasing this parameter (learning rate) to see whether it solves your issue.请尝试增加此参数(学习率),看看它是否解决了您的问题。 You can also try removing the division by numel() , which will have the same effect (the division is also applied to the gradients).您也可以尝试通过numel()删除除法,这将具有相同的效果(除法也适用于渐变)。

Next time, please provide a small minimal example than can be run and help reproduce your error.下一次,请提供一个可以运行的最小示例并帮助重现您的错误。 Here most of your code is data loading which can be replaced with 2 lines of dummy data generation.在这里,您的大部分代码是数据加载,可以用 2 行虚拟数据生成替换。

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

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