Last active
March 24, 2017 17:06
-
-
Save BenMacKenzie/4f0c2ed4ad267de6e1733c1a6fd93811 to your computer and use it in GitHub Desktop.
# counts number of 1's in a sequence of 10 steps, but outputs a runnint total at each step.
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 | |
from random import shuffle | |
# counts number of 1's in a sequence of 10 steps, but outputs a runnint total at each step. | |
NUM_EXAMPLES = 1000 | |
train_input = ['{0:010b}'.format(i) for i in range(2**10)] | |
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: | |
l1 = [] | |
count = 0 | |
for j in i: | |
if j[0] == 1: | |
count+=1 | |
l2 = ([0]*11) | |
l2[count]=1 | |
l1.append(l2) | |
train_output.append(l1) | |
test_input = train_input[NUM_EXAMPLES:] | |
test_output = train_output[NUM_EXAMPLES:] | |
train_input = train_input[:NUM_EXAMPLES] | |
train_output = train_output[:NUM_EXAMPLES] | |
print "test and training data loaded" | |
model = Sequential() | |
model.add(LSTM(14, return_sequences=True, input_shape=(10,1))) | |
model.add(TimeDistributed(Dense(11, activation='softmax'))) | |
model.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy']) | |
model.fit(np.asarray(train_input), np.asarray(train_output), epochs=100, 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]]])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment