第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

Keras:如何在 LSTM 模型中顯示注意力權(quán)重

Keras:如何在 LSTM 模型中顯示注意力權(quán)重

藍(lán)山帝景 2021-06-06 12:34:37
我使用帶有注意力層的 LSTM 制作了一個(gè)文本分類(lèi)模型。我的模型做得很好,效果很好,但是我無(wú)法顯示評(píng)論(輸入文本)中每個(gè)單詞的注意力權(quán)重和重要性/注意力。該模型使用的代碼是:def dot_product(x, kernel):   if K.backend() == 'tensorflow':       return K.squeeze(K.dot(x, K.expand_dims(kernel)),axis=-1)   else:       return K.dot(x, kernel)class AttentionWithContext(Layer):    """Attention operation, with a context/query vector, for temporal data."Hierarchical Attention Networks for Document Classification"by using a context vector to assist the attention# Input shape    3D tensor with shape: (samples, steps, features).# Output shape    2D tensor with shape: (samples, features).How to use:Just put it on top of an RNN Layer (GRU/LSTM/SimpleRNN) with return_sequences=True.The dimensions are inferred based on the output shape of the RNN.Note: The layer has been tested with Keras 2.0.6Example:    model.add(LSTM(64, return_sequences=True))    model.add(AttentionWithContext())    # next add a Dense layer (for classification/regression) or whatever     """def __init__(self,             W_regularizer=None, u_regularizer=None, b_regularizer=None,             W_constraint=None, u_constraint=None, b_constraint=None,             bias=True, **kwargs):    self.supports_masking = True    self.init = initializers.get('glorot_uniform')    self.W_regularizer = regularizers.get(W_regularizer)    self.u_regularizer = regularizers.get(u_regularizer)    self.b_regularizer = regularizers.get(b_regularizer)    self.W_constraint = constraints.get(W_constraint)    self.u_constraint = constraints.get(u_constraint)    self.b_constraint = constraints.get(b_constraint)    self.bias = bias    super(AttentionWithContext, self).__init__(**kwargs)
查看完整描述

3 回答

?
慕慕森

TA貢獻(xiàn)1856條經(jīng)驗(yàn) 獲得超17個(gè)贊

看完以上綜合答案,我終于明白了如何提取注意力層的權(quán)重。總的來(lái)說(shuō),@李翔和@Okorimi Manoury 的想法都是正確的。對(duì)于@Okorimi Manoury 的代碼段,來(lái)自以下鏈接:Textual attention visualization。

現(xiàn)在,讓我逐步解釋該過(guò)程:

(1). 你應(yīng)該有一個(gè)訓(xùn)練有素的模型,你需要加載模型并提取注意力層的權(quán)重。要提取某些層權(quán)重,您可以使用model.summary()來(lái)檢查模型架構(gòu)。然后,您可以使用:

layer_weights = model.layers[3].get_weights() #suppose your attention layer is the third layer

layer_weights是一個(gè)列表,例如對(duì)于HAN注意力的詞級(jí)注意力,該列表layer_weights具有三個(gè)元素:W、b和u。換句話(huà)說(shuō),layer_weights[0] = W, layer_weights[1] = b, and layer_weights[2] = u。

(2). 您還需要在注意力層之前獲得層輸出。在這個(gè)例子中,我們需要得到第二層輸出。您可以使用以下代碼執(zhí)行以下操作:

new_model = Model(inputs=model.input, outputs=model.layers[2].output) output_before_att = new_model.predict(x_test_sample) #extract layer output

(3). 查看詳情:假設(shè)你輸入的是一個(gè)100字300維度的文本段(輸入是[100, 300]),第二層之后維度是128,那么形狀output_before_att就是[100, 128]。相應(yīng)地,layer_weights[0](W)為[128, 128],layer_weights[1](b)為[1, 128],layer_weights[2](u)為[1,128]。然后,我們需要以下代碼:

eij = np.tanh(np.dot(output_before_att, layer_weights[0]) + layer_weights[1]) #Eq.(5) in the paper

eij = np.dot(eij, layer_weights[2]) #Eq.(6)

eij = eij.reshape((eij.shape[0], eij.shape[1])) # reshape the vector

ai = np.exp(eij) #Eq.(6)

weights = ai / np.sum(ai) # Eq.(6)

weights是一個(gè)列表(100 維),每個(gè)元素是 100 個(gè)輸入詞的注意力權(quán)重(重要性)。之后,您可以可視化注意力權(quán)重。

希望我的解釋能幫到你。


查看完整回答
反對(duì) 回復(fù) 2021-06-09
?
拉風(fēng)的咖菲貓

TA貢獻(xiàn)1995條經(jīng)驗(yàn) 獲得超2個(gè)贊

您可以使用get_weights()自定義圖層的方法來(lái)獲取所有權(quán)重的列表。您可以在此處找到更多信息。


您需要在模型創(chuàng)建期間對(duì)代碼進(jìn)行以下修改:


model1.add(TimeDistributed(Dense(200)))

atn = AttentionWithContext()

model1.add(atn)

然后,訓(xùn)練后,只需使用:


atn.get_weights()[index]

將權(quán)重矩陣提取W為numpy數(shù)組(我認(rèn)為index應(yīng)該設(shè)置為0,但您必須自己嘗試)。然后你可以使用pyplot's imshow/matshow 方法來(lái)顯示矩陣。


查看完整回答
反對(duì) 回復(fù) 2021-06-09
  • 3 回答
  • 0 關(guān)注
  • 603 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)