Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Tensorflow:BeamSearchDecoder的故障-动态_解码_Python_Tensorflow_Rnn_Seq2seq - Fatal编程技术网

Python Tensorflow:BeamSearchDecoder的故障-动态_解码

Python Tensorflow:BeamSearchDecoder的故障-动态_解码,python,tensorflow,rnn,seq2seq,Python,Tensorflow,Rnn,Seq2seq,通过注意和波束搜索实现具有双向多LSTM层的seq2seq模型。 (只发布了必要的代码以保持简单) 编码器 译码器 我发现以下错误: ValueError: Shapes must be equal rank, but are 3 and 2 for 'decode_1/decoder/while/Select_4' (op: 'Select') with input shapes: [64,10], [64,10,256], [64,10,256]. 有没有人有过这样的经历,或者遇到过同样的

通过注意和波束搜索实现具有双向多LSTM层的seq2seq模型。 (只发布了必要的代码以保持简单)

编码器 译码器 我发现以下错误:

ValueError: Shapes must be equal rank, but are 3 and 2 for 'decode_1/decoder/while/Select_4' (op: 'Select') with input shapes: [64,10], [64,10,256], [64,10,256].
有没有人有过这样的经历,或者遇到过同样的问题?我真的很感谢你的建议

Tensorflow:1.6.0 批量大小=64
rnn_size=256

我认为您需要设置解码器初始状态=编码器状态

enc_output = tf.contrib.seq2seq.tile_batch(enc_output, multiplier=beam_width)
num_bi_layes = int(num_layers/2)
if num_bi_layes == 1:
    encoder_state = enc_state
else:
    encoder_state = []
    for layer_id in range(num_bi_layes):
        encoder_state.append(enc_state[0][layer_id]) #forward
        encoder_state.append(enc_state[1][layer_id]) #backward
        encoder_state = touple(encoder_state)


encoder_state = tf.contrib.seq2seq.tile_batch(encoder_state, multiplier=beam_width)

text_length = tf.contrib.seq2seq.tile_batch(text_length, multiplier=beam_width)

dec_cell = tf.nn.rnn_cell.MultiRNNCell([make_lstm(rnn_size, keep_prob) for _ in range(num_layers)])
dec_cell = decoder_cell(dec_cell, rnn_size, enc_output, text_length)

start_tokens = tf.tile(tf.constant([word2ind['<GO>']], dtype = tf.int32), [batch_size], name = 'start_tokens')

with tf.variable_scope("decode", reuse = True):
    decoder = tf.contrib.seq2seq.BeamSearchDecoder( cell=dec_cell,
                                                    embedding=embeddings,
                                                    start_tokens=start_tokens,
                                                    end_token=end_token,
                                                    initial_state=encoder_state,
                                                    beam_width=beam_width,
                                                    output_layer=output_layer,
                                                    length_penalty_weight=0.0)
