Skip to content

Instantly share code, notes, and snippets.

@BenMacKenzie
Created March 24, 2017 14:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BenMacKenzie/8bf7b766a949d6a284ba62aaa36904b1 to your computer and use it in GitHub Desktop.
Save BenMacKenzie/8bf7b766a949d6a284ba62aaa36904b1 to your computer and use it in GitHub Desktop.
predict whether two numbers in sequence are same or not using stateful LSTM with steps = 1 batch size =1
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.
# use a stateful LSTM
# this requires that i train with a batch size of 1 and a sequence size of 1
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=(1, 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=1, shuffle=False)
print model.predict(np.asarray([[[1]]]))
print model.predict(np.asarray([[[0]]]))
print model.predict(np.asarray([[[0]]]))
print model.predict(np.asarray([[[1]]]))
print model.predict(np.asarray([[[1]]]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment