Created
March 24, 2017 14:28
-
-
Save BenMacKenzie/fbc3aec9aab5232933a92142a5c44b99 to your computer and use it in GitHub Desktop.
train model to count number of 1's in a sequence of 20 time steps, based on https://gist.github.com/monikkinom/e97d518fe02a79177b081c028a83ec1c
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 | |
from random import shuffle | |
#based on: https://gist.github.com/monikkinom/e97d518fe02a79177b081c028a83ec1c | |
#train model to count number of 1's in a sequence of 20 time steps | |
NUM_EXAMPLES = 10000 | |
train_input = ['{0:020b}'.format(i) for i in range(2**20)] | |
shuffle(train_input) | |
train_input = [map(int,i) for i in train_input] | |
ti = [] | |
for i in train_input: | |
temp_list = [] | |
for j in i: | |
temp_list.append([j]) | |
ti.append(np.array(temp_list)) | |
train_input = ti | |
train_output = [] | |
for i in train_input: | |
count = 0 | |
for j in i: | |
if j[0] == 1: | |
count+=1 | |
temp_list = ([0]*21) | |
temp_list[count]=1 | |
train_output.append(temp_list) | |
test_input = train_input[NUM_EXAMPLES:] | |
test_output = train_output[NUM_EXAMPLES:] | |
train_input = train_input[:NUM_EXAMPLES] | |
train_output = train_output[:NUM_EXAMPLES] | |
model = Sequential() | |
model.add(LSTM(24, input_shape=(20,1))) | |
model.add(Dense(21)) | |
model.add(Activation('softmax')) | |
model.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy']) | |
model.fit(np.asarray(train_input), np.asarray(train_output), epochs=1000, batch_size=128) | |
#loss_and_metrics = model.evaluate(test_input, test_output) | |
print model.predict(np.asarray([[[1],[0],[0],[1],[1],[0],[1],[1],[1],[0],[1],[0],[0],[1],[1],[0],[1],[1],[1],[0]]])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment