This repository has been archived by the owner on Jan 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lstm.py
158 lines (116 loc) · 5.98 KB
/
lstm.py
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# -*- coding: utf-8 -*-
import numpy as np
import pickle
class Weights:
def __init__(self, dim_s, dim_x, dim_y = None):
self.dim_x = dim_x
self.dim_s = dim_s
self.dim_y = dim_y or dim_s
self.wg = 0.01 * np.random.randn(self.dim_s, self.dim_x + self.dim_s)
self.wi = 0.01 * np.random.randn(self.dim_s, self.dim_x + self.dim_s)
self.wo = 0.01 * np.random.randn(self.dim_s, self.dim_x + self.dim_s)
self.wy = 0.01 * np.random.randn(self.dim_y, self.dim_s)
self.dwg = np.zeros((self.dim_s, self.dim_x + self.dim_s))
self.dwi = np.zeros((self.dim_s, self.dim_x + self.dim_s))
self.dwo = np.zeros((self.dim_s, self.dim_x + self.dim_s))
self.dwy = np.zeros((self.dim_y, self.dim_s))
def descend_gradient(self, learning_rate=0.3):
self.wg -= learning_rate * self.dwg
self.wi -= learning_rate * self.dwi
self.wo -= learning_rate * self.dwo
self.wy -= learning_rate * self.dwy
self.dwg = np.zeros((self.dim_s, self.dim_x + self.dim_s))
self.dwi = np.zeros((self.dim_s, self.dim_x + self.dim_s))
self.dwo = np.zeros((self.dim_s, self.dim_x + self.dim_s))
self.dwy = np.zeros((self.dim_y, self.dim_s))
def sigmoid(x):
return 1/(1 + np.exp(-x))
def softmax(x):
exp_x = np.exp(x)
return exp_x / np.sum(exp_x)
def cost_function(x, y):
return 0.5*np.norm2(x-y)
def cost_function_derivative(x, y):
return x-y
class LstmCell:
def __init__(self, weights):
self.weights = weights
def propagate(self, previous_s, previous_h, x):
self.x = x
self.x_concatenated = np.concatenate((previous_h, x), axis=0)
self.g = sigmoid(np.dot(self.weights.wg, self.x_concatenated))
self.i = np.tanh(np.dot(self.weights.wi, self.x_concatenated))
self.s = self.g * self.i + previous_s
self.o = sigmoid(np.dot(self.weights.wo, self.x_concatenated))
self.l = np.tanh(self.s)
self.h = self.l * self.o
#self.y = sigmoid(np.dot(self.weights.wy, self.h))
self.y = softmax(np.dot(self.weights.wy, self.h))
def backpropagate(self, ds, dh, y_true):
#dJ = cost_function_derivative(self.y, y_true)*self.y*(1-self.y)
dy = -np.dot(self.y, self.y.T) + np.diag(self.y.flatten())
dJ = np.dot(dy, cost_function_derivative(self.y, y_true))
self.dh = dh + np.dot(self.weights.wy.T, dJ)
self.weights.dwy += np.dot(dJ, self.h.T)
self.weights.dwo += np.dot(self.dh*self.l*self.o*(1-self.o), self.x_concatenated.T)
self.ds = ds + self.dh*self.o*(1-self.l**2)
self.weights.dwi += np.dot(self.ds*self.g*(1-self.i**2), self.x_concatenated.T)
self.weights.dwg += np.dot(self.ds*self.i*self.g*(1-self.g), self.x_concatenated.T)
self.dh = np.dot(self.weights.wi.T, self.ds*self.g*(1-self.i**2)) + \
np.dot(self.weights.wg.T, self.ds*self.i*self.g*(1-self.g)) + \
np.dot(self.weights.wo.T, self.dh*self.l*self.o*(1-self.o))
self.dh = self.dh[:self.weights.dim_s]
return self.ds, self.dh
class LstmNetwork:
def __init__(self, weights, length):
self.weights = weights
self.cells = [LstmCell(self.weights) for _ in range(length)]
self.length = length
def propagate(self, X, previous_s=None, previous_h=None):
previous_s = previous_s or np.zeros((self.weights.dim_s, 1))
previous_h = previous_h or np.zeros((self.weights.dim_s, 1))
self.cells[0].propagate(previous_s, previous_h, X[0])
for i in range(1,self.length):
self.cells[i].propagate(self.cells[i-1].s, self.cells[i-1].h, X[i])
return [self.cells[i].y for i in range(self.length)]
def propagate_self_feeding(self, x, previous_s=None, previous_h=None):
previous_s = previous_s or np.zeros((self.weights.dim_s, 1))
previous_h = previous_h or np.zeros((self.weights.dim_s, 1))
self.cells[0].propagate(previous_s, previous_h, x)
for i in range(1,self.length):
self.cells[i].propagate(self.cells[i-1].s, self.cells[i-1].h, self.cells[i-1].y)
return [self.cells[i].y for i in range(self.length)]
def learn(self, X, Y, learning_rate=0.3, previous_s=None, previous_h=None):
self.propagate(X, previous_s, previous_h)
self.cells[-1].backpropagate(np.zeros((self.weights.dim_s, 1)), np.zeros((self.weights.dim_s, 1)), Y[-1])
for i in range(self.length-2, -1, -1):
self.cells[i].backpropagate(self.cells[i+1].ds, self.cells[i+1].dh, Y[i])
self.weights.descend_gradient(learning_rate)
def learn_self_feeding(self, x, Y, learning_rate=0.3, previous_s=None, previous_h=None):
self.propagate_self_feeding(x, previous_s, previous_h)
self.cells[-1].backpropagate(np.zeros((self.weights.dim_s, 1)), np.zeros((self.weights.dim_s, 1)), Y[-1])
for i in range(self.length-2, -1, -1):
self.cells[i].backpropagate(self.cells[i+1].ds, self.cells[i+1].dh, Y[i])
self.weights.descend_gradient(learning_rate)
def cell_test():
weights = Weights(dim_s, dim_x)
cell = LstmCell(weights)
print('ok creation LstmCell')
cell.propagate(np.ones((dim_s, 1)), np.ones((dim_s, 1)), np.ones((dim_x, 1)))
print('ok propagation')
cell.backpropagate(np.ones((dim_s, 1)), np.ones((dim_s, 1)), np.ones((dim_s, 1)))
print('ok backpropagation')
cell.weights.descend_gradient(0.3)
print('ok apprentissage')
def network_test():
network = LstmNetwork(dim_s, dim_x, 3)
print('ok creation LstmNetwork')
network.propagate(np.ones((3,dim_x,1)))
print('ok propagation reseau')
network.learn(np.ones((3,dim_x,1)), np.ones((3,dim_s,1)))
print('ok learning')
if __name__ == '__main__':
dim_s = 50
dim_x = 20
cell_test()
network_test()