tensorflow - Keras 不同注意力层之间的差异

标签 tensorflow keras recurrent-neural-network attention-model

我正在尝试为我的文本分类模型添加一个注意层。输入是文本(例如电影评论),输出是二元结果(例如正面与负面)。

model = Sequential()
model.add(Embedding(max_features, 32, input_length=maxlen))
model.add(Bidirectional(CuDNNGRU(16,return_sequences=True)))
##### add attention layer here #####
model.add(Dense(1, activation='sigmoid'))

经过一番搜索,我发现了几个用于 keras 的可阅读注意层。有keras.layers.Attention Keras 内置的层。还有SeqWeightedAttentionSeqSelfAttention layer在 keras-self-attention 包中。作为深度学习领域的新手,我很难理解这些层背后的机制。

这些层有什么作用?哪一个最适合我的模型?

非常感谢!

最佳答案

如果您使用 RNN,我不建议使用 keras.layers.Attention 类。
在分析 tf.keras.layers.Attention Github 代码以更好地理解如何使用相同的代码时,我遇到的第一行是 - “此类适用于 Dense 或 CNN 网络,而不适用于 RNN 网络”
Cyber​​ZHG 维护的另一个开源版本称为
keras-自我注意。据我所知,这不是 Keras 或 TensorFlow 库的一部分,似乎是一段独立的代码。这包含您提到的两个类 - SeqWeightedAttention 和 SeqSelfAttention 层类。前者返回一个 2D 值,后者返回一个 3D 值。所以 SeqWeightedAttention 应该适合你的情况。前者似乎松散地基于 Raffel 等人,可用于 Seq 分类,后者似乎是 Bahdanau 的变体。
一般来说,我建议您编写自己的 seq 到分类模型。可以在不到六行代码中添加注意力片段(基​​本要素)……比您花在集成、调试或理解这些外部库中的代码上的时间少得多。
请引用:Create an LSTM layer with Attention in Keras for multi-label text classification neural network

关于tensorflow - Keras 不同注意力层之间的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58546876/

相关文章:

tensorflow - 延迟回声 - 无法在 Keras 中重现 Tensorflow 结果

python - 如何在 Keras 中使用附加功能和词嵌入?

machine-learning - 构建神经网络 - 将网络作为参数传递在 keras 中不起作用

deep-learning - 堆叠式 LSTM 网络中每个 LSTM 层的输入是什么?

r - 为什么RNN总是输出1

gpu - 有没有办法强制 Bazel 串行运行测试

python - Tensorflow Hub 和 JS : how to fine-tune a pre-trained model and export it for using in Tensorflow. js?

python - 带有 TensorFlow 的推荐系统 (SVD)

python - SampleRNN - Pytorch 实现初学者

python - 以函数为核的卷积