Created
March 24, 2017 14:40
-
-
Save BenMacKenzie/007766ded18a8854ea542c2115d907ae to your computer and use it in GitHub Desktop.
same as seq2a but with batch size = 5. DOES NOT WORK. State will not get copied correctly.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from keras.models import Sequential | |
from keras.layers import LSTM, Dense, Activation | |
import numpy as np | |
# predict whether two numbers in sequence are same or not. | |
# same as previous seq2a, but try to batch training. | |
# | |
# this doesn't work. | |
train_input = [[[np.random.randint(0,2)]] for r in xrange(1000)] | |
train_output = [[0]] | |
for i in range(1, len(train_input)): | |
if(train_input[i][0][0]==train_input[i-1][0][0]): | |
train_output.append([1]) | |
else: | |
train_output.append([0]) | |
#train has to be processed as a whole | |
print "test and training data loaded" | |
model = Sequential() | |
model.add(LSTM(10, return_sequences=False, batch_input_shape=(5, 1,1), stateful=True)) | |
model.add(Dense(1)) | |
model.add(Activation('sigmoid')) | |
model.compile(loss='binary_crossentropy', optimizer='sgd', metrics=['accuracy']) | |
model.fit(np.asarray(train_input), np.asarray(train_output), epochs=100, batch_size=5, shuffle=False) | |
print model.predict(np.asarray([[1]] [[1]] [[0]] [[0]] [[0]])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment