In [1]:
from keras.models import model_from_yaml
import numpy as np
In [120]:
from vectorizer import Vectorizer
py="\n\nfrom keras.models import Sequential\nfrom keras.layers.recurrent import LSTM, JZS1, GRU\n" * 20
In [121]:
vec = Vectorizer(py, cutoff=60)
print(dict(enumerate(vec.dictionary)))
In [123]:
window_len = 40
matrix = vec.to_matrix(py)
trainX = []
trainy = []
for i in range(len(matrix)-window_len-1):
trainX.append(matrix[i:i+window_len])
trainy.append(matrix[i+window_len])
trainX = np.array(trainX,dtype="float32")
trainy = np.array(trainy,dtype="float32")
print(trainX.shape)
print(trainy.shape)
In [141]:
from keras.models import Sequential
from keras.optimizers import Adadelta, SGD, Adagrad, RMSprop
from keras.layers.core import Dense, TimeDistributedDense, Flatten
from keras.layers.recurrent import LSTM, JZS1, GRU
lstmmodel = Sequential()
lstmmodel.add(LSTM(33,60, return_sequences=True))
lstmmodel.add(LSTM(60,60))
lstmmodel.add(Dense(60,33))
lstmmodel.compile(optimizer=SGD(lr=0.1,momentum=0.8),loss='mse',class_mode="categorical")
print("Initial loss")
print(lstmmodel.evaluate(trainX,trainy,show_accuracy=True))
print("Training")
lstmmodel.fit(trainX,trainy,nb_epoch = 100, show_accuracy=True,verbose=1)
print("New loss")
print(lstmmodel.evaluate(trainX,trainy,show_accuracy=True))
In [145]:
print(lstmmodel.evaluate(trainX,trainy,show_accuracy=True))
In [146]:
lstmyaml=lstmmodel.to_yaml()
print(lstmyaml)
In [152]:
lstmcopy = model_from_yaml(lstmyaml)
print(lstmcopy.evaluate(trainX,trainy,show_accuracy=True))
lstmweights=lstmmodel.get_weights()
lstmcopy.set_weights(lstmweights)
print("Original:", lstmmodel.evaluate(trainX,trainy,show_accuracy=True))
print("Copy:",lstmcopy.evaluate(trainX,trainy,show_accuracy=True))
In [153]:
jzs1model = Sequential()
jzs1model.add(JZS1(33,60, return_sequences=True))
jzs1model.add(JZS1(60,60))
jzs1model.add(Dense(60,33))
jzs1model.compile(optimizer=SGD(lr=0.1,momentum=0.8),loss='mse',class_mode="categorical")
In [154]:
print("Initial loss")
print(jzs1model.evaluate(trainX,trainy,show_accuracy=True))
print("Training")
import time
start=time.time()
jzs1model.fit(trainX,trainy,nb_epoch = 5, show_accuracy=True,verbose=1)
print("Turning verbose off and training 100 epochs, this will take approx.", 20*(time.time()-start),"seconds")
from sys import stdout
stdout.flush()
jzs1model.fit(trainX,trainy,nb_epoch = 100, show_accuracy=True,verbose=0)
print("New loss")
print(jzs1model.evaluate(trainX,trainy,show_accuracy=True))
In [157]:
jzs1yaml=jzs1model.to_yaml()
print(jzs1yaml)
In [158]:
jzs1copy = model_from_yaml(jzs1yaml)
print("Untrained:",jzs1copy.evaluate(trainX,trainy,show_accuracy=True))
jzs1weights = jzs1model.get_weights()
jzs1copy.set_weights(jzs1weights)
print("Original:", jzs1model.evaluate(trainX,trainy,show_accuracy=True))
print("Copy:",jzs1copy.evaluate(trainX,trainy,show_accuracy=True))
In [ ]: