`tf.contrib.seq2seq.BeamSearchDecoder的输出中predict_ids的形状

时间:2018-05-24 14:50:58

标签: tensorflow seq2seq

outputs的{​​{1}}内容的形状是什么。我知道它是tf.contrib.seq2seq.BeamSearchDecoder的一个实例,但class BeamSearchDecoderOutput(scores, predicted_ids, parent_ids)scorespredicted_ids的形状是什么?

1 个答案:

答案 0 :(得分:1)

我写了一些关注玩具代码来自己探索一下。

tgt_vocab_size = 20
embedding_decoder = tf.one_hot(list(range(0, tgt_vocab_size)), tgt_vocab_size)
batch_size = 2
start_tokens = tf.fill([batch_size], 0)
end_token = 1
beam_width = 3
num_units=18

decoder_cell = tf.nn.rnn_cell.BasicLSTMCell(num_units)
encoder_outputs = decoder_cell.zero_state(batch_size, dtype=tf.float32)
tiled_encoder_outputs = tf.contrib.seq2seq.tile_batch(encoder_outputs, multiplier=beam_width)

my_decoder = tf.contrib.seq2seq.BeamSearchDecoder(cell=decoder_cell,
                                                  embedding=embedding_decoder,
                                                  start_tokens=start_tokens,
                                                  end_token=end_token,
                                                  initial_state=tiled_encoder_outputs,
                                                  beam_width=beam_width)

 # dynamic decoding
outputs, final_context_state, _ = tf.contrib.seq2seq.dynamic_decode(my_decoder,
                                                                   maximum_iterations=4,
                                                                   output_time_major=True)
final_predicted_ids = outputs.predicted_ids
scores = outputs.beam_search_decoder_output.scores
predicted_ids = outputs.beam_search_decoder_output.predicted_ids
parent_ids = outputs.beam_search_decoder_output.parent_ids

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    final_predicted_ids_vals = sess.run(final_predicted_ids)
    print("final_predicted_ids shape:")
    print(final_predicted_ids_vals.shape)
    print("final_predicted_ids_vals: \n%s" %final_predicted_ids_vals)
    print("scores shape:")
    print(sess.run(scores).shape)
    print("scores values: \n %s" % sess.run(scores))
    print("predicted_ids shape: ")
    print(sess.run(predicted_ids).shape)
    print("predicted_ids values: \n %s" % sess.run(predicted_ids))
    print("parent_ids shape:")
    print(sess.run(parent_ids).shape)
    print("parent_ids values: \n %s" % sess.run(parent_ids))

印刷品如下:

final_predicted_ids shape:
(4, 2, 3)
final_predicted_ids_vals: 
[[[ 1  8  8]
  [ 1  8  8]]

 [[ 1 13 13]
  [ 1 13 13]]

 [[ 1 13 13]
  [ 1 13 13]]

 [[ 1 13  2]
  [ 1 13  2]]]
scores shape:
(4, 2, 3)
scores values: 
 [[[ -2.8376358  -2.843168   -2.8478816]
  [ -2.8376358  -2.843168   -2.8478816]]

 [[ -2.8478816  -5.655898   -5.6810265]
  [ -2.8478816  -5.655898   -5.6810265]]

 [[ -2.8478816  -8.478384   -8.495466 ]
  [ -2.8478816  -8.478384   -8.495466 ]]

 [[ -2.8478816 -11.292251  -11.307263 ]
  [ -2.8478816 -11.292251  -11.307263 ]]]
predicted_ids shape: 
(4, 2, 3)
predicted_ids values: 
 [[[ 8 13  1]
  [ 8 13  1]]

 [[ 1 13 13]
  [ 1 13 13]]

 [[ 1 13 12]
  [ 1 13 12]]

 [[ 1 13  2]
  [ 1 13  2]]]
parent_ids shape:
(4, 2, 3)
parent_ids values: 
 [[[0 0 0]
  [0 0 0]]

 [[2 0 1]
  [2 0 1]]

 [[0 1 1]
  [0 1 1]]

 [[0 1 1]
  [0 1 1]]]

outputs的{​​{1}}实际上是tf.contrib.seq2seq.dynamic_decode(BeamSearchDecoder)的一个实例,其中包含:

class FinalBeamSearchDecoderOutput:所有解码完成后,波束搜索返回的最终输出。形状的张力 predicted_ids (如果[batch_size, num_steps, beam_width][num_steps, batch_size, beam_width],则为output_time_major)。梁从最好到最差排列。

True:BeamSearchDecoderOutput的一个实例,用于描述波束搜索的状态。

因此,如果beam_search_decoder_output需要确保[beam_width, batch_size, num_steps]transpose([2, 0, 1])的最终预测/翻译形状为tf.transpose(final_predicted_ids)