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')

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