【问题标题】:Pytorch showing the error: 'NoneType' object has no attribute 'zero_'Pytorch 显示错误:“NoneType”对象没有属性“zero_”
【发布时间】:2021-06-11 02:25:40
【问题描述】:

我正在使用 Python 3.8 和 VSCode。

我尝试创建一个没有激活和偏差的基本神经网络,但由于错误,我无法更新权重的梯度。

矩阵详情:

层形状:(1,神经元数量)

权重层形状:(上一层的神经元数,下一层的神经元数)

这是我的代码:

# sample neural network

# importing libraries
import torch
import numpy as np
torch.manual_seed(0)

# hyperparameters
epochs = 100
lr = 0.01 # learning rate

# data
X_train = torch.tensor([[1]], dtype = torch.float)
y_train = torch.tensor([[2]], dtype = torch.float)

'''
Network Architecture:
    1 neuron in the first layer
    4 neurons in the second layer
    4 neurons in the third layer
    1 neuron in the last layer

    * I haven't added bias and activation.
'''

# initializing the weights
weights = []
weights.append(torch.rand((1, 4), requires_grad = True))
weights.append(torch.rand((4, 4), requires_grad = True))
weights.append(torch.rand((4, 1), requires_grad = True))

# calculating y_pred
y_pred = torch.matmul(torch.matmul(torch.matmul(X_train, weights[0]), weights[1]), weights[2])

# calculating loss
loss = (y_pred - y_train)**2

# calculating the partial derivatives
loss.backward()

# updating the weights and zeroing the gradients
with torch.no_grad():
    for i in range(len(weights)):
        weights[i] = weights[i] - weights[i].grad
        weights[i].grad.zero_()

它显示错误:

File "test3.py", line 43, in <module>
weights[i].grad.zero_()
AttributeError: 'NoneType' object has no attribute 'zero_'

我不明白为什么它会显示此错误。谁能解释一下?

【问题讨论】:

  • 因为weights[i] = weights[i] - weights[i].grad 中的weights[i].grad 属于Nonetype

标签: machine-learning neural-network pytorch


【解决方案1】:

您的模型没有任何可训练的参数来计算 grad。使用火炬的参数。请参阅此link 以创建具有可学习参数的模块。

  torch.nn.parameter.Parameter
        A kind of Tensor that is to be considered a module parameter.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-14
    • 2021-02-17
    • 2023-02-17
    • 2017-12-20
    相关资源
    最近更新 更多