Last active
March 21, 2022 15:10
-
-
Save viksit/f6465e5a3852a8eb145d to your computer and use it in GitHub Desktop.
Simple Keras recurrent neural network skeleton for sequence-to-sequence mapping
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
__author__ = 'Conan' | |
import numpy as np | |
import random | |
from keras.models import Sequential | |
from keras.layers.recurrent import SimpleRNN | |
# Toy dictionary of 1000 indices to random length 10 vectors | |
toywords = {} | |
toywords[0] = np.zeros(10) | |
for i in range(1,1000): | |
toywords[i] = np.random.rand(1,10) | |
# toy sequences from above dictionary/list: | |
toyinput = [] | |
for i in range(100): | |
wordnum = np.random.randint(3,20) | |
toyinput.append(random.sample(xrange(1000), wordnum)) | |
# 100 sets of output vectors to correspond to those sequences | |
toyoutput = [] | |
for ti in toyinput: | |
toyoutput.append([]) | |
for t in ti: | |
toyoutput[-1].insert(-1,np.random.rand(1,5)) | |
X = np.zeros((100, 20, 10)) | |
for r in range(len(toyinput)): | |
for c in range(20): | |
try: | |
X[r,c,:] = toywords[toyinput[r][c]] | |
except: | |
X[r,c,:] = toywords[0] | |
Y = np.zeros((100, 20, 5)) | |
for r in range(len(toyoutput)): | |
for c in range(20): | |
try: | |
Y[r,c,:] = toyoutput[r][c] | |
except: | |
Y[r,c,:] = np.zeros(5) | |
# length 10 toy vectors | |
embedding_size = 10 | |
# Not sure what difference it makes | |
batch_size = 16 | |
# Hidden dimension 100 | |
hidden_size = 100 | |
# Maximum length ?? | |
maxlen = 200 | |
# Maximum features ?? | |
output_size = 5 | |
# Sequential | |
model = Sequential() | |
model.add(SimpleRNN(10, 5, | |
init='glorot_uniform', inner_init='orthogonal', activation='sigmoid', weights=None, | |
truncate_gradient=-1, return_sequences=True)) | |
model.compile(loss='binary_crossentropy', optimizer='adam') | |
for e in range(100): | |
results = model.fit(X, Y, batch_size=batch_size, nb_epoch=1, validation_split=0.1, show_accuracy=True, verbose=1) | |
preds = model.predict_proba(X, batch_size=batch_size) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment