Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
tflearn/examples/nlp/lstm_generator_cityname.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
45 lines (37 sloc)
1.7 KB
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 __future__ import absolute_import, division, print_function | |
import os | |
from six import moves | |
import ssl | |
import tflearn | |
from tflearn.data_utils import * | |
path = "US_Cities.txt" | |
if not os.path.isfile(path): | |
context = ssl._create_unverified_context() | |
moves.urllib.request.urlretrieve("https://raw.githubusercontent.com/tflearn/tflearn.github.io/master/resources/US_Cities.txt", path, context=context) | |
maxlen = 20 | |
string_utf8 = open(path, "r").read().decode('utf-8') | |
X, Y, char_idx = \ | |
string_to_semi_redundant_sequences(string_utf8, seq_maxlen=maxlen, redun_step=3) | |
g = tflearn.input_data(shape=[None, maxlen, len(char_idx)]) | |
g = tflearn.lstm(g, 512, return_seq=True) | |
g = tflearn.dropout(g, 0.5) | |
g = tflearn.lstm(g, 512) | |
g = tflearn.dropout(g, 0.5) | |
g = tflearn.fully_connected(g, len(char_idx), activation='softmax') | |
g = tflearn.regression(g, optimizer='adam', loss='categorical_crossentropy', | |
learning_rate=0.001) | |
m = tflearn.SequenceGenerator(g, dictionary=char_idx, | |
seq_maxlen=maxlen, | |
clip_gradients=5.0, | |
checkpoint_path='model_us_cities') | |
for i in range(40): | |
seed = random_sequence_from_string(string_utf8, maxlen) | |
m.fit(X, Y, validation_set=0.1, batch_size=128, | |
n_epoch=1, run_id='us_cities') | |
print("-- TESTING...") | |
print("-- Test with temperature of 1.2 --") | |
print(m.generate(30, temperature=1.2, seq_seed=seed).encode('utf-8')) | |
print("-- Test with temperature of 1.0 --") | |
print(m.generate(30, temperature=1.0, seq_seed=seed).encode('utf-8')) | |
print("-- Test with temperature of 0.5 --") | |
print(m.generate(30, temperature=0.5, seq_seed=seed).encode('utf-8')) |