【问题标题】:BI LSTM with attention layer in python for text classificationBI LSTM 在 python 中具有注意力层,用于文本分类
【发布时间】:2020-11-04 08:02:46
【问题描述】:

我想应用这种方法来实现 Bi-LSTM,并注意。方法在这里讨论:Bi-LSTM Attention model in Keras

我收到以下错误: 'module' object is not callable

它不能在这一行应用乘法: sent_representation = merge([lstm, attention], mode='mul')

from keras.layers import merge
import tensorflow as tf
from tensorflow.keras.layers import Concatenate, Dense, Input, LSTM, Embedding, Dropout, Activation, Flatten, Permute, RepeatVector
from tensorflow.keras.layers import Bidirectional

inp =Input(shape=(maxlen,), dtype='float32')
x = Embedding(max_features, embed_size, weights=[emb_matrix])(inp)
lstm = Bidirectional(LSTM(50, return_sequences=True), name="bi_lstm_0")(x)

attention = Dense(1, activation='tanh')(lstm)
attention = Flatten()(attention)
attention = Activation('softmax')(attention)
attention = RepeatVector(max_features*2)(attention)
attention = Permute([2,1])(attention)

sent_representation = merge([lstm, attention], mode='mul')
sent_representation = Lambda(lambda xin: K.sum(xin, axis=1))(sent_representation)

output = Dense(3, activation="softmax")(sent_representation)

【问题讨论】:

  • 我删除了与问题无关的代码。在原始代码中,您使用 softmax 进行了两次投影,它不会那样学习。
  • 谢谢。你能帮我解决我的问题吗?我是 nlp 和深度学习的新手,我在这几天和每次遇到新错误时都在关注这个问题。
  • 因为最后我需要 3 个标签,我不能使用 sigmoid 或者..你能帮帮我吗?
  • 但是您已经在使用带有 3 个标签的 softmax 归一化。那有什么问题呢?
  • 即注意力层。

标签: python tensorflow keras deep-learning nlp


【解决方案1】:

在 Keras 中,merge 是一个包含层的模块,这些层实现了合并其他层输出的各种方式。您需要选择一种用于合并状态的方法。

在这种特殊情况下,您希望 concatenate 输出。

【讨论】:

  • 你能告诉我,如何添加连接?
  • 它是tensorflow.keras.layers.Concatenate。您已经导入了它。
  • 我试过了,但错误:无法将 类型的对象转换为张量。内容:。考虑将元素转换为支持的类型。
猜你喜欢
  • 1970-01-01
  • 2020-11-06
  • 1970-01-01
  • 2021-08-24
  • 1970-01-01
  • 2018-08-11
  • 1970-01-01
  • 2019-05-22
  • 2020-03-16
相关资源
最近更新 更多