简体   繁体   English

AttributeError:“ Tensor”对象没有属性“ attention”

[英]AttributeError: 'Tensor' object has no attribute 'attention'

I'm trying to use dynamic_decode in Tensorflow for an attention model. 我正在尝试将Tensorflow中的dynamic_decode用于关注模型。 The original version is provided by https://github.com/tensorflow/nmt#decoder 原始版本由https://github.com/tensorflow/nmt#decoder提供

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)

But i got an error when i ran 但是我跑的时候出错

tf.contrib.seq2seq.dynamic_decode(decoder)

and error shows as below 错误显示如下

    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'

I tried installed the latest tensorflow 1.2.1 but it didn't work. 我尝试安装了最新的tensorflow 1.2.1,但是没有用。 Thank you for your help. 谢谢您的帮助。

UPDATE: 更新:

The problem is if I change initial_states of BasicDecoder: 问题是如果我更改BasicDecoder的initial_states:

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

into: 成:

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

Then it works. 然后就可以了。 I have no idea if it is a correct solution because initial_states set to zero seems wired. 我不知道这是否是正确的解决方案,因为initial_states设置为零似乎是有线的。 Thank you for your help. 谢谢您的帮助。

Can you write all your calls to use kwargs everywhere? 您可以编写所有在任何地方使用kwarg的电话吗? ie, tf.nn.dynamic_rnn(cell=..., inputs=...) etc. i think your args are misplaced somewhere and using kwargs should resolve it. 即, tf.nn.dynamic_rnn(cell=..., inputs=...)等。我认为您的args tf.nn.dynamic_rnn(cell=..., inputs=...)在某个地方,使用kwargs应该可以解决它。

Your approach is correct. 您的方法是正确的。 i added better error messaging in tf master branch for future users. 我在tf master分支中为将来的用户添加了更好的错误消息。 since you're using attention, you probably don't really need to pass anything through to the decoder initial state. 因为您正在使用注意力,所以您可能实际上不需要将任何内容传递给解码器的初始状态。 however it's still common to feed the encoder final state in. you can do this by creating a decoder cell zero state the way you're doing, and calling its .clone method with arg cell_state=encoder_final_state. 但是,将编码器的最终状态馈入仍然很常见。您可以通过以下方式来实现此目的:创建解码器单元零状态,然后使用arg cell_state = encoder_final_state调用其.clone方法。 use the resulting object as the initial decoder state. 将结果对象用作初始解码器状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AttributeError: &#39;Tensor&#39; 对象在注意力模型中没有属性 &#39;assign&#39; - AttributeError: 'Tensor' object has no attribute 'assign' in an attention model AttributeError: &#39;Tensor&#39; 对象没有属性 &#39;numpy&#39; - AttributeError: 'Tensor' object has no attribute 'numpy' AttributeError:“张量”对象没有“关闭”属性 - AttributeError: 'Tensor' object has no attribute 'close' AttributeError:&#39;Tensor&#39;对象没有属性&#39;append&#39; - AttributeError: 'Tensor' object has no attribute 'append' TensorFlow:AttributeError:&#39;Tensor&#39;对象没有属性&#39;shape&#39; - TensorFlow: AttributeError: 'Tensor' object has no attribute 'shape' Medpy AttributeError: &#39;Tensor&#39; 对象没有属性 &#39;astype&#39; - Medpy AttributeError: 'Tensor' object has no attribute 'astype' AttributeError:“张量”object 没有属性“to_sparse” - AttributeError: 'Tensor' object has no attribute 'to_sparse' AttributeError:&#39;Tensor&#39;对象没有属性&#39;reshape&#39; - AttributeError: 'Tensor' object has no attribute 'reshape' AttributeError: &#39;Tensor&#39; 对象没有属性 &#39;numpy - AttributeError: 'Tensor' object has no attribute 'numpy AttributeError:&#39;Tensor&#39;对象没有属性&#39;shape&#39; - AttributeError: 'Tensor' object has no attribute 'shape'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM