【发布时间】: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