2017-07-14 83 views
0

我想在Tensorflow中使用dynamic_decode作爲注意模型。最初的版本是由https://github.com/tensorflow/nmt#decoderAttributeError:'張量'對象沒有'注意'屬性

learning_rate = 0.001 
n_hidden = 128 
total_epoch = 10000 
num_units=128 
n_class = n_input = 47 

num_steps=8 
embedding_size=30 


mode = tf.placeholder(tf.bool) 
embed_enc = tf.placeholder(tf.float32, shape=[None,num_steps,300]) 
embed_dec = tf.placeholder(tf.float32, shape=[None,num_steps,300]) 
targets=tf.placeholder(tf.int32, shape=[None,num_steps]) 

enc_seqlen = tf.placeholder(tf.int32, shape=[None]) 
dec_seqlen = tf.placeholder(tf.int32, shape=[None]) 
decoder_weights= tf.placeholder(tf.float32, shape=[None, num_steps]) 

with tf.variable_scope('encode'): 
    enc_cell = tf.contrib.rnn.BasicRNNCell(n_hidden) 
    enc_cell = tf.contrib.rnn.DropoutWrapper(enc_cell, output_keep_prob=0.5) 
    outputs, enc_states = tf.nn.dynamic_rnn(enc_cell, embed_enc,sequence_length=enc_seqlen, 
              dtype=tf.float32,time_major=True) 


attention_states = tf.transpose(outputs, [1, 0, 2]) 

# Create an attention mechanism 
attention_mechanism = tf.contrib.seq2seq.LuongAttention(
    num_units, attention_states, 
    memory_sequence_length=enc_seqlen) 


decoder_cell = tf.contrib.rnn.BasicLSTMCell(num_units) 
decoder_cell = tf.contrib.seq2seq.AttentionWrapper(
    decoder_cell, attention_mechanism, 
    attention_layer_size=num_units) 

helper = tf.contrib.seq2seq.TrainingHelper(
    embed_dec, dec_seqlen, time_major=True) 
# Decoder 
projection_layer = Dense(
    47, use_bias=False) 
decoder = tf.contrib.seq2seq.BasicDecoder(
    decoder_cell, helper, enc_states, 
    output_layer=projection_layer) 
# Dynamic decoding 
outputs, _ = tf.contrib.seq2seq.dynamic_decode(decoder) 

規定,但是,當我跑

tf.contrib.seq2seq.dynamic_decode(decoder) 

我得到一個錯誤,錯誤會顯示如下

Traceback (most recent call last): 

    File "<ipython-input-19-0708495dbbfb>", line 27, in <module> 
    outputs, _ = tf.contrib.seq2seq.dynamic_decode(decoder) 

    File "D:\Anaconda3\lib\site-packages\tensorflow\contrib\seq2seq\python\ops\decoder.py", line 286, in dynamic_decode 
    swap_memory=swap_memory) 

    File "D:\Anaconda3\lib\site-packages\tensorflow\python\ops\control_flow_ops.py", line 2775, in while_loop 
    result = context.BuildLoop(cond, body, loop_vars, shape_invariants) 

    File "D:\Anaconda3\lib\site-packages\tensorflow\python\ops\control_flow_ops.py", line 2604, in BuildLoop 
    pred, body, original_loop_vars, loop_vars, shape_invariants) 

    File "D:\Anaconda3\lib\site-packages\tensorflow\python\ops\control_flow_ops.py", line 2554, in _BuildLoop 
    body_result = body(*packed_vars_for_body) 

    File "D:\Anaconda3\lib\site-packages\tensorflow\contrib\seq2seq\python\ops\decoder.py", line 234, in body 
    decoder_finished) = decoder.step(time, inputs, state) 

    File "D:\Anaconda3\lib\site-packages\tensorflow\contrib\seq2seq\python\ops\basic_decoder.py", line 139, in step 
    cell_outputs, cell_state = self._cell(inputs, state) 

    File "D:\Anaconda3\lib\site-packages\tensorflow\python\ops\rnn_cell_impl.py", line 180, in __call__ 
    return super(RNNCell, self).__call__(inputs, state) 

    File "D:\Anaconda3\lib\site-packages\tensorflow\python\layers\base.py", line 450, in __call__ 
    outputs = self.call(inputs, *args, **kwargs) 

    File "D:\Anaconda3\lib\site-packages\tensorflow\contrib\seq2seq\python\ops\attention_wrapper.py", line 1143, in call 
    cell_inputs = self._cell_input_fn(inputs, state.attention) 

AttributeError: 'Tensor' object has no attribute 'attention' 

我試着安裝了最新的1.2.1 tensorflow但它不起作用。 謝謝你的幫助。

UPDATE:

的問題是,如果我改變BasicDecoder的initial_states:

decoder = tf.contrib.seq2seq.BasicDecoder(
     decoder_cell, helper, enc_states, 
     output_layer=projection_layer) 

到:

decoder = tf.contrib.seq2seq.BasicDecoder(
     decoder_cell, helper, 
     decoder_cell.zero_state(dtype=tf.float32,batch_size=batch_size), 
     output_layer=projection_layer) 

然後,它的工作原理。我不知道它是否是一個正確的解決方案,因爲initial_states設置爲零似乎有線。 謝謝你的幫助。

回答

0

你的方法是正確的。我爲未來的用戶在tf master分支中添加了更好的錯誤消息。由於您正在使用注意力,因此您可能不需要將任何內容傳遞給解碼器初始狀態。然而,將編碼器最終狀態輸入進來仍然很常見。您可以通過創建解碼器單元零狀態來執行此操作,並使用arg cell_state = encoder_final_state調用其.clone方法。使用結果對象作爲初始解碼器狀態。

0

你能寫所有的電話到處使用kwargs嗎?即tf.nn.dynamic_rnn(cell=..., inputs=...)等,我認爲你的參數是錯誤的地方,並使用kwargs應該解決它。

+0

我試過這個,但它顯示了同樣的錯誤。你能看到我的更新嗎?這是一個正確的方法嗎? – Shichen

相關問題