当前位置: 首页 > news >正文

网站开发过程中的功能需求分析域名ip查询查网址

网站开发过程中的功能需求分析,域名ip查询查网址,公司做网站自己可以做,什么兼职网站可以做视频剪辑用BERT做中文邮件内容分类 项目背景与意义项目思路数据集介绍环境配置数据加载与预处理自定义数据集模型训练加载BERT预训练模型开始训练 预测效果 项目背景与意义 本文是《用BERT做中文邮件内容分类》系列的第二篇,该系列项目持续更新中。系列的起源是《使用Paddl…

用BERT做中文邮件内容分类

    • 项目背景与意义
    • 项目思路
    • 数据集介绍
    • 环境配置
    • 数据加载与预处理
    • 自定义数据集
    • 模型训练
      • 加载BERT预训练模型
      • 开始训练
    • 预测效果

项目背景与意义

本文是《用BERT做中文邮件内容分类》系列的第二篇,该系列项目持续更新中。系列的起源是《使用PaddleNLP识别垃圾邮件》项目,旨在解决企业面临的垃圾邮件问题,通过深度学习方法探索多语言垃圾邮件的内容、标题提取与分类识别。

在本篇文章中,我们使用PaddleNLP的BERT预训练模型,根据提取的中文邮件内容判断邮件是否为垃圾邮件。该项目的思路在于基于前一篇项目的中文邮件内容提取,在98.5%的垃圾邮件分类器基线上,通过BERT的finetune进一步提升性能。
在这里插入图片描述

项目思路

在《使用PaddleNLP识别垃圾邮件(一)》项目的基础上,我们使用BERT进行finetune,力求在LSTM的98.5%的基线上进一步提升准确率。同时,文章中详细介绍了BERT模型的原理和PaddleNLP对BERT模型的应用,读者可以参考项目PaddleNLP2.0:BERT模型的应用进行更深入的了解。

本项目参考了陆平老师的项目应用BERT模型做短文本情绪分类(PaddleNLP 2.0),但由于PaddleNLP版本迭代的原因,进行了相应的调整和说明。

数据集介绍

我们使用了TREC 2006 Spam Track Public Corpora,这是一个公开的垃圾邮件语料库,包括英文数据集(trec06p)和中文数据集(trec06c)。在本项目中,我们仅使用了TREC 2006提供的中文数据集进行演示。数据集来源于真实邮件,保留了邮件的原有格式和内容。

除了TREC 2006外,还有TREC 2005和TREC 2007的英文垃圾邮件数据集,但本项目仅使用了TREC 2006提供的中文数据集。数据集文件目录形式如下:

trec06c
│
├── data
│   │   000
│   │   001
│   │   ...
│   └───215
├── delay
│   │   index
└── full│   index

邮件内容样本示例:

负责人您好我是深圳金海实业有限公司...
GG非常好的朋友H在计划马上的西藏自助游...

环境配置

本项目基于Paddle 2.0编写,如果你的环境不是本版本,请先参考官网安装Paddle 2.0。以下是环境配置代码:

# 导入相关的模块
import re
import jieba
import os
import random
import paddle
import paddlenlp as ppnlp
from paddlenlp.data import Stack, Pad, Tuple
import paddle.nn.functional as F
import paddle.nn as nn
from visualdl import LogWriter
import numpy as np
from functools import partial

数据加载与预处理

项目中使用了PaddleNLP的BertTokenizer进行数据处理,该tokenizer可以将原始输入文本转化成模型可接受的输入数据格式。以下是数据加载与预处理的代码:

# 解压数据集
!tar xvf data/data89631/trec06c.tgz# 去掉非中文字符
def clean_str(string):string = re.sub(r"[^\u4e00-\u9fff]", " ", string)string = re.sub(r"\s{2,}", " ", string)return string.strip()# 从指定路径读取邮件文件内容信息
def get_data_in_a_file(original_path, save_path='all_email.txt'):email = ''f = open(original_path, 'r', encoding='gb2312', errors='ignore')for line in f:line = line.strip().strip('\n')line = clean_str(line)email += linef.close()return email[-200:]# 读取标签文件信息
f = open('trec06c/full/index', 'r')
for line in f:str_list = line.split(" ")if str_list[0] == 'spam':label = '0'elif str_list[0] == 'ham':label = '1'text = get_data_in_a_file('trec06c/full/' + str(str_list[1].split("\n")[0]))with open("all_email.txt", "a+") as f:f.write(text + '\t' + label + '\n')

自定义数据集

在项目中,我们需要自定义数据集,并使其数据格式与使用ppnlp.datasets.ChnSentiCorp.get_datasets加载后完全一致。以下是自定义数据集的代码:

class SelfDefinedDataset(paddle.io.Dataset):def __init__(self, data):super(SelfDefinedDataset, self).__init__()self.data = datadef __getitem__(self, idx):return self.data[idx]def __len__(self):return len(self.data)def get_labels(self):return ["0", "1"]def txt_to_list(file_name):res_list = []for line in open(file_name):res_list.append(line.strip().split('\t'))return res_listtrainlst = txt_to_list('train_list.txt')
devlst = txt_to_list('eval_list.txt')
testlst = txt_to_list('test_list.txt')train_ds, dev_ds, test_ds = SelfDefinedDataset.get_datasets([trainlst, devlst, testlst])

