-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLayers.py
More file actions
124 lines (101 loc) · 5.8 KB
/
Layers.py
File metadata and controls
124 lines (101 loc) · 5.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# -*- coding: utf-8 -*-
#
import tensorflow as tf
#creating the encoder using MultiRNNCell
def encoding_layer(rnn_inputs, rnn_size, num_layers, keep_prob,
source_vocab_size,
encoding_embedding_size):
# :return: tuple (RNN output, RNN state)
embed = tf.contrib.layers.embed_sequence(rnn_inputs,
vocab_size=source_vocab_size,
embed_dim=encoding_embedding_size)
# Create a RNN cell composed sequentially of a number of RNNCells.
stacked_cells = tf.contrib.rnn.MultiRNNCell([tf.contrib.rnn.DropoutWrapper(tf.contrib.rnn.LSTMCell(rnn_size), keep_prob) for _ in range(num_layers)])
# outputs are the outputs fro each time step of the rnn
# state is the context vector that will be feed to the decoder
outputs, state = tf.nn.dynamic_rnn(stacked_cells,
embed,
dtype=tf.float32)
return outputs, state
#creating the decoder training
def decoding_layer_train(encoder_state, dec_cell, dec_embed_input,
target_sequence_length, max_summary_length,
output_layer, keep_prob):
# Create a training process in decoding layer
# :return: BasicDecoderOutput containing training logits and sample_id
dec_cell = tf.contrib.rnn.DropoutWrapper(dec_cell,
output_keep_prob=keep_prob)
# for only input layer
helper = tf.contrib.seq2seq.TrainingHelper(dec_embed_input,
target_sequence_length)
#creating the basic decoder
decoder = tf.contrib.seq2seq.BasicDecoder(dec_cell,
helper,
encoder_state,
output_layer)
# unrolling the decoder layer
outputs, _, _ = tf.contrib.seq2seq.dynamic_decode(decoder,
impute_finished=True,
maximum_iterations=max_summary_length)
return outputs
#creating the decoder inferance
def decoding_layer_infer(encoder_state, dec_cell, dec_embeddings, start_of_sequence_id,
end_of_sequence_id, max_target_sequence_length,
vocab_size, output_layer, batch_size, keep_prob):
# Create a inference process in decoding layer
# :return: BasicDecoderOutput containing inference logits and sample_id
dec_cell = tf.contrib.rnn.DropoutWrapper(dec_cell,
output_keep_prob=keep_prob)
helper = tf.contrib.seq2seq.GreedyEmbeddingHelper(dec_embeddings,
tf.fill([batch_size], start_of_sequence_id),
end_of_sequence_id)
#creating the basic decoder
decoder = tf.contrib.seq2seq.BasicDecoder(dec_cell,
helper,
encoder_state,
output_layer)
# unrolling the decoder layer
outputs, _, _ = tf.contrib.seq2seq.dynamic_decode(decoder,
impute_finished=True,
maximum_iterations=max_target_sequence_length)
return outputs
def decoding_layer(dec_input, encoder_state,
target_sequence_length, max_target_sequence_length,
rnn_size,
num_layers, target_vocab_to_int, target_vocab_size,
batch_size, keep_prob, decoding_embedding_size):
# Create decoding layer
# :return: Tuple of (Training BasicDecoderOutput, Inference BasicDecoderOutput)
target_vocab_size = len(target_vocab_to_int)
#creating the embedding matrix
dec_embeddings = tf.Variable(tf.random_uniform([target_vocab_size + 3, decoding_embedding_size]))
#embedding the decoder training inputs
dec_embed_input = tf.nn.embedding_lookup(dec_embeddings, dec_input)
#making the layers of LSTM cells
cells = tf.contrib.rnn.MultiRNNCell([tf.contrib.rnn.LSTMCell(rnn_size) for _ in range(num_layers)])
#compiling the decoding layer
with tf.variable_scope("decode"):
#output for layer for projection
output_layer = tf.layers.Dense(target_vocab_size)
#getting training output
train_output = decoding_layer_train(encoder_state,
cells,
dec_embed_input,
target_sequence_length,
max_target_sequence_length,
output_layer,
keep_prob)
#reusing the same decode scope
with tf.variable_scope("decode", reuse=True):
#getting inferance output
infer_output = decoding_layer_infer(encoder_state,
cells,
dec_embeddings,
target_vocab_to_int['<GO>'],
target_vocab_to_int['<EOS>'],
max_target_sequence_length,
target_vocab_size,
output_layer,
batch_size,
keep_prob)
return (train_output, infer_output)