【发布时间】:2025-12-18 03:25:02
【问题描述】:
我正在尝试使用 Pytorch 框架构建线性回归,并在实现梯度下降时,根据我在 Python 代码中使用算术运算的方式观察到两种不同的输出。下面是代码:
#X and Y are input and target labels respectively
X = torch.randn(100,1)*10
Y = X + 3*torch.randn(100,1) +2
plt.scatter(X.numpy(),Y.numpy())
#Initialiation of weight and bias
w = torch.tensor(1.0,requires_grad=True)
b = torch.tensor(1.0,requires_grad=True)
#forward pass
def forward_feed(x):
y = w*x +b
return y
#Parameters Learning
epochs = 100
lr = 0.00008
loss_list = []
for epoch in range(epochs):
print('epoch',epoch)
Y_pred = forward_feed(X)
loss = torch.sum((Y - Y_pred)**2)
loss_list.append(loss)
loss.backward()
with torch.no_grad():
w -= lr*w.grad
b -= lr*b.grad
w.grad.zero_()
b.grad.zero_()
如果我使用此代码,我会得到预期的结果,即我的代码能够估计权重和偏差。但是,如果我改变梯度下降代码行如下:
w =w- lr*w.grad
b =b- lr*b.grad
我收到以下错误:
AttributeError Traceback (most recent call
last)
<ipython-input-199-84b86804d4d5> in <module>()
---> 41 w.grad.zero_()
42 b.grad.zero_()
AttributeError: 'NoneType' object has no attribute 'zero_'
谁能帮我解决这个问题?
我确实尝试在谷歌上查看答案并找到了相关链接:https://github.com/pytorch/pytorch/issues/7731。但这与我所面临的完全相反。根据此链接,他们说就地分配会导致问题,因为张量共享相同的存储。但是,对于我的代码,Inplace 操作不是正常操作。
【问题讨论】:
标签: python machine-learning pytorch