【发布时间】: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