我正在研究具有注意力机制的 sequence2sequence 模型,因此我使用的是编码器-解码器架构。
解码器部分代码是:
# Now create the Decoder layers.
decoder_inputs = Input(shape=(len_target,))
decoder_emb = Embedding(input_dim=vocab_out_size, output_dim=embedding_dim)
decoder_lstm = LSTM(units=units, return_sequences=True, return_state=True)
decoder_lstm_out, _, _ = decoder_lstm(decoder_emb(decoder_inputs),
initial_state=encoder_states)
x2 = reduce_dim(decoder_lstm_out)
attn_layer = AttentionLayer(name='attention_layer')
attn_out, attn_states = attn_layer([z, x2])
decoder_concat_input = Concatenate(axis=-1, name='concat_layer')([x2,
attn_out])
lm= Lambda(decoder_concat_input)
decoder_d2 = Dense(vocab_out_size, activation="softmax")
decoder_out = decoder_d2(lm)
model = tf.keras.Model([encoder_inputs, decoder_inputs], decoder_out)
reduce_dim 函数()是:
def reduce_dim(decoder_lstm_out):
x = decoder_lstm_out
x = tf.placeholder(tf.float32, shape=[None, 12, 12, 256])
dim = tf.reduce_prod(tf.shape(x)[0:1])
x2 = tf.reshape(x, [dim,12,256])
return x2
引发错误:
AttributeError:“Lambda”对象没有属性“shape”
在线:
解码器输出 = 解码器 d2(lm)
如果有人可以帮助我吗?