Created
March 24, 2017 14:42
-
-
Save BenMacKenzie/4f65757af033417d588d9729fd74de10 to your computer and use it in GitHub Desktop.
same as 2a but with 5 time steps
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, TimeDistributed, Dense, Activation | |
import numpy as np | |
# predict whether two numbers in sequence are same or not. | |
# the only difference between this an seq2a is that it has 5 time steps. | |
# this works, except for first number in sequence (should output 0)...effectively there is only a single training example for the model to learn this | |
# since step 1 from all batches other than the first are initialized with state from last step of previous batch. | |
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]==train_input[i-1]): | |
train_output.append(1) | |
else: | |
train_output.append(0) | |
train_input = np.reshape(train_input, (200, 5, 1)) | |
train_output = np.reshape(train_output, (200,5,1)) | |
print "test and training data loaded" | |
model = Sequential() | |
model.add(LSTM(10, return_sequences=True, batch_input_shape=(1, 5, 1), stateful=True)) | |
model.add(TimeDistributed(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=200, batch_size=1, shuffle=False) | |
print model.predict(np.asarray([1,0,0,1,1]).reshape(1,5,1)) | |
print model.predict(np.asarray([1,1,0,0,1]).reshape(1,5,1)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment