-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
train_model.py is the training script in tensorflow
- Loading branch information
1 parent
2f57697
commit 17c0249
Showing
5 changed files
with
55 additions
and
1 deletion.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# training the built model_inputs | ||
|
||
from build_model import * | ||
# Set the Hyperparameters | ||
epochs = 100 | ||
batch_size = 64 | ||
rnn_size = 256 | ||
num_layers = 2 | ||
learning_rate = 0.005 | ||
keep_probability = 0.75 | ||
|
||
# Build the graph | ||
train_graph = tf.Graph() | ||
# Set the graph to default to ensure that it is ready for training | ||
with train_graph.as_default(): | ||
|
||
# Load the model inputs | ||
input_data, targets, lr, keep_prob, summary_length, max_summary_length, text_length = model_inputs() | ||
|
||
# Create the training and inference logits | ||
training_logits, inference_logits = seq2seq_model(tf.reverse(input_data, [-1]), | ||
targets, | ||
keep_prob, | ||
text_length, | ||
summary_length, | ||
max_summary_length, | ||
len(vocab_to_int)+1, | ||
rnn_size, | ||
num_layers, | ||
vocab_to_int, | ||
batch_size) | ||
|
||
# Create tensors for the training logits and inference logits | ||
training_logits = tf.identity(training_logits.rnn_output, 'logits') | ||
inference_logits = tf.identity(inference_logits.sample_id, name='predictions') | ||
|
||
# Create the weights for sequence_loss | ||
masks = tf.sequence_mask(summary_length, max_summary_length, dtype=tf.float32, name='masks') | ||
|
||
with tf.name_scope("optimization"): | ||
# Loss function | ||
cost = tf.contrib.seq2seq.sequence_loss( | ||
training_logits, | ||
targets, | ||
masks) | ||
|
||
# Optimizer | ||
optimizer = tf.train.AdamOptimizer(learning_rate) | ||
|
||
# Gradient Clipping | ||
gradients = optimizer.compute_gradients(cost) | ||
capped_gradients = [(tf.clip_by_value(grad, -5., 5.), var) for grad, var in gradients if grad is not None] | ||
train_op = optimizer.apply_gradients(capped_gradients) | ||
print("Graph is built.") |