gpt4 book ai didi

python - 如何使用 tensorflow 注意层?

转载 作者:行者123 更新时间:2023-12-03 20:51:23 25 4
gpt4 key购买 nike

我想了解如何使用 tf.keras.layers.Attention此处显示:
Tensorflow Attention Layer
我正在尝试将它与编码器解码器 seq2seq 模型一起使用。下面是我的代码:

encoder_inputs = Input(shape=(max_len_text,)) 
enc_emb = Embedding(x_voc_size, latent_dim,trainable=True)(encoder_inputs)
encoder_lstm=LSTM(latent_dim, return_state=True, return_sequences=True)
encoder_outputs, state_h, state_c= encoder_lstm(enc_emb)

decoder_inputs = Input(shape=(max_len_summary,))
dec_emb_layer = Embedding(y_voc_size, latent_dim,trainable=True)
dec_emb = dec_emb_layer(decoder_inputs)

decoder_lstm = LSTM(latent_dim, return_sequences=True, return_state=True)
decoder_outputs,decoder_fwd_state, decoder_back_state = decoder_lstm(dec_emb,initial_state=[state_h, state_c])
我的问题是,如何使用这个模型在 keras 中使用给定的注意力层?我无法理解他们的文档。

最佳答案

如果您使用的是 RNN,我不建议您使用上述类。
在分析 tf.keras.layers.Attention Github 代码以更好地理解您的难题时,我遇到的第一行是 - “该类(class)适用于 Dense 或 CNN 网络,而不适用于 RNN 网络”
我建议您编写自己的 seq 到 seq 模型,只需不到十行代码即可完成。例如:https://www.tensorflow.org/tutorials/text/nmt_with_attention
要编写您自己的自定义注意力层(基于您是否喜欢 Bahdanau、Luong、Raffel、Yang 等),也许这篇概述基本要素的帖子可能会有所帮助:Custom Attention Layer using in Keras

关于python - 如何使用 tensorflow 注意层?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62614719/

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