python - return_sequence=True 的 LSTM 之后的 Keras Dense 层

标签 python keras deep-learning nlp

我正在尝试重新实现这篇论文1作者在 Keras 中使用 PyTorch 2 。这是网络架构: enter image description here 到目前为止我所做的是:

number_of_output_classes = 1
hidden_size = 100
direc = 2
lstm_layer=Bidirectional(LSTM(hidden_size, dropout=0.2, return_sequences=True))(combined) #shape after this step (None, 200) 
#weighted sum and attention should be here
attention = Dense(hidden_size*direc, activation='linear')(lstm_layer) #failed trial
drop_out_layer = Dropout(0.2)(attention)    
output_layer=Dense(1,activation='sigmoid')(drop_out_layer) #shape after this step (None, 1)

我想在 LSTM 之后包含注意力层和最终 FF 层,但由于维度和 return_sequence= True 选项,我遇到了错误。

最佳答案

这是一个序列分类任务。序列分类是多对一的映射,其中顺序输入被标记为单个类别。在这种情况下,您的输入的形状应为(batch_size,time_steps,channels),输出的形状应为(batch_size,channels)。如果 LSTM 类的 return_sequences 参数为 True,则输出的形状将为(batch_size、time_steps、channels)。将其输入到密集层和丢失层不会减少维度数。要将维度数减少到 2,您必须将最后 LSTM 层的 return_sequences 参数设置为 False。对于你的情况

lstm_layer=Bidirectional(LSTM(hidden_size, dropout=0.2, return_sequences=False))(combined)

关于python - return_sequence=True 的 LSTM 之后的 Keras Dense 层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55232757/

相关文章:

Python c-api 和 unicode 字符串

python - 加入列表中的元组

python - Keras 双向 LSTM - 层分组

tensorflow - 如何使用 Keras 打开大型 Parquet 文件?

python - 将从模型训练层提取的特征向量显示为图像

python - ValueError : not enough values to unpack (expected 3, 在 Pytorch 中得到 2)

python - 使用 Keras、Python 纠正 CNN、基于 LSTM 的分类器的输入维度

Python 到 MySQLdb 不会传递变量我想我已经尝试了一切

python - Google Client API v3 - 使用 Python 更新驱动器上的文件

python - 使用 tf.keras 重写 TensorFlow 和 Keras