【pytorch模型实现3】C2W

24 篇文章 2 订阅
24 篇文章 0 订阅
该博客介绍了C2W模型的实现,该模型使用了字符双向LSTM和语言模型LSTM来预测单词。代码中定义了一个C2W类,包括字符嵌入、双向LSTM层、词向量表示、映射到词汇表的全连接层。模型的配置参数如字符、单词向量的维度、隐藏层大小和最大序列长度等也在Config类中定义。
摘要由CSDN通过智能技术生成

C2W模型实现

NLP模型代码github仓库:https://github.com/lyj157175/Models

import torch
import torch.nn as nn 

class C2W(nn.Module):

    def __init__(self, config):
        super(C2W, self).__init__()
        self.char_hidden_dim = config.char_hidden_dim
        self.word_embed_dim = config.word_embed_dim
        self.max_seq_len = config.max_seq_len
        self.lm_hidden_dim = config.lm_hidden_dim 

        self.char_embed = nn.Embedding(config.n_chars, config.char_embed_dim)

        # 字符双向lstm嵌入层
        self.char_lstm = nn.LSTM(input_size=config.char_embed_dim, hidden_size=config.char_hidden_dim, bidirectional=True, batch_first=True)
        # 语言模型单向lstm
        self.lm_lstm = nn.LSTM(input_size=config.word_embed_dim, hidden_size=config.lm_hidden_dim, batch_first=True)
        
        # 词向量表示
        self.fc1 = nn.Linear(2 * config.char_hidden_dim, config.word_embed_dim) 
        # 映射词表进行分类预测
        self.fc2 = nn.Linear(config.lm_hidden_dim, config.vocab_size)

    def forward(self, x):
        x = torch.Tensor(x).long()   # b, char_seq_len
        embed_x = self.char_embed(x) # b, char_seq_len, char_embed_dim
        char_out, _ = self.char_lstm(embed_x)   # b, char_seq_len, 2*char_hidden_dim
        word_input = torch.cat([char_out[:, -1, 0:self.char_hidden_dim], 
                                char_out[:, 0, self.char_hidden_dim:]], dim=1)   # b, 2*char_hidden_dim
        word_input = self.fc1(word_input)   # b, word_embed_dim
        word_input = word_input.view(-1, self.max_seq_len, self.word_embed_dim)  # b, max_seq_len, word_embed_dim
        word_out, _ = self.lm_lstm(word_input)   # b, max_seq_len, lm_hidden_dim 
        word_out = word_out.contiguous().view(-1, self.lm_hidden_dim)  # b*max_seq_len, lm_hidden_dim  
        out = self.fc2(word_out)  # b*max_seq_len, vocab_size
        return out



class Config:
    def __init__(self):
        self.n_chars = 32         # 字符个数
        self.char_embed_dim = 20  # 字符向量的维度
        self.char_hidden_dim = 30 # 字符的隐藏层神经元数目
        self.word_embed_dim = 50  # 单词的维度
        self.lm_hidden_dim = 100  # 语言模型的隐藏神经元数目
        self.max_seq_len = 10     # 句子的最大单词长度
        self.vocab_size = 1000    # 词表的大小

if __name__ == '__main__':
    config = Config()
    model = C2W(config)
    print(model) 
    for name, param in model.named_parameters():
        if param.requires_grad:
            print(name)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值