enc_输出=tf.contrib.seq2seq.tile_批次(enc_输出,乘数=波束宽度)
num_bi_layes=int(num_layers/2)
如果num_bi_layes==1:
编码器_状态=enc_状态
其他:
编码器_状态=[]
对于范围内的层id(num\u bi\u layes):
编码器_state.append(enc_state[0][layer_id])#转发
编码器_state.append(enc_state[1][layer_id])#向后
编码器\ U状态=touple(编码器\ U状态)
编码器状态=tf.contrib.seq2seq.tile批次(编码器状态,倍增器=波束宽度)
文本长度=tf.contrib.seq2seq.tile\u批次(文本长度,乘数=波束宽度)
dec_cell=tf.nn.rnn_cell.multirncell([make_lstm(rnn_大小,keep_prob)用于范围内(num_层)])
dec_cell=解码器_cell(dec_cell、rnn_大小、enc_输出、文本长度)
start\u tokens=tf.tile(tf.constant([word2ind['']],dtype=tf.int32),[batch\u size],name='start\u tokens')
使用tf.variable_scope(“decode”,reuse=True):
解码器=tf.contrib.seq2seq.BeamSearchDecoder(小区=dec_小区,
嵌入=嵌入,
启动令牌=启动令牌,
结束标记=结束标记,
初始状态=编码器状态,
波束宽度=波束宽度,
输出层=输出层,
长度(惩罚(重量=0.0)

确保将
插补完成=False
传递到
动态解码()?我面对的是同样的。。。
beam_width = 10
dec_cell = tf.nn.rnn_cell.MultiRNNCell([make_lstm(rnn_size, keep_prob) for _ in range(num_layers)])
output_layer = Dense(vocab_size, kernel_initializer = tf.truncated_normal_initializer(mean = 0.0, stddev=0.1))
dec_cell = decoder_cell(dec_cell, rnn_size, enc_output, text_length)


with tf.variable_scope("decode"):
    # (dec_embed_input comes from another function but should not be 
    #   relevant in this context. )
    helper = tf.contrib.seq2seq.TrainingHelper(inputs = dec_embed_input, 
                                               sequence_length = summary_length,
                                               time_major = False)

    decoder = tf.contrib.seq2seq.BasicDecoder(cell = dec_cell,
                                              helper = helper,
                                              initial_state = dec_cell.zero_state(batch_size, tf.float32),
                                              output_layer = output_layer)

    logits = tf.contrib.seq2seq.dynamic_decode(decoder=decoder, 
                                           output_time_major=False, 
                                           impute_finished=True, 
                                           maximum_iterations=max_summary_length)




enc_output = tf.contrib.seq2seq.tile_batch(enc_output, multiplier=beam_width)
enc_state = tf.contrib.seq2seq.tile_batch(enc_state, multiplier=beam_width)
text_length = tf.contrib.seq2seq.tile_batch(text_length, multiplier=beam_width)

dec_cell = tf.nn.rnn_cell.MultiRNNCell([make_lstm(rnn_size, keep_prob) for _ in range(num_layers)])
dec_cell = decoder_cell(dec_cell, rnn_size, enc_output, text_length)

start_tokens = tf.tile(tf.constant([word2ind['<GO>']], dtype = tf.int32), [batch_size], name = 'start_tokens')

with tf.variable_scope("decode", reuse = True):


    decoder = tf.contrib.seq2seq.BeamSearchDecoder( cell=dec_cell,
                                                    embedding=embeddings,
                                                    start_tokens=start_tokens,
                                                    end_token=end_token,
                                                    initial_state=dec_cell.zero_state(batch_size = batch_size*beam_width , dtype = tf.float32),
                                                    beam_width=beam_width,
                                                    output_layer=output_layer,
                                                    length_penalty_weight=0.0)



    logits = tf.contrib.seq2seq.dynamic_decode(decoder=decoder, 
                                           output_time_major=False, 
                                           impute_finished=True, 
                                           maximum_iterations=max_summary_length)
decoder = tf.contrib.seq2seq.BeamSearchDecoder( cell=dec_cell,
                                                    embedding=embeddings,
                                                    start_tokens=start_tokens,
                                                    end_token=end_token,
                                                    initial_state=dec_cell.zero_state(batch_size = batch_size*beam_width , dtype = tf.float32),
                                                    beam_width=beam_width,
                                                    output_layer=output_layer,
                                                    length_penalty_weight=0.0)
ValueError: Shapes must be equal rank, but are 3 and 2 for 'decode_1/decoder/while/Select_4' (op: 'Select') with input shapes: [64,10], [64,10,256], [64,10,256].
enc_output = tf.contrib.seq2seq.tile_batch(enc_output, multiplier=beam_width)
num_bi_layes = int(num_layers/2)
if num_bi_layes == 1:
    encoder_state = enc_state
else:
    encoder_state = []
    for layer_id in range(num_bi_layes):
        encoder_state.append(enc_state[0][layer_id]) #forward
        encoder_state.append(enc_state[1][layer_id]) #backward
        encoder_state = touple(encoder_state)


encoder_state = tf.contrib.seq2seq.tile_batch(encoder_state, multiplier=beam_width)

text_length = tf.contrib.seq2seq.tile_batch(text_length, multiplier=beam_width)

dec_cell = tf.nn.rnn_cell.MultiRNNCell([make_lstm(rnn_size, keep_prob) for _ in range(num_layers)])
dec_cell = decoder_cell(dec_cell, rnn_size, enc_output, text_length)

start_tokens = tf.tile(tf.constant([word2ind['<GO>']], dtype = tf.int32), [batch_size], name = 'start_tokens')

with tf.variable_scope("decode", reuse = True):
    decoder = tf.contrib.seq2seq.BeamSearchDecoder( cell=dec_cell,
                                                    embedding=embeddings,
                                                    start_tokens=start_tokens,
                                                    end_token=end_token,
                                                    initial_state=encoder_state,
                                                    beam_width=beam_width,
                                                    output_layer=output_layer,
                                                    length_penalty_weight=0.0)