首页 > 其他 > 详细

pytorch与NLP(一):textCNN

时间:2021-04-20 20:59:08      阅读:30      评论:0      收藏:0      [点我收藏+]

一、代码

from torch import nn
import torch
import torch.nn.functional as F


class BiLSTM_Attention(nn.Module):
    def __init__(self,embedding_dim, num_hiddens, num_layers):
        super(BiLSTM_Attention, self).__init__()
        # bidirectional设为True即得到双向循环神经网络
        self.encoder = nn.LSTM(input_size=embedding_dim,
                               hidden_size=num_hiddens,
                               num_layers=num_layers,
                               batch_first=True,
                               bidirectional=True)
        # 初始时间步和最终时间步的隐藏状态作为全连接层输入
        self.w_omega = nn.Parameter(torch.Tensor(
            num_hiddens * 2, num_hiddens * 2))
        self.u_omega = nn.Parameter(torch.Tensor(num_hiddens * 2, 1))
        self.decoder = nn.Linear(2 * num_hiddens, 4)
        nn.init.uniform_(self.w_omega, -0.1, 0.1)
        nn.init.uniform_(self.u_omega, -0.1, 0.1)

    def forward(self, embeddings):


        # rnn.LSTM只返回最后一层的隐藏层在各时间步的隐藏状态。
        # embeddings形状是:(batch_size, seq_len, embedding_size)
        outputs, _ = self.encoder(embeddings)  # output, (h, c)
        # outputs形状是(batch_size, seq_len, 2 * num_hiddens)

        # Attention过程
        u = torch.tanh(torch.matmul(outputs, self.w_omega))
        # u形状是(batch_size, seq_len, 2 * num_hiddens)
        att = torch.matmul(u, self.u_omega)
        # att形状是(batch_size, seq_len, 1)
        att_score = F.softmax(att, dim=1)
        # att_score形状仍为(batch_size, seq_len, 1)
        scored_x = outputs * att_score
        # scored_x形状是(batch_size, seq_len, 2 * num_hiddens)
        # Attention过程结束

        feat = torch.sum(scored_x, dim=1)  # 加权求和
        # feat形状是(batch_size, 2 * num_hiddens)
        outs = self.decoder(feat)
        # out形状是(batch_size, 4)
        return outs

 二、图形

技术分享图片

 

pytorch与NLP(一):textCNN

原文:https://www.cnblogs.com/zhangxianrong/p/14682246.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!