Tensorflow BahdanauAttention-图层memory_layer预期有1个输入,但收到了2个输入张量

时间:2019-03-08 17:22:23

标签: python tensorflow artificial-intelligence

  

Tensorflow:1.12

我正在使用bidirectional_dynamic_rnn。我写了encoder_output到BahdanauAttention的memory选项(在tensorflow网站上推荐),但是它抛出一个错误:

  

ValueError:层memory_layer需要1个输入,但收到2个   输入张量。收到的输入:tf.Tensor   'bidirectional_rnn / fw / fw / transpose_1:0'shape =(?,?,512)   dtype = float32,tf.Tensor'ReverseSequence:0'shape =(?,?,512)   dtype = float32]

def model_inputs():
    inputs = tf.placeholder(tf.int32, [None, None], name='input')
    targets = tf.placeholder(tf.int32, [None, None], name='target')
    lr = tf.placeholder(tf.float32, name='learning_rate')
    keep_prob = tf.placeholder(tf.float32, name='keep_prob')
    return inputs, targets, lr, keep_prob

def preprocess_targets(targets, word2int, batch_size):
    left_side = tf.fill([batch_size, 1], word2int['<SOS>'])
    right_side = tf.strided_slice(targets, [0,0], [batch_size, -1], [1,1])
    preprocessed_targets = tf.concat([left_side, right_side], 1)
    return preprocessed_targets

#Encoder RNN
def encoder_rnn(rnn_inputs, rnn_size, num_layers, keep_prob, sequence_lenght):
    lstm = tf.contrib.rnn.BasicLSTMCell(rnn_size)
    lstm_dropout = tf.contrib.rnn.DropoutWrapper(lstm, input_keep_prob = keep_prob)
    encoder_cell = tf.contrib.rnn.MultiRNNCell([lstm_dropout] * num_layers)
    global encoder_output, encoder_state
    encoder_output, encoder_state = tf.nn.bidirectional_dynamic_rnn(cell_fw = encoder_cell,
                                                                    cell_bw = encoder_cell,
                                                                    sequence_length = sequence_length,
                                                                    inputs = rnn_inputs,
                                                                    dtype = tf.float32)

    return encoder_state



#Decoding training set
def decode_training_set(encoder_state, decoder_cell, decoder_embedded_input, sequence_lenght, decoding_scope, output_function, keep_prob, batch_size):
    #attention_states = tf.zeros([batch_size, 1, decoder_cell.output_size])
    attention_mechanism = tf.contrib.seq2seq.BahdanauAttention(num_units = decoder_cell.output_size, memory = encoder_output, normalize=False)

我该怎么办?

0 个答案:

没有答案