我对波士顿数据集做了线性回归问题,并获得了下一个结果:
损耗值不会随着数量增加而变化。这个错误的原因是什么?请帮我
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)
答案 0 :(得分:0)
欢迎来到Stackoverflow
您的主循环很好(不过,您可以使自己的生活更轻松,但是,您应该阅读this),但是您的学习率(1e-5)很可能太低了。
我尝试了一个小的虚拟问题,很快以1e-2的学习速度解决了这个问题,而使用1e-5的学习时间则要长得多。虽然它确实会收敛,但是要经过100个以上的纪元。您提到您尝试增加纪元的数量,但没有写出实际进行多少次实验。
请尝试增加此参数(学习率)以查看它是否可以解决您的问题。您也可以尝试通过<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="header">
<div class="header__label">Grandtotal:<span id="total_grand">0</span> </div>
<div class="header__label">XP:<span id="total_xp">0</span></div>
</div>
<div data-id='order'>
<div class="order__header">
<div class="header__label">Order Total: <span class="order__total">000</span></div>
<div class="header__label">Order Fee: <span class="order__fee">000</span></div>
<div class="header__label">Order XP: <span class="xp__total--order">000</span></div>
</div>
<div data-id='history'>
<div class="history__name">Item 1 (10xp)</div>
<div><span class="history__sold">25</span>Sold</div>
<div><span class="history__cost">10</span>Cost</div>
<div><span class="history__subtotal">000</span>Subtotal</div>
<div>Fee:<span class="history__fee">0</span></div>
<div><span class="xp__total--history">000</span>XP</div>
</div>
<div data-id='history'>
<div class="history__name">Item 2 (10xp)</div>
<div><span class="history__sold">10</span>Sold</div>
<div><span class="history__cost">50</span>Cost</div>
<div><span class="history__subtotal">000</span>Subtotal</div>
<div>Fee:<span class="history__fee">0</span></div>
<div><span class="xp__total--history">000</span>XP</div>
</div>
</div>
<div data-id='order'>
<div class="order__header">
<div class="header__label">Order Total: <span class="order__total">000</span></div>
<div class="header__label">Order Fee: <span class="order__fee">000</span></div>
<div class="header__label">Order XP: <span class="xp__total--order">000</span></div>
</div>
<div data-id='history'>
<div class="history__name">Item 1 (10xp)</div>
<div><span class="history__sold">25</span>Sold</div>
<div><span class="history__cost">10</span>Cost</div>
<div><span class="history__subtotal">000</span>Subtotal</div>
<div>Fee:<span class="history__fee">0</span></div>
<div><span class="xp__total--history">000</span>XP</div>
</div>
<div data-id='history'>
<div class="history__name">Item 2 (10xp)</div>
<div><span class="history__sold">10</span>Sold</div>
<div><span class="history__cost">50</span>Cost</div>
<div><span class="history__subtotal">000</span>Subtotal</div>
<div>Fee:<span class="history__fee">0</span></div>
<div><span class="xp__total--history">000</span>XP</div>
</div>
</div>
删除除法,这将具有相同的效果(除法也适用于渐变)。
下次,请提供一个small minimal example号,该号将无法运行,并有助于重现您的错误。这里的大部分代码都是数据加载,可以用两行虚拟数据生成代替。