Building the graph

The graph is built using the high-level seq2seq_model function. Let's look at the code for building the graph and optimizer:

train_graph = tf.Graph()
with train_graph.as_default():

data_inp, tgts, lrt, dprobs, len_summ, max_len_summ, len_txt = model_inputs()

tr_op, inf_op = seq2seq_model(tf.reverse(data_inp, [-1]),
tgts,
dprobs,
len_txt,
len_summ,
max_len_summ,
len(word2int)+1,
rnn_len,
n_layers,
word2int,
batch_size)

tr_op = tf.identity(tr_op.rnn_output, 'tr_op')
inf_op = tf.identity(inf_op.sample_id, name='predictions')

seq_masks = tf.sequence_mask(len_summ, max_len_summ, dtype=tf.float32, name='masks')

with tf.name_scope("optimizer"):
tr_cost = sequence_loss(tr_op,tgts,seq_masks)
optzr = tf.train.AdamOptimizer(lrt)
grds = optzr.compute_gradients(tr_cost)
capped_grds = [(tf.clip_by_value(grd, -5., 5.), var) for grd, var in grds
if grd is not None]
train_op = optzr.apply_gradients(capped_grds)
tf.summary.scalar("cost", tr_cost)
print("Graph created.")

We utilized AdamOptimizer to minimize the cost, computed with the training logits and the target summary words. Note that because we are using RNNs, we cap the gradients, to avoid the problem of exploding gradients.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset