我正在尝试使用Tensorflow中的seq2seq.dynamic_decode来构建序列到序列模型。我已经完成了编码器部分。我对解码器感到困惑,因为decoder_outputs
似乎返回[batch_size x sequence_length x embedding_size]
但我需要实际的单词索引来正确计算我的损失[batch_size x sequence_length]
。我想知道我的一个形状输入是否不正确,或者我只是忘记了什么。
解码器和编码器单元rnn.BasicLSTMCell()
。
# Variables
cell_size = 100
decoder_vocabulary_size = 7
batch_size = 2
decoder_max_sentence_len = 7
# Part of the encoder
_, encoder_state = tf.nn.dynamic_rnn(
cell=encoder_cell,
inputs=features,
sequence_length=encoder_sequence_lengths,
dtype=tf.float32)
# ---- END Encoder ---- #
# ---- Decoder ---- #
# decoder_sequence_lengths = _sequence_length(features)
embedding = tf.get_variable(
"decoder_embedding", [decoder_vocabulary_size, cell_size])
helper = seq2seq.GreedyEmbeddingHelper(
embedding=embedding,
start_tokens=tf.tile([GO_SYMBOL], [batch_size]),
end_token=END_SYMBOL)
decoder = seq2seq.BasicDecoder(
cell=decoder_cell,
helper=helper,
initial_state=encoder_state)
decoder_outputs, _ = seq2seq.dynamic_decode(
decoder=decoder,
output_time_major=False,
impute_finished=True,
maximum_iterations=self.decoder_max_sentence_len)
# I need labels (decoder_outputs) to be indices
losses = nn_ops.sparse_softmax_cross_entropy_with_logits(
labels=labels, logits=logits)
loss = tf.reduce_mean(losses)
我发现解决方案是:
from tensorflow.python.layers.core import Dense
decoder = seq2seq.BasicDecoder(
cell=decoder_cell,
helper=helper,
initial_state=encoder_state,
output_layer=Dense(decoder_vocabulary_size))
...
logits = decoder_outputs[0]
您必须指定一个密集图层,以从cell_size投影到词汇大小。