跟着教材《动手学深度学习》里循环神经网络报错

这个是梯度裁剪时出现的,查了很多报错分析,貌似没有和我一样的,具体内容章节在书的6.4,谢谢解答

<ipython-input-45-464944151d51> in grad_clipping(params, theta, ctx)
      2     norm = nd.array([0], ctx)
      3     for param in params:
----> 4         norm += (param.grad**2).sum()
      5     norm = norm.sqrt().asscalar()
      6     if norm > theta:

TypeError: unsupported operand type(s) for ** or pow(): 'NoneType' and 'int'

原函数

def grad_clipping(params, theta, ctx):
    norm = nd.array([0], ctx)
    for param in params:
        norm += (param.grad**2).sum()
    norm = norm.sqrt().asscalar()
    if norm > theta:
        for param in params:
            param.grad[:] *= theta / norm

param.grad是一个空值类型 ,无法与数据运算。检查params类型,如果是字典型的话,调用键为grad值的方法是param['grad']。

如有帮助,请点采纳。

检查一下你的param里面有没有grad属性?nonetype说明可能找不到这个属性

检查一下param.grad的数据类型,或者是否为空。报错的意思就是这个数据类型不对或者为空的时候不能进行平方操作