Skip to content

Instantly share code, notes, and snippets.

View BenMacKenzie's full-sized avatar

Ben Mackenzie BenMacKenzie

  • Databricks
  • Quebec Canada
View GitHub Profile
@BenMacKenzie
BenMacKenzie / seq2a
Created March 24, 2017 14:37
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
@BenMacKenzie
BenMacKenzie / seq1a.py
Created March 24, 2017 14:28
train model to count number of 1's in a sequence of 20 time steps, based on https://gist.github.com/monikkinom/e97d518fe02a79177b081c028a83ec1c
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
@BenMacKenzie
BenMacKenzie / seq2b.py
Created March 24, 2017 14:40
same as seq2a but with batch size = 5. DOES NOT WORK. State will not get copied correctly.
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.
@BenMacKenzie
BenMacKenzie / seq2c.py
Created March 24, 2017 14:42
same as 2a but with 5 time steps
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.
@BenMacKenzie
BenMacKenzie / seq2d.py
Created March 24, 2017 14:44
same as 2c, but save model to file
from keras.models import Sequential
from keras.layers import LSTM, TimeDistributed, Dense, Activation
from keras.models import load_model
import numpy as np
# same as 2c except it saves model to file.
def train_model():
train_input = [np.random.randint(0,2) for r in xrange(1000)]
@BenMacKenzie
BenMacKenzie / seq3a
Created March 24, 2017 14:47
calculate difference between two numbers. train with a batch size 10, but deploy to a model that will process continuously
from keras.models import Sequential
from keras.layers import LSTM, TimeDistributed, Dense, Activation
from keras.models import load_model
import numpy as np
# predict (i.e., calculate, not predict based on a pattern) difference between consecutive numbers in sequence
# here i train using a batch size of 10, save the weights and then load into a model with batch size of 1 and num_steps = 1,
# i.e., something suitable for real-time continuous prediction.
@BenMacKenzie
BenMacKenzie / seq3b.py
Last active March 24, 2017 15:10
same as 3a, but numbers can range between 0 and 10
from keras.models import Sequential
from keras.layers import LSTM, TimeDistributed, Dense, Activation
from keras.models import load_model
import numpy as np
# same as 3a, but allow values between 0 and 10.
num_samples = 5000
num_steps = 10
max_distance = 10.0 #this needs to be 10.0 not 10...otherwise divide rounds to 0.
@BenMacKenzie
BenMacKenzie / seq1b.py
Last active March 24, 2017 17:06
# counts number of 1's in a sequence of 10 steps, but outputs a runnint total at each step.
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