簡體   English   中英

python中帶有注意力層的BI LSTM用於文本分類

[英]BI LSTM with attention layer in python for text classification

我想應用這種方法來實現 Bi-LSTM 的注意。 此處討論該方法: Keras 中的 Bi-LSTM 注意力模型

我收到以下錯誤: '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)

在 Keras 中, merge是一個模塊,其中包含實現合並其他層輸出的各種方式的層。 您需要選擇要用於合並狀態的方法。

在這種特殊情況下,您希望連接輸出。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM