forked from C2Tao/Interspeech-2016
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_keras.py
166 lines (126 loc) · 5.93 KB
/
test_keras.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
159
160
161
162
163
164
165
166
from keras.layers import containers, Dropout, LSTM
import theano
import theano.tensor as T
from keras.optimizers import RMSprop
from keras import models
from keras.utils import generic_utils
import numpy as np
from keras.layers.core import Activation, AutoEncoder, Dense, TimeDistributedDense, TimeDistributedMerge, Merge, Lambda
from keras.layers.recurrent import LSTM, GRU, SimpleRNN
from keras.layers.recurrent import Highway_LSTM, Highway_GRU, Highway_SimpleRNN
from keras.models import Sequential, Graph
def test_sequential():
model = Sequential()
model.add(Dense(10, input_dim=13))
model.add(Dense(13))
model.add(Activation('tanh'))
model.compile(loss='mse', optimizer='adam')
X_train = np.random.rand(64,13)
model.fit(X_train, X_train, nb_epoch=100,batch_size=16, verbose=1)
def test_lstm():
model = Sequential()
model.add(TimeDistributedDense(10, input_dim=13))
model.add(LSTM(7, return_sequences=True))
model.add(TimeDistributedDense(13))
model.add(Activation('tanh'))
model.compile(loss='mse', optimizer='adam')
X_train = np.random.rand(64,10,13)
model.fit(X_train, X_train, nb_epoch=100,batch_size=16, verbose=1)
def test_custom_lstm():
from keras.layers.recurrent import Highway_LSTM, Highway_GRU, Highway_SimpleRNN
#from keras.layers.recurrent import Residual_LSTM, Residual_GRU, Residual_SimpleRNN
rnn = LSTM
rnn = GRU
rnn = SimpleRNN
rnn = Highway_LSTM
rnn = Highway_GRU
rnn = Highway_SimpleRNN
#rnn = Residual_LSTM
#rnn = Residual_GRU
#rnn = Residual_SimpleRNN
model = Sequential()
model.add(TimeDistributedDense(10, input_dim=13))
model.add(rnn(10, return_sequences=True))
model.add(TimeDistributedDense(13))
model.add(Activation('tanh'))
model.compile(loss='mse', optimizer='adam')
X_train = np.random.rand(64,10,13)
model.fit(X_train, X_train, nb_epoch=100,batch_size=16, verbose=1)
def test_graph():
graph = Graph()
graph.add_input(name='input', input_shape=(32,))
graph.add_node(Dense(16), name='dense1', input='input')
graph.add_node(Dense(32), name='dense2', input='input')
graph.add_node(Dense(32), name='dense3', input='dense1')
graph.add_output(name='output1', input='dense2')
graph.add_output(name='output2', input='dense3')
graph.compile(optimizer='rmsprop', loss={'output1':'mse', 'output2':'mse'})
X_train = np.random.rand(64, 32)
history = graph.fit({'input':X_train, 'output1':X_train, 'output2':X_train}, nb_epoch=10)
def test_custom_objective():
graph = Graph()
graph.add_input(name='input', input_shape=(32,))
graph.add_node(Dense(16), name='dense1', input='input')
graph.add_node(Dense(32), name='dense2', input='input')
graph.add_node(Dense(32), name='dense3', input='dense1')
graph.add_output(name='output1', input='dense2')
graph.add_output(name='output2', input='dense3')
X_train = np.random.rand(64, 32)
import theano
import theano.tensor as T
epsilon = 1.0e-9
def custom_objective(y_true, y_pred):
'''Just another crossentropy'''
y_pred = T.clip(y_pred, epsilon, 1.0 - epsilon)
y_pred /= y_pred.sum(axis=-1, keepdims=True)
cce = T.nnet.categorical_crossentropy(y_pred, y_true)
return cce
graph.compile(optimizer='rmsprop', loss={'output1':'mse', 'output2':custom_objective}, loss_weights={'output1':1,'output2':-0.001})
history = graph.fit({'input':X_train, 'output1':X_train, 'output2':X_train}, nb_epoch=10)
print graph.inputs['input']
print graph.nodes['dense1']
def test_graph_merge():
graph = Graph()
graph.add_input(name='input', input_shape=[10,39])
graph.add_node(TimeDistributedDense(10), name='proj', input='input')
graph.add_node(LSTM(10, return_sequences=True), name='lstm', input='proj')
graph.add_node(TimeDistributedDense(10), merge_mode = 'sum', name = 'merge', inputs=['proj','lstm'])
graph.add_node(TimeDistributedDense(39), name='proj2', input='merge')
graph.add_output(name='output', input='proj2')
X_train = np.random.rand(64,10, 39)
graph.compile(optimizer='rmsprop', loss={'output':'mse' })
history = graph.fit({'input':X_train, 'output':X_train}, nb_epoch=10)
def test_lambda_join():
def custom_fun(X):
print X.keys()
return X['proj']+X['lstm']
graph = Graph()
graph.add_input(name='input', input_shape=[10, 39])
graph.add_node(TimeDistributedDense(7), name='proj', input='input')
graph.add_node(LSTM(7, return_sequences=True), name='lstm', input='proj')
graph.add_node(Lambda(custom_fun, output_shape=[10, 7]), merge_mode = 'join', name = 'merge', inputs=['proj','lstm'])
graph.add_node(TimeDistributedDense(39), name='proj2', input='merge')
graph.add_output(name='output', input='proj2')
X_train = np.random.rand(64, 10, 39)
graph.compile(optimizer='rmsprop', loss={'output':'mse'})
history = graph.fit({'input':X_train, 'output':X_train}, nb_epoch=10)
def speed_limit():
#almost there omg
def custom_fun(X):
from keras.objectives import mean_squared_error as mse
print X.keys()
return mse(X['proj'], X['lstm'])
graph = Graph()
graph.add_input(name='input', input_shape=[10, 39])
graph.add_node(TimeDistributedDense(7), name='proj', input='input')
graph.add_node(LSTM(7, return_sequences=True), name='lstm', input='proj')
graph.add_node(Lambda(custom_fun, output_shape=[10, 7]), merge_mode = 'join', name = 'diff', inputs=['proj','lstm'])
graph.add_output(name = 'reconstruction', input='diff')
graph.add_node(TimeDistributedDense(39), name='proj2', input='lstm')
graph.add_output(name='output', input='proj2')
speed = 0
X_train = np.random.rand(64, 10, 39)
zero_vec = np.ones([64, 10]) *speed
graph.compile(optimizer='rmsprop', loss={'output':'mse', 'reconstruction':'mse'})
history = graph.fit({'input':X_train, 'output':X_train, 'reconstruction': zero_vec}, nb_epoch=10)
test_custom_lstm()