Skip to content

Commit 32172a6

Browse files
shagunsodhanizygmuntz
authored andcommitted
Updated models to keras2.0 (#1)
* updated .gitignore and updated PointerLSTM and train.py for keras2.0 * updated remaining files to keras2.0
1 parent c514832 commit 32172a6

8 files changed

+209
-206
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,7 @@ ENV/
9999

100100
# mypy
101101
.mypy_cache/
102+
103+
.idea/*
104+
model_weights/*
105+

PointerLSTM.py

+30-14
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
from keras import initializations
2-
from keras.layers.recurrent import time_distributed_dense
1+
# from keras import initializations
2+
import keras.backend as K
33
from keras.activations import tanh, softmax
4-
from keras.layers import LSTM
54
from keras.engine import InputSpec
6-
import keras.backend as K
5+
from keras.layers import LSTM
6+
from keras.layers.recurrent import Recurrent
7+
from keras.layers.recurrent import _time_distributed_dense
78

89

910
class PointerLSTM(LSTM):
@@ -12,19 +13,31 @@ def __init__(self, hidden_shape, *args, **kwargs):
1213
self.input_length = []
1314
super(PointerLSTM, self).__init__(*args, **kwargs)
1415

16+
def get_initial_states(self, x_input):
17+
return Recurrent.get_initial_state(self, x_input)
18+
1519
def build(self, input_shape):
1620
super(PointerLSTM, self).build(input_shape)
1721
self.input_spec = [InputSpec(shape=input_shape)]
18-
init = initializations.get('orthogonal')
19-
self.W1 = init((self.hidden_shape, 1))
20-
self.W2 = init((self.hidden_shape, 1))
21-
self.vt = init((input_shape[1], 1))
22+
# init = initializations.get('orthogonal')
23+
self.W1 = self.add_weight(name="W1",
24+
shape=(self.hidden_shape, 1),
25+
initializer="uniform",
26+
trainable=True)
27+
self.W2 = self.add_weight(name="W2",
28+
shape=(self.hidden_shape, 1),
29+
initializer="uniform",
30+
trainable=True)
31+
self.vt = self.add_weight(name="vt",
32+
shape=(input_shape[1], 1),
33+
initializer='uniform',
34+
trainable=True)
2235
self.trainable_weights += [self.W1, self.W2, self.vt]
2336

2437
def call(self, x, mask=None):
2538
input_shape = self.input_spec[0].shape
2639
en_seq = x
27-
x_input = x[:, input_shape[1]-1, :]
40+
x_input = x[:, input_shape[1] - 1, :]
2841
x_input = K.repeat(x_input, input_shape[1])
2942
initial_states = self.get_initial_states(x_input)
3043

@@ -41,17 +54,17 @@ def call(self, x, mask=None):
4154
return outputs
4255

4356
def step(self, x_input, states):
44-
#print "x_input:", x_input, x_input.shape
45-
# <TensorType(float32, matrix)>
46-
57+
# print "x_input:", x_input, x_input.shape
58+
# <TensorType(float32, matrix)>
59+
4760
input_shape = self.input_spec[0].shape
4861
en_seq = states[-1]
4962
_, [h, c] = super(PointerLSTM, self).step(x_input, states[:-1])
5063

5164
# vt*tanh(W1*e+W2*d)
5265
dec_seq = K.repeat(h, input_shape[1])
53-
Eij = time_distributed_dense(en_seq, self.W1, output_dim=1)
54-
Dij = time_distributed_dense(dec_seq, self.W2, output_dim=1)
66+
Eij = _time_distributed_dense(en_seq, self.W1, output_dim=1)
67+
Dij = _time_distributed_dense(dec_seq, self.W2, output_dim=1)
5568
U = self.vt * tanh(Eij + Dij)
5669
U = K.squeeze(U, 2)
5770

@@ -62,3 +75,6 @@ def step(self, x_input, states):
6275
def get_output_shape_for(self, input_shape):
6376
# output shape is not affected by the attention component
6477
return (input_shape[0], input_shape[1], input_shape[1])
78+
79+
def compute_output_shape(self, input_shape):
80+
return (input_shape[0], input_shape[1], input_shape[1])

test.py

+23-27
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,11 @@
22

33
"testing (validating) a trained model"
44

5-
import pickle
65
import numpy as np
7-
8-
from keras.models import Model
96
from keras.layers import LSTM, Input
10-
from keras.utils.np_utils import to_categorical
117
from keras.metrics import categorical_accuracy
12-
from sklearn.metrics import accuracy_score as accuracy
8+
from keras.models import Model
9+
from keras.utils.np_utils import to_categorical
1310

1411
from PointerLSTM import PointerLSTM
1512

@@ -24,18 +21,18 @@
2421
batch_size = 100
2522

2623
hidden_size = 64
27-
weights_file = 'model_weights/model_weights_{}_steps_{}.hdf5'.format( n_steps, hidden_size )
24+
weights_file = 'model_weights/model_weights_{}_steps_{}.hdf5'.format(n_steps, hidden_size)
2825

2926
#
3027

31-
x = np.loadtxt( x_file, delimiter = ',', dtype = int )
32-
y = np.loadtxt( y_file, delimiter = ',', dtype = int )
28+
x = np.loadtxt(x_file, delimiter=',', dtype=int)
29+
y = np.loadtxt(y_file, delimiter=',', dtype=int)
3330

34-
x = np.expand_dims( x, axis = 2 )
31+
x = np.expand_dims(x, axis=2)
3532

3633
YY = []
3734
for y_ in y:
38-
YY.append(to_categorical(y_))
35+
YY.append(to_categorical(y_))
3936
YY = np.asarray(YY)
4037

4138
x_train = x[:split_at]
@@ -52,31 +49,30 @@
5249
print("building model...")
5350
main_input = Input(shape=(seq_len, 1), name='main_input')
5451

55-
encoder = LSTM(output_dim = hidden_size, return_sequences = True, name="encoder")(main_input)
52+
encoder = LSTM(output_dim=hidden_size, return_sequences=True, name="encoder")(main_input)
5653
decoder = PointerLSTM(hidden_size, output_dim=hidden_size, name="decoder")(encoder)
5754

58-
model = Model( input=main_input, output=decoder )
55+
model = Model(input=main_input, output=decoder)
5956

60-
print( "loading weights from {}...".format( weights_file ))
57+
print("loading weights from {}...".format(weights_file))
6158
try:
62-
model.load_weights( weights_file )
59+
model.load_weights(weights_file)
6360
except IOError:
64-
print( "no weights file." )
65-
raise SystemExit
61+
print("no weights file.")
62+
raise SystemExit
6663

6764
model.compile(optimizer='rmsprop',
68-
loss='categorical_crossentropy',
69-
metrics=['accuracy'])
70-
65+
loss='categorical_crossentropy',
66+
metrics=['accuracy'])
7167

72-
print "testing..."
68+
print("testing...")
7369

74-
p = model.predict( x_test[0:1] )
75-
p_bin = p.argmax( axis = 2 )
70+
p = model.predict(x_test[0:1])
71+
p_bin = p.argmax(axis=2)
7672

77-
for y_, p_ in zip( y_test, p )[:5]:
78-
print "y_test:", y_
79-
print "p: ", p_.argmax( axis = 1 )
80-
print
73+
for y_, p_ in zip(y_test, p)[:5]:
74+
print("y_test:", y_)
75+
print("p: ", p_.argmax(axis=1))
76+
print()
8177

82-
print "categorical accuracy: ", categorical_accuracy( YY_test, p ).eval()
78+
print("categorical accuracy: ", categorical_accuracy(YY_test, p).eval())

test_sums.py

+23-27
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,11 @@
22

33
"testing (validating) a trained model"
44

5-
import pickle
65
import numpy as np
7-
8-
from keras.models import Model
96
from keras.layers import LSTM, Input
10-
from keras.utils.np_utils import to_categorical
117
from keras.metrics import categorical_accuracy
12-
from sklearn.metrics import accuracy_score as accuracy
8+
from keras.models import Model
9+
from keras.utils.np_utils import to_categorical
1310

1411
from PointerLSTM import PointerLSTM
1512

@@ -22,22 +19,22 @@
2219
batch_size = 100
2320

2421
hidden_size = 100
25-
weights_file = 'model_weights/model_weights_sums_{}.hdf5'.format( hidden_size )
22+
weights_file = 'model_weights/model_weights_sums_{}.hdf5'.format(hidden_size)
2623

2724
n_steps = 3
2825
n_features = 2
2926

3027
#
3128

32-
x = np.loadtxt( x_file, delimiter = ',', dtype = int )
33-
y = np.loadtxt( y_file, delimiter = ',', dtype = int )
29+
x = np.loadtxt(x_file, delimiter=',', dtype=int)
30+
y = np.loadtxt(y_file, delimiter=',', dtype=int)
3431

35-
x = x.reshape( x.shape[0], n_steps, -1 )
36-
assert( x.shape[-1] == n_features )
32+
x = x.reshape(x.shape[0], n_steps, -1)
33+
assert (x.shape[-1] == n_features)
3734

3835
YY = []
3936
for y_ in y:
40-
YY.append(to_categorical(y_))
37+
YY.append(to_categorical(y_))
4138
YY = np.asarray(YY)
4239

4340
x_train = x[:split_at]
@@ -52,31 +49,30 @@
5249
#
5350

5451
print("building model...")
55-
main_input = Input( shape=( x.shape[1], x.shape[2] ), name='main_input' )
52+
main_input = Input(shape=(x.shape[1], x.shape[2]), name='main_input')
5653

57-
encoder = LSTM(output_dim = hidden_size, return_sequences = True, name="encoder")(main_input)
54+
encoder = LSTM(output_dim=hidden_size, return_sequences=True, name="encoder")(main_input)
5855
decoder = PointerLSTM(hidden_size, output_dim=hidden_size, name="decoder")(encoder)
5956

60-
model = Model( input=main_input, output=decoder )
57+
model = Model(input=main_input, output=decoder)
6158

62-
print( "loading weights from {}...".format( weights_file ))
59+
print("loading weights from {}...".format(weights_file))
6360
try:
64-
model.load_weights( weights_file )
61+
model.load_weights(weights_file)
6562
except IOError:
66-
print( "no weights file." )
63+
print("no weights file.")
6764

6865
model.compile(optimizer='rmsprop',
69-
loss='categorical_crossentropy',
70-
metrics=['accuracy'])
71-
72-
print( 'testing...')
66+
loss='categorical_crossentropy',
67+
metrics=['accuracy'])
7368

74-
p = model.predict( x_test )
69+
print('testing...')
7570

76-
for y_, p_ in zip( y_test, p )[:5]:
77-
print "y_test:", y_
78-
print "p: ", p_.argmax( axis = 1 )
79-
print
71+
p = model.predict(x_test)
8072

81-
print "categorical accuracy: ", categorical_accuracy( YY_test, p ).eval()
73+
for y_, p_ in zip(y_test, p)[:5]:
74+
print("y_test:", y_)
75+
print("p: ", p_.argmax(axis=1))
76+
print()
8277

78+
print("categorical accuracy: ", categorical_accuracy(YY_test, p).eval())

train.py

+34-33
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@
22

33
"order integer sequences of length given by n_steps"
44

5-
import pickle
65
import numpy as np
7-
8-
from keras.models import Model
96
from keras.layers import LSTM, Input
7+
from keras.models import Model
108
from keras.utils.np_utils import to_categorical
119

1210
from PointerLSTM import PointerLSTM
@@ -15,26 +13,25 @@
1513

1614
n_steps = 5
1715

18-
x_file = 'data/x_{}.csv'.format( n_steps )
19-
y_file = 'data/y_{}.csv'.format( n_steps )
16+
x_file = 'data/x_{}.csv'.format(n_steps)
17+
y_file = 'data/y_{}.csv'.format(n_steps)
2018

2119
split_at = 9000
2220
batch_size = 100
2321

2422
hidden_size = 64
25-
weights_file = 'model_weights/model_weights_{}_steps_{}.hdf5'.format( n_steps, hidden_size )
26-
23+
weights_file = 'model_weights/model_weights_{}_steps_{}.hdf5'.format(n_steps, hidden_size)
2724

2825
#
2926

30-
x = np.loadtxt( x_file, delimiter = ',', dtype = int )
31-
y = np.loadtxt( y_file, delimiter = ',', dtype = int )
27+
x = np.loadtxt(x_file, delimiter=',', dtype=int)
28+
y = np.loadtxt(y_file, delimiter=',', dtype=int)
3229

33-
x = np.expand_dims( x, axis = 2 )
30+
x = np.expand_dims(x, axis=2)
3431

3532
YY = []
3633
for y_ in y:
37-
YY.append(to_categorical(y_))
34+
YY.append(to_categorical(y_))
3835
YY = np.asarray(YY)
3936

4037
x_train = x[:split_at]
@@ -44,42 +41,46 @@
4441
YY_train = YY[:split_at]
4542
YY_test = YY[split_at:]
4643

47-
assert( n_steps == x.shape[1] )
44+
assert (n_steps == x.shape[1])
4845

4946
#
5047

51-
print( "building model..." )
52-
main_input = Input( shape=( n_steps, 1 ), name='main_input' )
48+
print("building model...")
49+
main_input = Input(shape=(n_steps, 1), name='main_input')
5350

54-
encoder = LSTM(output_dim = hidden_size, return_sequences = True, name="encoder")(main_input)
55-
decoder = PointerLSTM(hidden_size, output_dim=hidden_size, name="decoder")(encoder)
51+
encoder = LSTM(units=hidden_size, return_sequences=True, name="encoder")(main_input)
52+
print(encoder)
53+
decoder = PointerLSTM(hidden_size, units=hidden_size, name="decoder")(encoder)
5654

57-
model = Model( input=main_input, output=decoder )
55+
model = Model(inputs=main_input, outputs=decoder)
5856

59-
print( "loading weights from {}...".format( weights_file ))
57+
print("loading weights from {}...".format(weights_file))
6058
try:
61-
model.load_weights( weights_file )
59+
model.load_weights(weights_file)
6260
except IOError:
63-
print( "no weights file, starting anew." )
61+
print("no weights file, starting anew.")
6462

6563
model.compile(optimizer='rmsprop',
66-
loss='categorical_crossentropy',
67-
metrics=['accuracy'])
68-
69-
print( 'training and saving model weights each epoch...' )
64+
loss='categorical_crossentropy',
65+
metrics=['accuracy'])
66+
67+
print('training and saving model weights each epoch...')
68+
69+
validation_data = (x_test, YY_test)
7070

71-
validation_data = ( x_test, YY_test )
71+
epoch_counter = 0
7272

7373
while True:
7474

75-
history = model.fit( x_train, YY_train, nb_epoch = 1, batch_size = batch_size,
76-
validation_data = validation_data )
75+
history = model.fit(x_train, YY_train, epochs=1, batch_size=batch_size,
76+
validation_data=validation_data)
7777

78-
p = model.predict( x_test )
78+
p = model.predict(x_test)
7979

80-
for y_, p_ in zip( y_test, p )[:5]:
81-
print "y_test:", y_
82-
print "p: ", p_.argmax( axis = 1 )
83-
print
80+
for y_, p_ in list(zip(y_test, p))[:5]:
81+
print("epoch_counter: ", epoch_counter)
82+
print("y_test:", y_)
83+
print("p: ", p_.argmax(axis=1))
84+
print()
8485

85-
model.save_weights( weights_file )
86+
# model.save(weights_file)

0 commit comments

Comments
 (0)