gpt4 book ai didi

python - 序列到序列模型 TypeError : Cannot iterate over a tensor with unknown first dimension 上的 Keras 注意层

转载 作者:行者123 更新时间:2023-12-04 16:39:43 35 4
gpt4 key购买 nike

我正在使用 Tensorflow 2.1.1 并尝试使用 Attention 构建序列到序列模型。

latent_dim = 300
embedding_dim=100
batch_size = 128

# Encoder
encoder_inputs = tf.keras.Input(shape=(None,), dtype='int32')

#embedding layer
enc_emb = tf.keras.layers.Embedding(x_voc, embedding_dim,trainable=True)(encoder_inputs)

#encoder lstm 1
encoder_lstm = tf.keras.layers.LSTM(latent_dim,return_sequences=True,return_state=True,dropout=0.4,recurrent_dropout=0.4)
encoder_output, state_h, state_c = encoder_lstm(enc_emb)
print(encoder_output.shape)

# Set up the decoder, using `encoder_states` as initial state.
decoder_inputs = tf.keras.Input(shape=(None,), dtype='int32')

#embedding layer
dec_emb_layer = tf.keras.layers.Embedding(y_voc, embedding_dim,trainable=True)
dec_emb = dec_emb_layer(decoder_inputs)

decoder_lstm = tf.keras.layers.LSTM(latent_dim, return_sequences=True, return_state=True,dropout=0.4,recurrent_dropout=0.2)
decoder_output,decoder_fwd_state, decoder_back_state = decoder_lstm(dec_emb,initial_state=[state_h, state_c])

# Attention layer
attn_out, attn_states = tf.keras.layers.Attention()([encoder_output, decoder_output])

# Concat attention input and decoder LSTM output
decoder_concat_input = tf.keras.layers.Concatenate(axis=-1, name='concat_layer')([decoder_output, attn_out])

#dense layer
decoder_dense = tf.keras.layers.TimeDistributed(Dense(y_voc, activation='softmax'))
decoder_outputs = decoder_dense(decoder_concat_input)

# Define the model
model = Model([encoder_inputs, decoder_inputs], decoder_outputs)

model.summary()

当我运行它时,我在创建注意力层时遇到错误 TypeError: Cannot iterate over a tensor with unknown first dimension.

我检查了 encoder_outputdecoder_output 的维度,它们都是 (None, None, 300) 所以我认为这可能是问题所在.但是我检查了 tensorflow example 中的注意示例他们的注意力层输入参数也有 None 维度。

我不知道我错过了什么?请提出建议。

编辑

添加堆栈跟踪

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-49-d37cd48e626b> in <module>()
28
29 # Attention layer
---> 30 attn_out, attn_states = tf.keras.layers.Attention()([encoder_output, decoder_output])
31
32 # Concat attention input and decoder LSTM output

~/anaconda3/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py in __iter__(self)
546 if shape[0] is None:
547 raise TypeError(
--> 548 "Cannot iterate over a tensor with unknown first dimension.")
549 for i in xrange(shape[0]):
550 yield self[i]

TypeError: Cannot iterate over a tensor with unknown first dimension.

最佳答案

错误是因为 keras Attention 输出 1 张量,而你期望的是 2。你需要更改

attn_out, attn_states = tf.keras.layers.Attention()([encoder_output, decoder_output])

进入

attn_out = tf.keras.layers.Attention()([encoder_output, decoder_output])

这里是完整的模型

# Encoder
encoder_inputs = tf.keras.Input(shape=(None,), dtype='int32')

#embedding layer
enc_emb = tf.keras.layers.Embedding(x_voc, embedding_dim)(encoder_inputs)

#encoder lstm 1
encoder_lstm = tf.keras.layers.LSTM(latent_dim, return_sequences=True,return_state=True)
encoder_output, state_h, state_c = encoder_lstm(enc_emb)

# Set up the decoder, using `encoder_states` as initial state.
decoder_inputs = tf.keras.Input(shape=(None,), dtype='int32')

#embedding layer
dec_emb = tf.keras.layers.Embedding(y_voc, embedding_dim)(decoder_inputs)

decoder_lstm = tf.keras.layers.LSTM(latent_dim, return_sequences=True, return_state=True)
decoder_output,decoder_fwd_state,decoder_back_state = decoder_lstm(dec_emb,initial_state=[state_h, state_c])

# Attention layer
attn_out = tf.keras.layers.Attention()([encoder_output, decoder_output])

# Concat attention input and decoder LSTM output
decoder_concat_input = tf.keras.layers.Concatenate(axis=-1, name='concat_layer')([decoder_output, attn_out])

#dense layer
decoder_dense = tf.keras.layers.TimeDistributed(Dense(y_voc, activation='softmax'))
decoder_outputs = decoder_dense(decoder_concat_input)

# Define the model
model = Model([encoder_inputs, decoder_inputs], decoder_outputs)

model.summary()

关于python - 序列到序列模型 TypeError : Cannot iterate over a tensor with unknown first dimension 上的 Keras 注意层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63289566/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com