赞
踩
自然语言处理(NLP)Bert与Lstm结合
https://blog.csdn.net/zhangtingduo/article/details/1084744013
可以参考:https://blog.csdn.net/zhangtingduo/article/details/1084744013
这里只重要的步骤
import pandas as pd import numpy as np import torch import torch.nn as nn import torch.nn.functional as F from torch.utils.data import TensorDataset, DataLoader # 利用transformers 先进行分字编码 from transformers import BertTokenizer,BertModel tokenizer = BertTokenizer.from_pretrained("./chinese-bert_chinese_wwm_pytorch/data") # result_comments是data的数据集 result_comments_id=tokenizer(result_comments,padding=True,truncation=True,max_length=200,return_tensors='pt') # 模型 class bert_lstm(nn.Module): def __init__(self, hidden_dim,output_size,n_layers,bidirectional=True, drop_prob=0.5): super(bert_lstm, self).__init__() self.output_size = output_size self.n_layers = n_layers self.hidden_dim = hidden_dim self.bidirectional = bidirectional #Bert ----------------重点,bert模型需要嵌入到自定义模型里面 self.bert=BertModel.from_pretrained("../chinese-bert_chinese_wwm_pytorch/data") for param in self.bert.parameters(): param.requires_grad = True # LSTM layers self.lstm = nn.LSTM(768, hidden_dim, n_layers, batch_first=True,bidirectional=bidirectional) # dropout layer self.dropout = nn.Dropout(drop_prob) # linear and sigmoid layers if bidirectional: self.fc = nn.Linear(hidden_dim*2, output_size) else: self.fc = nn.Linear(hidden_dim, output_size) #self.sig = nn.Sigmoid() def forward(self, x, hidden): batch_size = x.size(0) #生成bert字向量 x=self.bert(x)[0] #bert 字向量 # lstm_out #x = x.float() lstm_out, (hidden_last,cn_last) = self.lstm(x, hidden) #print(lstm_out.shape) #[32,100,768] #print(hidden_last.shape) #[4, 32, 384] #print(cn_last.shape) #[4, 32, 384] #修改 双向的需要单独处理 if self.bidirectional: #正向最后一层,最后一个时刻 hidden_last_L=hidden_last[-2] #print(hidden_last_L.shape) #[32, 384] #反向最后一层,最后一个时刻 hidden_last_R=hidden_last[-1] #print(hidden_last_R.shape) #[32, 384] #进行拼接 hidden_last_out=torch.cat([hidden_last_L,hidden_last_R],dim=-1) #print(hidden_last_out.shape,'hidden_last_out') #[32, 768] else: hidden_last_out=hidden_last[-1] #[32, 384] # dropout and fully-connected layer out = self.dropout(hidden_last_out) #print(out.shape) #[32,768] out = self.fc(out) return out def init_hidden(self, batch_size): weight = next(self.parameters()).data number = 1 if self.bidirectional: number = 2 if (USE_CUDA): hidden = (weight.new(self.n_layers*number, batch_size, self.hidden_dim).zero_().float().cuda(), weight.new(self.n_layers*number, batch_size, self.hidden_dim).zero_().float().cuda() ) else: hidden = (weight.new(self.n_layers*number, batch_size, self.hidden_dim).zero_().float(), weight.new(self.n_layers*number, batch_size, self.hidden_dim).zero_().float() ) return hidden
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。