forked from 459548764/Myself-Machine-Learning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tu
143 lines (121 loc) · 5 KB
/
tu
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import numpy as np
import random
class MultiNetwork():
def __init__(self,
layer_dims):
self.parameter = {}
L = len(layer_dims)
for i in range(1,L):
self.parameter['W'+str(i)] = np.random.rand(layer_dims[i-1],
layer_dims[i])/np.sqrt(layer_dims[i-1])
self.parameter['b'+str(i)] = np.zeros([1,layer_dims[i]])
assert(self.parameter['W'+str(i)].shape == (layer_dims[i-1],layer_dims[i]))
assert(self.parameter['b'+str(i)].shape == (1,layer_dims[i]))
def _sigmoid(self,z):
return 1/(1+np.exp(-z))
def _diff_sigmoid(self,z):
return self._sigmoid(z)*(1-self._sigmoid(z))
def _relu(self,z):
return np.maximum(0,z)
def _diff_relu(self,z):
dz = np.array(z,copy = True)
dz[z <= 0] = 0
return dz
def linear_forward(self,
input,
weight,
bias):
z = np.dot(input,weight) + bias
assert(z.shape == (input.shape[0],weight.shape[1]))
return z
def linear_activation_forward(self,
input,
weight,
bias,
method):
z = self.linear_forward(input,weight,bias)
if method == 'sigmoid':
a = self._sigmoid(z)
if method == 'none':
a = z
if method == 'relu':
a = self._relu(z)
return a
def forward(self,
data):
self.backparameter = {}
self.cache = {}
input = data
L = len(self.parameter)//2
for i in range(1,L):
input_pre = input
self.cache['X'+str(i)] = input_pre
input = self.linear_activation_forward(input_pre,
self.parameter['W'+str(i)],
self.parameter['b'+str(i)],
'sigmoid')
self.backparameter['diff'+str(i)] = self._diff_sigmoid(input)
self.cache['X'+str(L)] = input
output = self.linear_activation_forward(input,
self.parameter['W'+str(L)],
self.parameter['b'+str(L)],
'relu')
self.backparameter['diff'+str(L)] = self._diff_relu(output)
return output
def compute_loss(self,
output,
label,
method):
if method == 'softmax':
loss = output - label
print(output)
return loss
def backward(self,
loss):
self.gradient = {}
L = len(self.parameter)//2
self.gradient['db' + str(L)] = loss*self.backparameter['diff'+str(L)]
self.gradient['dW' + str(L)] = np.dot(self.cache['X'+str(L)].T,
loss*self.backparameter['diff'+str(L)])
self.gradient['dA' + str(L)] = np.dot(loss*self.backparameter['diff'+str(L)],
self.parameter['W'+str(L)].T,)
for i in reversed(range(1,L)):
self.gradient['db' + str(i)] = self.gradient['dA' + str(i+1)]
self.gradient['dW' + str(i)] = np.dot(self.cache['X'+str(i)].T,
self.gradient['dA' + str(i+1)])
self.gradient['dA' + str(i)] = np.dot(self.gradient['dA' + str(i+1)],
self.parameter['W'+str(i)].T)
def update(self,
studyratio,
dropout):
L = len(self.parameter)//2
for i in range(1,L+1):
total_drop = int(dropout * self.parameter['W'+str(i)].shape[1])
resultList = random.sample(range(0,self.parameter['W'+str(i)].shape[1]),
total_drop)
for dt in resultList:
self.gradient['dW'+str(dt)] = 0
self.gradient['db'+str(dt)] = 0
self.parameter['W'+str(i)] = self.parameter['W'+str(i)] - studyratio*self.gradient['dW'+str(i)]
self.parameter['b'+str(i)] = self.parameter['b'+str(i)] - studyratio*self.gradient['db'+str(i)]
def train(self,
data,
label,
studyratio,
dropout = 0,
iteration = 2000):
for i in range(iteration):
output = self.forward(data)
loss = self.compute_loss(output,label,'softmax')
self.backward(loss)
self.update(studyratio,dropout)
data = np.array([[1.0,1.0],[0.0,0.0],[0.0,1.0],[1.0,0.0]])
label = np.array([[1.0,0.0],
[1.0,0.0],
[0.0,1.0],
[0.0,1.0]])
studyratio = 0.1
dropout = 0.25
Layers = [2,4,4,2]
bp = MultiNetwork(Layers)
bp.train(data,label,studyratio,dropout,2000)