def __call__(self, inputs, name, training=False):
"""
Runs the bidirectional LSTM, produces outputs and saves both forward and backward states as well as gradients.
:param inputs: The inputs should be a list of shape [sequence_length, batch_size, 64]
:param name: Name to give to the tensorflow op
:param training: Flag that indicates if this is a training or evaluation stage
:return: Returns the LSTM outputs, as well as the forward and backward hidden states.
"""
with tf.name_scope('bid-lstm' + name), tf.variable_scope('bid-lstm', reuse=self.reuse):
with tf.variable_scope("encoder"):
fw_lstm_cells_encoder = [rnn.LSTMCell(num_units=self.layer_sizes[i], activation=tf.nn.tanh)
for i in range(len(self.layer_sizes))]
bw_lstm_cells_encoder = [rnn.LSTMCell(num_units=self.layer_sizes[i], activation=tf.nn.tanh)
for i in range(len(self.layer_sizes))]
outputs, output_state_fw, output_state_bw = rnn.stack_bidirectional_rnn(
fw_lstm_cells_encoder,
bw_lstm_cells_encoder,
inputs,
dtype=tf.float32
)
print("out shape", tf.stack(outputs, axis=0).get_shape().as_list())
with tf.variable_scope("decoder"):
fw_lstm_cells_decoder = [rnn.LSTMCell(num_units=self.layer_sizes[i], activation=tf.nn.tanh)
for i in range(len(self.layer_sizes))]
bw_lstm_cells_decoder = [rnn.LSTMCell(num_units=self.layer_sizes[i], activation=tf.nn.tanh)
for i in range(len(self.layer_sizes))]
outputs, output_state_fw, output_state_bw = rnn.stack_bidirectional_rnn(
fw_lstm_cells_decoder,
bw_lstm_cells_decoder,
outputs,
dtype=tf.float32
)
self.reuse = True
self.variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope='bid-lstm')
return outputs, output_state_fw, output_state_bw
one_shot_learning_network.py 文件源码
python
阅读 22
收藏 0
点赞 0
评论 0
评论列表
文章目录