-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlstm-kenneth.py
More file actions
64 lines (54 loc) · 2.24 KB
/
lstm-kenneth.py
File metadata and controls
64 lines (54 loc) · 2.24 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
#!/usr/bin/env python3
from keras.models import Model, Sequential
from keras.layers import Dense, LSTM, Input, merge, Bidirectional
import numpy as np
import json
def pp_json(json_thing, sort=True, indents=4):
if type(json_thing) is str:
print(json.dumps(json.loads(json_thing), sort_keys=sort, indent=indents))
else:
print(json.dumps(json_thing, sort_keys=sort, indent=indents))
return None
def input_gen(shape, dtype=np.float32):
inp = np.empty(shape, dtype=dtype)
i = 0
for x in np.nditer(inp, op_flags=['readwrite']):
x[...] = i + 0.05
i+= 0.05
print("Inputs:")
print(inp)
return inp
def model(timestep, dimension, output):
inp = Input(shape=(timestep, dimension))
forward1 = LSTM(units=int(output/2), activation='tanh', recurrent_activation='hard_sigmoid',
unit_forget_bias=False,
return_sequences=True)(inp)
backward1 = LSTM(units=int(output/2), activation='tanh', recurrent_activation='hard_sigmoid',
return_sequences=True,
unit_forget_bias=False,
go_backwards=True)(inp)
blstm1 = merge([forward1, backward1], mode="concat", concat_axis=-1)
forward2 = LSTM(units=int(output/2), activation='tanh', recurrent_activation='hard_sigmoid',
unit_forget_bias=False,
return_sequences=True)(blstm1)
backward2 = LSTM(units=int(output/2), activation='tanh', recurrent_activation='hard_sigmoid',
return_sequences=True,
unit_forget_bias=False,
go_backwards=True)(blstm1)
blstm2 = merge([forward2, backward2], mode="concat", concat_axis=-1)
model = Model(inputs=[inp], outputs=blstm2)
print(model.summary())
print(model.get_config())
return model
def model2(timestep, dimension, output):
model = Sequential()
model.add(Bidirectional(LSTM(units=int(output/2),activation='tanh', return_sequences=True), input_shape=(timestep, dimension)))
model.add(Bidirectional(LSTM(units=int(output/2),activation='tanh', return_sequences=True)))
print(model.summary())
print(model.get_config())
return model
x = input_gen([1, 4, 6])
m = model(4, 6, 6)
predict = m.predict(x)
print(predict)
import gc; gc.collect()