This file contains hidden or 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
gen_length = 500 | |
""" | |
The prime word is used as the start word for the text generation. | |
To generate different text try different prime words like: | |
'marge_simpson' | |
'bart_simpson' | |
'lisa_simpson' |
This file contains hidden or 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
def pick_word(probabilities, int_to_vocab): | |
word_id = np.argmax(probabilities) | |
word_string = int_to_vocab[word_id] | |
return word_string |
This file contains hidden or 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
def get_tensors(loaded_graph): | |
input_tensor = loaded_graph.get_tensor_by_name('input:0') | |
initial_state_tensor = loaded_graph.get_tensor_by_name('initial_state:0') | |
final_state_tensor = loaded_graph.get_tensor_by_name('final_state:0') | |
probs_tensor = loaded_graph.get_tensor_by_name('probs:0') | |
return input_tensor, initial_state_tensor, final_state_tensor, probs_tensor |
This file contains hidden or 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
batches = get_batches(int_text, batch_size, seq_length) | |
with tf.Session(graph=train_graph) as sess: | |
sess.run(tf.global_variables_initializer()) | |
for epoch_i in range(num_epochs): | |
state = sess.run(initial_state, {input_text: batches[0][0]}) | |
for batch_i, (x, y) in enumerate(batches): | |
feed = { |
This file contains hidden or 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
train_graph = tf.Graph() | |
with train_graph.as_default(): | |
vocab_size = len(int_to_vocab) | |
input_text, targets, lr = get_inputs() | |
input_data_shape = tf.shape(input_text) | |
cell, initial_state = get_init_cell(input_data_shape[0], rnn_size) | |
logits, final_state = build_nn(cell, rnn_size, input_text, vocab_size, embed_dim) | |
# Probabilities for generating words | |
probs = tf.nn.softmax(logits, name='probs') |
This file contains hidden or 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
# Number of Epochs | |
num_epochs = 50 | |
# Batch Size | |
batch_size = 32 | |
# RNN Size | |
rnn_size = 512 | |
# Embedding Dimension Size | |
embed_dim = 256 | |
# Sequence Length | |
seq_length = 16 |
This file contains hidden or 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
def get_batches(int_text, batch_size, seq_length): | |
n_batches = len(int_text) // (batch_size * seq_length) | |
words = np.asarray(int_text[:n_batches*(batch_size * seq_length)]) | |
batches = np.zeros(shape=(n_batches, 2, batch_size, seq_length)) | |
input_sequences = words.reshape(-1, seq_length) | |
target_sequences = np.roll(words, -1) | |
target_sequences = target_sequences.reshape(-1, seq_length) | |
This file contains hidden or 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
def build_nn(cell, rnn_size, input_data, vocab_size, embed_dim): | |
embeddings = get_embed(input_data, vocab_size, embed_dim) | |
inputs, final_state = build_rnn(cell, embeddings) | |
logits = tf.contrib.layers.fully_connected(inputs=inputs, num_outputs=vocab_size, activation_fn=None) | |
return logits, final_state |
This file contains hidden or 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
def build_rnn(cell, inputs): | |
outputs, state = tf.nn.dynamic_rnn(cell, inputs, dtype=tf.float32) | |
final_state = tf.identity(state, name="final_state") | |
return outputs, final_state |
This file contains hidden or 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
def get_embed(input_data, vocab_size, embed_dim): | |
embedding = tf.Variable(tf.random_uniform((vocab_size, embed_dim), -1, 1)) | |
embed = tf.nn.embedding_lookup(embedding, input_data) | |
return embed |
NewerOlder