赞
踩
DL之simpleNet:利用自定义的simpleNet(设好权重)对新样本进行预测、评估、输出梯度值
导读
理解神经网络内部的数学机制
目录
输出权重参数: [[-0.94465146 -1.22617291 0.89064915]
[-0.36625531 1.57482914 0.82170746]]
输出预测: [-0.89642065 0.68164248 1.27392621]
最大值的索引: 2
loss: 0.5111458955952319
求出梯度值:
[[ 0.04107652 0.19903878 -0.2401153 ]
[ 0.06161477 0.29855818 -0.36017295]]
- 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
-
- net = simpleNet()
-
- x = np.array([0.6, 0.9])
- p = net.predict(x)
-
-
- t= np.array([0, 0, 1])
- print('loss:',net.loss(x, t))
-
-
-
-
- f = lambda w: net.loss(x, t)
-
- dW = numerical_gradient(f, net.W)
- print('求出梯度值:','\n',dW)
相关文章
DL之simpleNet:利用自定义的simpleNet(设好权重)对新样本进行预测、评估、输出梯度值
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。