Skip to content

Instantly share code, notes, and snippets.

@schlerp
Created September 5, 2017 06:20
Show Gist options
  • Save schlerp/003c4ec1469e4d5f4da686f832dfabe2 to your computer and use it in GitHub Desktop.
Save schlerp/003c4ec1469e4d5f4da686f832dfabe2 to your computer and use it in GitHub Desktop.
from __future__ import absolute_import, division, print_function
import os
import pickle
from six.moves import urllib
import tflearn
from tflearn.data_utils import *
path = "data/sherlock_edited.txt"
char_idx_file = 'char_idx.pickle'
#if not os.path.isfile(path):
#urllib.request.urlretrieve("https://raw.githubusercontent.com/tflearn/tflearn.github.io/master/resources/shakespeare_input.txt", path)
maxlen = 25
char_idx = None
if os.path.isfile(char_idx_file):
print('Loading previous char_idx')
char_idx = pickle.load(open(char_idx_file, 'rb'))
X, Y, char_idx = \
textfile_to_semi_redundant_sequences(path, seq_maxlen=maxlen, redun_step=3)
pickle.dump(char_idx, open(char_idx_file,'wb'))
# input
i = tflearn.input_data([None, maxlen, len(char_idx)])
# path a
a = tflearn.conv_1d(i, 16, 10)
a = tflearn.lstm(a, 16, return_seq=True)
a = tflearn.dropout(a, 0.5)
a = tflearn.gru(a, 32)
a = tflearn.dropout(a, 0.5)
# path b
b = tflearn.lstm(i, 48, return_seq=True)
b = tflearn.dropout(b, 0.5)
b = tflearn.gru(b, 64)
b = tflearn.dropout(b, 0.5)
# path c
c = tflearn.lstm(i, 96, return_seq=True)
c = tflearn.dropout(c, 0.5)
c = tflearn.gru(c, 128)
c = tflearn.dropout(c, 0.5)
# merge
m = tflearn.merge_outputs([a, b, c])
# fully connected
fc = tflearn.highway(m, 224, activation='relu')
fc = tflearn.dropout(fc, 0.5)
fc = tflearn.fully_connected(fc, len(char_idx), activation='softmax')
fc = tflearn.regression(fc, optimizer='adam', loss='categorical_crossentropy',
learning_rate=0.002)
# final model
m = tflearn.SequenceGenerator(fc, dictionary=char_idx,
seq_maxlen=maxlen,
clip_gradients=5.0,
checkpoint_path='model_aiw',
tensorboard_verbose=1)
for i in range(10):
seed = random_sequence_from_textfile(path, maxlen)
m.fit(X, Y, validation_set=0.1, batch_size=128,
n_epoch=1, run_id='aiw')
print("-- TESTING...")
print("-- Test with temperature of 1.0 --")
print(m.generate(600, temperature=1.0, seq_seed=seed))
print("-- Test with temperature of 0.5 --")
print(m.generate(600, temperature=0.5, seq_seed=seed))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment