当前位置:   article > 正文

NLP进阶,Bert+BiLSTM情感分析实战,Python多线程实现方式及并发与同步_bert+bilstm情感分析代码

bert+bilstm情感分析代码

#Bert ----------------重点,bert模型需要嵌入到自定义模型里面

self.bert=BertModel.from_pretrained(bertpath)

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

bert_lstm需要的参数功6个,参数说明如下:

–bertpath:bert预训练模型的路径

–hidden_dim:隐藏层的数量。

–output_size:分类的个数。

–n_layers:lstm的层数

–bidirectional:是否是双向lstm

–drop_prob:dropout的参数

定义bert的参数,如下:

class ModelConfig:

batch_size = 2

output_size = 2

hidden_dim = 384 #768/2

n_layers = 2

lr = 2e-5

bidirectional = True #这里为True,为双向LSTM

training params

epochs = 10

batch_size=50

print_every = 10

clip=5 # gradient clipping

use_cuda = USE_CUDA

bert_path = ‘bert-base-chinese’ #预训练bert路径

save_path = ‘bert_bilstm.pth’ #模型保存路径

batch_size:batchsize的大小,根据显存设置。

output_size:输出的类别个数,本例是2.

hidden_dim:隐藏层的数量。

n_layers:lstm的层数。

在这里插入图片描述

感谢每一个认真阅读我文章的人,看着粉丝一路的上涨和关注,礼尚往来总是要有的:

① 2000多本Python电子书(主流和经典的书籍应该都有了)

② Python标准库资料(最全中文版)

③ 项目源码(四五十个有趣且经典的练手项目及源码)

④ Python基础入门、爬虫、web开发、大数据分析方面的视频(适合小白学习)

⑤ Python学习路线图(告别不入流的学习)

小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Python工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Python爬虫全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频

如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注:python)
img

效又漫长,而且极易碰到天花板技术停滞不前!**

因此收集整理了一份《2024年Python爬虫全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频

如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注:python)
[外链图片转存中…(img-VOHZxYK1-1711058211021)]

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/383852
推荐阅读
相关标签
  

闽ICP备14008679号