paddlepaddle中在BiLstm後添加Attention

paddlepaddle中實現bilstm比較簡單, 但如何實現bilstm+attention目前沒搜到,參考這篇博文(https://aistudio.baidu.com/aistudio/projectdetail/122459),我自己寫了一個,不知道對不對,反正能跑通。以下除Attention層外的代碼來自於百度Ai Stuio中新出的EasyDL專業版(https://ai.baidu.com/easydl/pro)中的代碼。

      # embedding layer
        emb = fluid.layers.embedding(
            input=unpad_data,
            size=[dict_dim, emb_dim],
            param_attr=fluid.ParamAttr(learning_rate=emb_lr))
            
        # bilstm layer
        fc0 = fluid.layers.fc(input=emb, size=hid_dim * 4)
        rfc0 = fluid.layers.fc(input=emb, size=hid_dim * 4)
        lstm_h, c = fluid.layers.dynamic_lstm(
            input=fc0, size=hid_dim * 4, is_reverse=False)
        rlstm_h, c = fluid.layers.dynamic_lstm(
            input=rfc0, size=hid_dim * 4, is_reverse=True)
        
        # concat layer
        lstm_h_concat = fluid.layers.concat(input=[lstm_h, rlstm_h], axis=1)
        lstm_h_tanh = fluid.layers.tanh(lstm_h_concat)
        
        #attenion layer
        hidlayer_state = fluid.layers.fc(input=lstm_h_tanh, size=hid_dim*8, bias_attr=False)
        attention_weights = fluid.layers.fc(input=hidlayer_state, size=1, bias_attr=False)
        attention_weights = fluid.layers.sequence_softmax(input=attention_weights)
        weigths_reshape = fluid.layers.reshape(x=attention_weights, shape=[-1])
        scaled = fluid.layers.elementwise_mul(x=lstm_h_concat, y=weigths_reshape, axis=0)
        lstm_sum = fluid.layers.sequence_pool(input=scaled, pool_type='sum')
        
        # full connect layer
        fc1 = fluid.layers.fc(input=lstm_sum, size=hid_dim2, act='tanh')
        # softmax layer
        predictions = fluid.layers.fc(input=fc1, size=num_labels, act='softmax')

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章