在神经网络的学习中,梯度的计算非常重要。神经网络中的梯度,指的就是损失函数关于权重参数的梯度。
我们以一个单层的简单网络为例,形状为2×3,权重参数为W,损失函数记为L。那么它的权重参数和梯度为:
![图片[1]-3.3 神经网络的梯度计算-大赛人网](https://www.dsrw.com/wp-content/uploads/2026/11/image-237.png)
class simpleNet:
def __init__(self):
self.W = np.random.randn(2,3)
def predict(self, x):
return np.dot(x, self.W)
def loss(self, x, t):
z = self.predict(x)
y = softmax(z)
loss = cross_entropy_error(y, t)
return loss
x = np.array([0.6, 0.9])
t = np.array([0, 0, 1])
net = simpleNet()
f = lambda w: net.loss(x, t)
dW = numerical_gradient(f, net.W)
print(dW)
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
















请登录后查看评论内容