forked from nicodjimenez/lstm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lstm_sin_train.py
71 lines (57 loc) · 1.77 KB
/
lstm_sin_train.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
import numpy as np
from LSTM.lstm import LstmParam, LstmNetwork
import pickle as pk
class Loss:
def __init__(self,mem_cell_ct):
self.v=np.zeros(mem_cell_ct)
def value(self, pred):
out=self.v.dot(pred)
return out
def loss(self, pred, label):
out=self.value(pred)
return (out- label) ** 2
def bottom_diff(self, pred, label):
out=self.value(pred)
df = 2 * (out - label)
diff=df*self.v
self.v-=0.01*pred*df
return diff
T = 50
x_dim = 40
def train():
# learns to repeat simple sequence from random inputs
np.random.seed(0)
np.random.seed(2)
L = 100
N = 500
x = np.empty((N, L), 'int64')
t=np.arange(N)
np.random.shuffle(t)
x = np.array(range(L)) +t.reshape(N, 1)
w=2*np.pi/T
data = np.sin(w*x).astype('float64')
# parameters for input data dimension and lstm cell count
mem_cell_ct = 100
lstm_param = LstmParam(mem_cell_ct, x_dim)
lstm_net = LstmNetwork(lstm_param)
loss=Loss(mem_cell_ct)
epoch=1
for cur_iter in range(epoch):
for n in range(N):
input_val_arr = data[n, :len(data)-x_dim]
y_list =data[n, x_dim:]
print("iter", "%2s" % str(cur_iter), end=": ")
for ind in range(len(y_list)):
lstm_net.x_list_add(input_val_arr[ind:ind+x_dim])
lossv = lstm_net.y_list_is(y_list, loss)
print("loss:", "%.3e" % lossv)
lstm_param.apply_diff(lr=0.01)
lstm_net.x_list_clear()
#模型保存
if cur_iter % 10 ==0:
fs = open('model/model_%d.pkl'%cur_iter, 'wb')
pk.dump(lstm_param,fs)
pk.dump(loss,fs)
fs.close()
if __name__ == "__main__":
train()