forked from codefan/python-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBackwardMultiLayerNet.py
More file actions
75 lines (60 loc) · 2.15 KB
/
Copy pathBackwardMultiLayerNet.py
File metadata and controls
75 lines (60 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import numpy as np
from common.layers import *
from common.gradient import numerical_gradient
class MultiLayerNet:
def __init__(self, layerSize, weight_init_std = 0.01):
self.l = len(layerSize) - 1
# 初始化权重
self.W = []
self.B = []
self.layers = []
# 初始化权重
for i in range(self.l):
# np.random.randn 为符合高斯分布(正态分布)的随机数据
self.W.append(weight_init_std * np.random.randn(layerSize[i], layerSize[i+1]))
self.B.append(np.zeros(layerSize[i+1]))
# 生成层
self.layers.append(Affine(self.W[i], self.B[i]))
# 最后一层 输出层 不需要激活函数, 使用 SoftmaxWithLoss
if(i<self.l-1):
self.layers.append(Relu())
self.lastLayer = SoftmaxWithLoss()
def predict(self, x):
for layer in self.layers:
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.ndim != 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)
gW = []
gB = []
for i in range(self.l):
gW.append(numerical_gradient(loss_W, self.W[i]))
gB.append(numerical_gradient(loss_W, self.B[i]))
return gW, gB
def gradient(self, x, t):
# forward
self.loss(x, t)
# backward
dout = 1
dout = self.lastLayer.backward(dout)
for layer in self.layers[::-1]:
dout = layer.backward(dout)
# 设定
gW = []
gB = []
for layer in self.layers:
if type(layer) == Affine :
gW.append(layer.dW)
gB.append(layer.db)
return gW, gB