def map(self, is_train, x, mask=None):
x = tf.transpose(x, [1, 0, 2])
if self.bidirectional:
with tf.variable_scope("forward"):
fw = self._apply_transposed(is_train, x)[0]
with tf.variable_scope("backward"):
bw = self._apply_transposed(is_train, tf.reverse_sequence(x, mask, 0, 1))[0]
bw = tf.reverse_sequence(bw, mask, 0, 1)
out = tf.concat([fw, bw], axis=2)
else:
out = self._apply_transposed(is_train, x)[0]
out = tf.transpose(out, [1, 0, 2])
if mask is not None:
out *= tf.expand_dims(tf.cast(tf.sequence_mask(mask, tf.shape(out)[1]), tf.float32), 2)
return out
评论列表
文章目录