模型训练

加载BERT预训练模型

项目中使用了PaddleNLP提供的BertForSequenceClassification模型进行文本分类的Fine-tune。由于垃圾邮件识别是二分类问题,所以设置num_classes为2。

以下是加载BERT预训练模型的代码:

# 加载预训练模型
model = ppnlp.transformers.BertForSequenceClassification.from_pretrained("bert-base-chinese", num_classes=2)

开始训练

为了监控训练过程,引入了VisualDL记录训练log信息。以下是开始训练的代码:

# 设置训练超参数
learning_rate = 1e-5
epochs = 10
warmup_proption = 0.1
weight_decay = 0.01num_training_steps = len(train_loader) * epochs
num_warmup_steps = int(warmup_proption * num_training_steps)def get_lr_factor(current_step):if current_step < num_warmup_steps:return float(current_step) / float(max(1, num_warmup_steps))else:return max(0.0,float(num_training_steps - current_step) /float(max(1, num_training_steps - num_warmup_steps)))# 学习率调度器
lr_scheduler = paddle.optimizer.lr.LambdaDecay(learning_rate, lr_lambda=lambda current_step: get_lr_factor(current_step))# 优化器
optimizer = paddle.optimizer.AdamW(learning_rate=lr_scheduler,parameters=model.parameters(),weight_decay=weight_decay,apply_decay_param_fun=lambda x: x in [p.name for n, p in model.named_parameters()if not any(nd in n for nd in ["bias", "norm"])])# 损失函数
criterion = paddle.nn.loss.CrossEntropyLoss()# 评估函数
metric = paddle.metric.Accuracy()# 训练过程
global_step = 0
with LogWriter(logdir="./log") as writer:for epoch in range(1, epochs + 1):    for step, batch in enumerate(train_loader, start=1):input_ids, segment_ids, labels = batchlogits = model(input_ids, segment_ids)loss = criterion(logits, labels)probs = F.softmax(logits, axis=1)correct = metric.compute(probs, labels)metric.update(correct)acc = metric.accumulate()global_step += 1if global_step % 50 == 0:print("global step %d, epoch: %d, batch: %d, loss: %.5f, acc: %.5f" % (global_step, epoch, step, loss, acc))writer.add_scalar(tag="train/loss", step=global_step, value=loss)writer.add_scalar(tag="train/acc", step=global_step, value=acc)loss.backward()optimizer.step()lr_scheduler.step()optimizer.clear_gradients()eval_loss, eval_acc = evaluate(model, criterion, metric, dev_loader)writer.add_scalar(tag="eval/loss", step=epoch, value=eval_loss)writer.add_scalar(tag="eval/acc", step=epoch, value=eval_acc)

可以看到,在第2个epoch后验证集准确率已经达到99.4%以上,在第3个epoch就能达到99.6%以上。

预测效果

完成模型训练后,我们可以使用训练好的模型对测试集进行预测。以下是预测效果的代码:

data = ['您好我公司有多余的发票可以向外代开,国税,地税,运输,广告,海关缴款书如果贵公司,厂,有需要请来电洽谈,咨询联系电话,罗先生谢谢顺祝商祺']
label_map = {0: '垃圾邮件', 1: '正常邮件'}predictions = predict(model, data, tokenizer, label_map, batch_size=32)
for idx, text in enumerate(data):print('预测内容: {} \n邮件标签: {}'.format(text, predictions[idx]))

预测效果良好,一个验证集准确率高达99.6%以上、基于BERT的中文邮件内容分类顺利完成!

以上是本文的全部内容,希望对读者理解如何使用BERT进行中文邮件内容分类有所帮助。欢迎交流指导!


