在练习二中,手写数字识别使用数值微分的方式实现了神经网络,现在用误差反向传播法来实现。两者的区别仅仅是使用不同方法求梯度。
1、2层神经网络的类
将2层神经网络实现为一个TwoLayerNet的类(和上次的代码仅仅是求梯度的方式不同,不同的地方加*表示):
class TwoLayerNet:
def __init__(self, input_size, hidden_size, output_size, weight_init_std=0.01):
# 初始化权重
self.params = {}
self.params['W1'] = weight_init_std *np.random.randn(input_size, hidden_size)
self.params['b1'] = np.zeros(hidden_size)
self.params['W2'] = weight_init_std *np.random.randn(hidden_size, output_size)
self.params['b2'] = np.zeros(output_size)
* #生成层
* self.layers = OrderedDict()
* self.layers['Affine1'] = Affine(self.params['W1'], self.params['b1'])
* self.layers['Relu1'] = Relu()
* self.layers['Affine2'] = Affine(self.params['W2'], self.params['b2'])
* self.lastLayer = SoftmaxWithLoss()
* def predict(self, x):
for layer in self.layers.values():
x = layer.forward(x)
return x
# x:输入数据, t:监督数据
def loss(self, x, t):
y = self.predict(x)
* return self.lastLayer.forward(y, t)
def accuracy(self, x, t):
y = self.predict(x)
y = np.argmax(y, axis=1)
* if t.dim != 1 : t=np.argmax(t,axis=1)
accuracy = np.sum(y == t) / float(x.shape[0])
return accuracy
# x:输入数据, t:监督数据
def numerical_gradient(self, x, t):
loss_W = lambda W: self.loss(x, t)
grads = {}
grads['W1'] = numerical_gradient(loss_W, self.params['W1'])
grads['b1'] = numerical_gradient(loss_W, self.params['b1'])
grads['W2'] = numerical_gradient(loss_W, self.params['W2'])
grads['b2'] = numerical_gradient(loss_W, self.params['b2'])
return grads
* def gradient(self, x, t):
# forward
self.loss(x, t)
# backward
dout = 1
dout = self.lastLayer.backward(dout)
layers = list(self.layers.values())
layers.reverse()
for layer in layers:
dout = layer.backward(dout)
# 设定
grads = {}
grads['W1'], grads['b1'] = self.layers['Affine1'].dW, self.layers['Affine1'].db
grads['W2'], grads['b2'] = self.layers['Affine2'].dW, self.layers['Affine2'].db
return grads