def get_last_hidden_state(self, sentence, init_hidden_state=None):
assert isinstance(sentence, Sentence)
with tf.variable_scope(self.scope, reuse=self.used):
J = sentence.shape[-1]
Ax = tf.nn.embedding_lookup(self.emb_mat, sentence.x) # [N, C, J, e]
F = reduce(mul, sentence.shape[:-1], 1)
init_hidden_state = init_hidden_state or self.cell.zero_state(F, tf.float32)
Ax_flat = tf.reshape(Ax, [F, J, self.input_size])
x_len_flat = tf.reshape(sentence.x_len, [F])
# Ax_flat_split = [tf.squeeze(x_flat_each, [1]) for x_flat_each in tf.split(1, J, Ax_flat)]
o_flat, h_flat = rnn.dynamic_rnn(self.cell, Ax_flat, x_len_flat, initial_state=init_hidden_state)
self.used = True
return h_flat
评论列表
文章目录