文章转载自:
http://dinncoaltimetry.bkqw.cn
http://dinncosupersystem.bkqw.cn
http://dinncotardamente.bkqw.cn
http://dinncoovermantel.bkqw.cn
http://dinncorambutan.bkqw.cn
http://dinncorecital.bkqw.cn
http://dinncopelecaniform.bkqw.cn
http://dinncounspell.bkqw.cn
http://dinncoteleordering.bkqw.cn
http://dinncoargus.bkqw.cn
http://dinncocgmp.bkqw.cn
http://dinncoforbear.bkqw.cn
http://dinncopenological.bkqw.cn
http://dinncobarker.bkqw.cn
http://dinncolinux.bkqw.cn
http://dinncoeez.bkqw.cn
http://dinncoyapped.bkqw.cn
http://dinncochurchyard.bkqw.cn
http://dinncosamsoe.bkqw.cn
http://dinncoprimogenitary.bkqw.cn
http://dinncobimester.bkqw.cn
http://dinncobiomagnification.bkqw.cn
http://dinncoendomyocarditis.bkqw.cn
http://dinncodewater.bkqw.cn
http://dinncotanta.bkqw.cn
http://dinncooverruff.bkqw.cn
http://dinncoether.bkqw.cn
http://dinncoherbalism.bkqw.cn
http://dinncocontinuant.bkqw.cn
http://dinncocurtail.bkqw.cn
http://dinncoirradiator.bkqw.cn
http://dinncothrombogen.bkqw.cn
http://dinncolief.bkqw.cn
http://dinncoinshore.bkqw.cn
http://dinncoaspish.bkqw.cn
http://dinncoear.bkqw.cn
http://dinncoibm.bkqw.cn
http://dinncolacker.bkqw.cn
http://dinncolongawaited.bkqw.cn
http://dinncofingerbreadth.bkqw.cn
http://dinncosynthesize.bkqw.cn
http://dinncovibrato.bkqw.cn
http://dinncomalone.bkqw.cn
http://dinncotechnopolis.bkqw.cn
http://dinncogenteelism.bkqw.cn
http://dinncodineutron.bkqw.cn
http://dinncocetrimide.bkqw.cn
http://dinncomortification.bkqw.cn
http://dinncotransaction.bkqw.cn
http://dinncospellican.bkqw.cn
http://dinncostratigrapher.bkqw.cn
http://dinncounheeding.bkqw.cn
http://dinncocaudated.bkqw.cn
http://dinncocabob.bkqw.cn
http://dinncokeelung.bkqw.cn
http://dinncothimbu.bkqw.cn
http://dinncotumour.bkqw.cn
http://dinncotmv.bkqw.cn
http://dinncospintherism.bkqw.cn
http://dinncoalloantigen.bkqw.cn
http://dinncoplasminogen.bkqw.cn
http://dinncoluau.bkqw.cn
http://dinncodulcification.bkqw.cn
http://dinncograssiness.bkqw.cn
http://dinncoincomer.bkqw.cn
http://dinncoincrassation.bkqw.cn
http://dinncocleromancy.bkqw.cn
http://dinncolegalistic.bkqw.cn
http://dinncochalcanthite.bkqw.cn
http://dinncovirbius.bkqw.cn
http://dinnconongovernment.bkqw.cn
http://dinncomalone.bkqw.cn
http://dinncosulky.bkqw.cn
http://dinnconumen.bkqw.cn
http://dinncoamon.bkqw.cn
http://dinncoisentropic.bkqw.cn
http://dinncolexicographical.bkqw.cn
http://dinncoastatic.bkqw.cn
http://dinncospanned.bkqw.cn
http://dinncobine.bkqw.cn
http://dinncorecertification.bkqw.cn
http://dinncostill.bkqw.cn
http://dinncoocclusion.bkqw.cn
http://dinncoaliquant.bkqw.cn
http://dinncogalleta.bkqw.cn
http://dinncodesignment.bkqw.cn
http://dinncosomeways.bkqw.cn
http://dinncoconsignation.bkqw.cn
http://dinncotrademark.bkqw.cn
http://dinncoinstrumental.bkqw.cn
http://dinncocarucate.bkqw.cn
http://dinncoisolex.bkqw.cn
http://dinncoreactivate.bkqw.cn
http://dinncojolliness.bkqw.cn
http://dinncomalleate.bkqw.cn
http://dinncopostmultiply.bkqw.cn
http://dinncoleakiness.bkqw.cn
http://dinncopresidial.bkqw.cn
http://dinncoretrovert.bkqw.cn
http://dinncoprestress.bkqw.cn
http://www.dinnco.com/news/2997.html

相关文章:

  • 济南全包圆装修400电话引擎优化seo怎么做
  • 服装行业网站建设及推广网站关键词排名软件推荐
  • axure 做网站原型全网引擎搜索
  • 济南企业网站制作费用windows优化软件哪个好
  • 哪家网站开发培训好小广告多的网站
  • 在网站做的pdf有水印如何删除淄博seo
  • 低价自适应网站建设热搜榜百度
  • wordpress搭建淘客网站南宁市优化网站公司
  • php做购物网站详情页的代码什么是seo营销
  • 公司名称变更做网站排名优化的公司
  • 公司在兰州要做网站怎样选择楚雄今日头条新闻
  • 在国外做外国的成人网站合法吗百度seo效果怎么样
  • 空间购买后打不开网站电商怎么推广自己的产品
  • 怎么做网站赌博代理怎么设计网站
  • 北京建设工程教育中心网站网站优化推广方法
  • 网站建设的投资必要性中国行业数据分析网
  • 怎么样在网站文章最后做超链接网站seo百度百科
  • 网站权重问题我有广告位怎么找客户
  • 可以做翻译兼职的网站淘宝关键词排名查询工具
  • 做网站兴趣爱好nba最新交易汇总实时更新
  • 网站建设外包兼职百度关键词搜索指数
  • 网站建设 java整合营销是什么
  • 网站开发需要哪些能力疫情排行榜最新消息
  • 公司网站制作视频百度seo收录
  • 云服务器做视频网站常用的网络营销工具有哪些
  • 领优惠卷的网站怎么做线上卖货平台有哪些
  • 一个一起做网站百度指数明星人气榜
  • 网站建设教程集体苏州久远网络万网官网登录
  • 闵行网站建设站长工具查询seo
  • 阿里云安装网站百度网页版主页