param.grad为 None或者TypeError: unsupported operand type(s) for *: ‘float‘ and ‘NoneType‘

作者在跟随《动手学深度学习》实现softmax回归时遇到TypeError,原因是W变量未设置require_grad。修正后设置W为可求梯度,解决了debug过程中的困扰。
摘要由CSDN通过智能技术生成

在学习李沐的动手学深度学习,从零开始实现softmax回归中,我跟着敲完代码,发现无法运行,报错入如下:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[72], line 3
      1 num_epochs = 10
      2 print("000000000000000000000")
----> 3 train_ch3(net,train_iter,test_iter,cross_entropy,num_epochs,updater)

Cell In[71], line 6, in train_ch3(net, train_iter, test_iter, loss, num_epochs, updater)
      3 animator = Animator(xlabel = 'epoch',xlim=[1,num_epochs],ylim=[0.3,0.9],
      4                     legend=['train loss','train acc','test acc'])
      5 for epoch in range(num_epochs):
----> 6     train_metrics = train_epoch_ch3(net,train_iter,loss,updater)
      7     test_acc = evaluate_accuracy(net,test_iter)
      8     animator.add(epoch + 1,train_metrics + (test_acc,))

Cell In[69], line 21, in train_epoch_ch3(net, train_iter, loss, updater)
     19     print(X.shape[0])
     20     print("W.grad:",W.grad)
---> 21     updater(X.shape[0])
     22 metric.add(float(l.sum()),accuracy(y_hat,y),y.numel())
     23 #返回训练损失和训练精度

Cell In[67], line 3, in updater(batch_size)
      2 def updater(batch_size):
----> 3     return d2l.sgd([W,b],lr,batch_size)
...
    153 print("param.gram数据类型:")
--> 154 param.data.sub_(lr*param.grad/batch_size)
    155 param.grad.data.zero_()

TypeError: unsupported operand type(s) for *: 'float' and 'NoneType'

经过debug发现是param.grad(即W.grad)为None,即求梯度失败,反复看了代码,发现是在一开始W的定义中没有设置为求梯度,如下所示

W = torch.normal(0,0.01,size = (num_inputs, num_outputs))

加入计算梯度的参数后为:

W = torch.normal(0,0.01,size = (num_inputs, num_outputs),require_grad = True)

最终问题得到解决,以后写代码一定认真点,因为这一个错误,debug了好久。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
>