作者:刘健健
来自:ChallengeHub
Twitter 的推文有许多特点,首先,与 Facebook 不同的是,推文是基于文本的,可以通过 Twitter 接口注册下载,便于作为自然语言处理所需的语料库。其次,Twitter 规定了每一个推文不超过 140 个字,实际推文中的文本长短不一、长度一般较短,有些只有一个句子甚至一个短语,这对其开展情感分类标注带来许多困难。再者,推文常常是随性所作,内容中包含情感的元素较多,口语化内容居多,缩写随处都在,并且使用了许多网络用语,情绪符号、新词和俚语随处可见。因此,与正式文本非常不同。如果采用那些适合处理正式文本的情感分类方法来对 Twitter 推文进行情感分类,效果将不尽人意。
公众情感在包括电影评论、消费者信心、政治选举、股票走势预测等众多领域发挥着越来越大的影响力。面向公共媒体内容开展情感分析是分析公众情感的一项基础工作。
二、数据基本情况
数据集基于推特用户发表的推文数据集,并且针对部分字段做出了一定的调整,所有的字段信息请以本练习赛提供的字段信息为准
字段信息内容参考如下:
tweet_id string 推文数据的唯一ID,比如test_0,train_1024
content string 推特内容
label int 推特情感的类别,共13种情感
其中训练集train.csv包含3w条数据,字段包括tweet_id,content,label;测试集test.csv包含1w条数据,字段包括tweet_id,content。
tweet_id,content,label
tweet_1,Layinnbedwithaheadacheughhhh...waitinonyourcall...,1
tweet_2,Funeralceremony...gloomyfriday...,1
tweet_3,wantstohangoutwithfriendsSOON!,2
tweet_4,"@dannycastilloWewanttotradewithsomeonewhohasHoustontickets,butnoonewill.",3
tweet_5,"Ishouldbesleep,butimnot!thinkingaboutanoldfriendwhoIwant.buthe'smarriednow.damn,&hewantsme2!scandalous!",1
tweet_6,Hmmm.
http://www.djhero.com/isdown,4
tweet_7,@charvirayCharlenemylove.Imissyou,1
tweet_8,cantfallasleep,3
!head/home/mw/input/Twitter4903/train.csv
tweet_id,content,label
tweet_0,@tiffanylueiknowiwaslistenintobadhabitearlierandistartedfreakinathispart=[,0
tweet_1,Layinnbedwithaheadacheughhhh...waitinonyourcall...,1
tweet_2,Funeralceremony...gloomyfriday...,1
tweet_3,wantstohangoutwithfriendsSOON!,2
tweet_4,"@dannycastilloWewanttotradewithsomeonewhohasHoustontickets,butnoonewill.",3
tweet_5,"Ishouldbesleep,butimnot!thinkingaboutanoldfriendwhoIwant.buthe'smarriednow.damn,&hewantsme2!scandalous!",1
tweet_6,Hmmm.http://www.djhero.com/isdown,4
tweet_7,@charvirayCharlenemylove.Imissyou,1
tweet_8,cantfallasleep,3
!head/home/mw/input/Twitter4903/test.csv
tweet_id,content
tweet_0,Re-pinging@ghostridah14:whydidn'tyougotoprom?BCmybfdidn'tlikemyfriends
tweet_1,@kelcouchI'msorryatleastit'sFriday?
tweet_2,Thestormishereandtheelectricityisgone
tweet_3,Sosleepyagainandit'snoteventhatlate.Ifailonceagain.
tweet_4,"WonderingwhyI'mawakeat7am,writinganewsong,plottingmyevilsecretplotsmuahahaha...ohdamnit,notsecretanymore"
tweet_5,IateSomethingIdon'tknowwhatitis...WhydoIkeepTellingthingsaboutfood
tweet_6,sotiredandithinki'mdefinitelygoingtogetanearinfection.goingtobed"early"foronce.
tweet_7,Itissoannoyingwhenshestartstypingonhercomputerinthemiddleofthenight!
tweet_8,Screwyou@davidbrussee!Ionlyhave3weeks...
!head/home/mw/input/Twitter4903/submission.csv
tweet_id,label
tweet_0,0
tweet_1,0
tweet_2,0
tweet_3,0
tweet_4,0
tweet_5,0
tweet_6,0
tweet_7,0
tweet_8,0
三、数据集定义
1.环境准备
环境准备 (建议gpu环境,速度好。pip install paddlepaddle-gpu)
!pipinstallpaddlepaddle
!pipinstall-Upaddlenlp
2.获取句子最大长度
自定义PaddleNLP dataset的read方法
importpandasaspd
train=pd.read_csv('/home/mw/input/Twitter4903/train.csv')
test=pd.read_csv('/home/mw/input/Twitter4903/test.csv')
sub=pd.read_csv('/home/mw/input/Twitter4903/submission.csv')
print('最大内容长度%d'%(max(train['content'].str.len())))
最大内容长度 166
3.定义数据集
定义读取函数
defread(pd_data):
forindex,iteminpd_data.iterrows():
yield{'text':item['content'],'label':item['label'],'qid':item['tweet_id'].strip('tweet_')}
分割训练集、测试机
frompaddle.ioimportDataset,Subset
frompaddlenlp.datasetsimportMapDataset
frompaddlenlp.datasetsimportload_dataset
dataset=load_dataset(read,pd_data=train,lazy=False)
dev_ds=Subset(dataset=dataset,indices=[iforiinrange(len(dataset))ifi%5==1])
train_ds=Subset(dataset=dataset,indices=[iforiinrange(len(dataset))ifi%5!=1])
查看训练集
foriinrange(5):
print(train_ds[i])
{'text':'@tiffanylueiknowiwaslistenintobadhabitearlierandistartedfreakinathispart=[','label':0,'qid':'0'}
{'text':'Funeralceremony...gloomyfriday...','label':1,'qid':'2'}
{'text':'wantstohangoutwithfriendsSOON!','label':2,'qid':'3'}
{'text':'@dannycastilloWewanttotradewithsomeonewhohasHoustontickets,butnoonewill.','label':3,'qid':'4'}
{'text':"Ishouldbesleep,butimnot!thinkingaboutanoldfriendwhoIwant.buthe'smarriednow.damn,&hewantsme2!scandalous!",'label':1,'qid':'5'}
在转换为MapDataset类型
train_ds=MapDataset(train_ds)
dev_ds=MapDataset(dev_ds)
print(len(train_ds))
print(len(dev_ds))
240006000
四、模型选择
近年来,大量的研究表明基于大型语料库的预训练模型(Pretrained Models, PTM)可以学习通用的语言表示,有利于下游NLP任务,同时能够避免从零开始训练模型。随着计算能力的发展,深度模型的出现(即 Transformer)和训练技巧的增强使得 PTM 不断发展,由浅变深。
情感预训练模型SKEP(Sentiment Knowledge Enhanced Pre-training for Sentiment Analysis)。SKEP利用情感知识增强预训练模型, 在14项中英情感分析典型任务上全面超越SOTA,此工作已经被ACL 2020录用。SKEP是百度研究团队提出的基于情感知识增强的情感预训练算法,此算法采用无监督方法自动挖掘情感知识,然后利用情感知识构建预训练目标,从而让机器学会理解情感语义。SKEP为各类情感分析任务提供统一且强大的情感语义表示。
百度研究团队在三个典型情感分析任务,句子级情感分类(Sentence-level Sentiment Classification),评价目标级情感分类(Aspect-level Sentiment Classification)、观点抽取(Opinion Role Labeling),共计14个中英文数据上进一步验证了情感预训练模型SKEP的效果。
具体实验效果参考:https://github.com/baidu/Senta#skep
PaddleNLP已经实现了SKEP预训练模型,可以通过一行代码实现SKEP加载。
句子级情感分析模型是SKEP fine-tune 文本分类常用模型SkepForSequenceClassification。其首先通过SKEP提取句子语义特征,之后将语义特征进行分类。
!pipinstallregex
Lookinginindexes:https://mirror.baidu.com/pypi/simple/
Requirementalreadysatisfied:regexin/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages(2021.8.28)
1.Skep模型加载
SkepForSequenceClassification可用于句子级情感分析和目标级情感分析任务。其通过预训练模型SKEP获取输入文本的表示,之后将文本表示进行分类。
pretrained_model_name_or_path:模型名称。支持"skep_ernie_1.0_large_ch",“skep_ernie_2.0_large_en”。
** “skep_ernie_1.0_large_ch”:是SKEP模型在预训练ernie_1.0_large_ch基础之上在海量中文数据上继续预训练得到的中文预训练模型;
“skep_ernie_2.0_large_en”:是SKEP模型在预训练ernie_2.0_large_en基础之上在海量英文数据上继续预训练得到的英文预训练模型;
num_classes: 数据集分类类别数。
关于SKEP模型实现详细信息参考:https://github.com/PaddlePaddle/PaddleNLP/tree/develop/paddlenlp/transformers/skep
from paddlenlp.transformers import SkepForSequenceClassification, SkepTokenizer
指定模型名称,一键加载模型
model=SkepForSequenceClassification.from_pretrained(pretrained_model_name_or_path="skep_ernie_2.0_large_en",num_classes=13)
同样地,通过指定模型名称一键加载对应的Tokenizer,用于处理文本数据,如切分token,转token_id等。
tokenizer=SkepTokenizer.from_pretrained(pretrained_model_name_or_path="skep_ernie_2.0_large_en")
[2021-09-161058,665][INFO]-Alreadycached/home/aistudio/.paddlenlp/models/skep_ernie_2.0_large_en/skep_ernie_2.0_large_en.pdparams
[2021-09-161010,133][INFO]-Found/home/aistudio/.paddlenlp/models/skep_ernie_2.0_large_en/skep_ernie_2.0_large_en.vocab.txt
2.引入可视化VisualDl
fromvisualdlimportLogWriter
writer=LogWriter("./log")
3.数据处理
SKEP模型对文本处理按照字粒度进行处理,我们可以使用PaddleNLP内置的SkepTokenizer完成一键式处理。
defconvert_example(example,
tokenizer,
max_seq_length=512,
is_test=False):
#将原数据处理成model可读入的格式,enocded_inputs是一个dict,包含input_ids、token_type_ids等字段
encoded_inputs=tokenizer(
text=example["text"],max_seq_len=max_seq_length)
# input_ids:对文本切分token后,在词汇表中对应的token id
input_ids=encoded_inputs["input_ids"]
# token_type_ids:当前token属于句子1还是句子2,即上述图中表达的segment ids
token_type_ids=encoded_inputs["token_type_ids"]
ifnotis_test:
# label:情感极性类别
label=np.array([example["label"]],dtype="int64")
returninput_ids,token_type_ids,label
else:
# qid:每条数据的编号
qid=np.array([example["qid"]],dtype="int64")
returninput_ids,token_type_ids,qid
defcreate_dataloader(dataset,
trans_fn=None,
mode='train',
batch_size=1,
batchify_fn=None):
iftrans_fn:
dataset=dataset.map(trans_fn)
shuffle=Trueifmode=='train'elseFalse
ifmode=="train":
sampler=paddle.io.DistributedBatchSampler(
dataset=dataset,batch_size=batch_size,shuffle=shuffle)
else:
sampler=paddle.io.BatchSampler(
dataset=dataset,batch_size=batch_size,shuffle=shuffle)
dataloader=paddle.io.DataLoader(
dataset,batch_sampler=sampler,collate_fn=batchify_fn)
returndataloader
4.评估函数定义
importnumpyasnp
importpaddle
@paddle.no_grad()
defevaluate(model,criterion,metric,data_loader):
model.eval()
metric.reset()
losses=[]
forbatchindata_loader:
input_ids,token_type_ids,labels=batch
logits=model(input_ids,token_type_ids)
loss=criterion(logits,labels)
losses.append(loss.numpy())
correct=metric.compute(logits,labels)
metric.update(correct)
accu=metric.accumulate()
#print("evalloss:%.5f,accu:%.5f"%(np.mean(losses),accu))
model.train()
metric.reset()
returnnp.mean(losses),accu
5.超参定义
定义损失函数、优化器以及评价指标后,即可开始训练。
推荐超参设置:
batch_size=100
max_seq_length=166
batch_size=100
learning_rate=4e-5
epochs=32
warmup_proportion=0.1
weight_decay=0.01
实际运行时可以根据显存大小调整batch_size和max_seq_length大小。
importos
fromfunctoolsimportpartial
importnumpyasnp
importpaddle
importpaddle.nn.functionalasF
frompaddlenlp.dataimportStack,Tuple,Pad
#批量数据大小
batch_size=100
#文本序列最大长度166
max_seq_length=166
#批量数据大小
batch_size=100
#定义训练过程中的最大学习率
learning_rate=4e-5
#训练轮次
epochs=32
#学习率预热比例
warmup_proportion=0.1
#权重衰减系数,类似模型正则项策略,避免模型过拟合
weight_decay=0.01
将数据处理成模型可读入的数据格式
trans_func=partial(
convert_example,
tokenizer=tokenizer,
max_seq_length=max_seq_length)
将数据组成批量式数据,如将不同长度的文本序列padding到批量式数据中最大长度将每条数据label堆叠在一起
batchify_fn=lambdasamples,fn=Tuple(
Pad(axis=0,pad_val=tokenizer.pad_token_id),#input_ids
Pad(axis=0,pad_val=tokenizer.pad_token_type_id),#token_type_ids
Stack()#labels
):[datafordatainfn(samples)]
train_data_loader=create_dataloader(
train_ds,
mode='train',
batch_size=batch_size,
batchify_fn=batchify_fn,
trans_fn=trans_func)
dev_data_loader=create_dataloader(
dev_ds,
mode='dev',
batch_size=batch_size,
batchify_fn=batchify_fn,
trans_fn=trans_func)
定义超参,loss,优化器等
frompaddlenlp.transformersimportLinearDecayWithWarmup
importtime
num_training_steps=len(train_data_loader)*epochs
lr_scheduler=LinearDecayWithWarmup(learning_rate,num_training_steps,warmup_proportion)
AdamW优化器
optimizer=paddle.optimizer.AdamW(
learning_rate=lr_scheduler,
parameters=model.parameters(),
weight_decay=weight_decay,
apply_decay_param_fun=lambdax:xin[
p.nameforn,pinmodel.named_parameters()
ifnotany(ndinnforndin["bias","norm"])
])
criterion=paddle.nn.loss.CrossEntropyLoss()#交叉熵损失函数
metric=paddle.metric.Accuracy()#accuracy评价指标
五、训练
训练且保存最佳结果
开启训练
global_step=0
best_val_acc=0
tic_train=time.time()
best_accu=0
forepochinrange(1,epochs+1):
forstep,batchinenumerate(train_data_loader,start=1):
input_ids,token_type_ids,labels=batch
#喂数据给model
logits=model(input_ids,token_type_ids)
#计算损失函数值
loss=criterion(logits,labels)
#预测分类概率值
probs=F.softmax(logits,axis=1)
#计算acc
correct=metric.compute(probs,labels)
metric.update(correct)
acc=metric.accumulate()
global_step+=1
ifglobal_step%10==0:
print(
"globalstep%d,epoch:%d,batch:%d,loss:%.5f,accu:%.5f,speed:%.2fstep/s"
%(global_step,epoch,step,loss,acc,
10/(time.time()-tic_train)))
tic_train=time.time()
#反向梯度回传,更新参数
loss.backward()
optimizer.step()
lr_scheduler.step()
optimizer.clear_grad()
ifglobal_step%100==0and:
#评估当前训练的模型
eval_loss,eval_accu=evaluate(model,criterion,metric,dev_data_loader)
print("evalondevloss:{:.8},accu:{:.8}".format(eval_loss,eval_accu))
#加入eval日志显示
writer.add_scalar(tag="eval/loss",step=global_step,value=eval_loss)
writer.add_scalar(tag="eval/acc",step=global_step,value=eval_accu)
#加入train日志显示
writer.add_scalar(tag="train/loss",step=global_step,value=loss)
writer.add_scalar(tag="train/acc",step=global_step,value=acc)
save_dir="best_checkpoint"
#加入保存
ifeval_accu>best_val_acc:
ifnotos.path.exists(save_dir):
os.mkdir(save_dir)
best_val_acc=eval_accu
print(f"模型保存在{global_step}步,最佳eval准确度为{best_val_acc:.8f}!")
save_param_path=os.path.join(save_dir,'best_model.pdparams')
paddle.save(model.state_dict(),save_param_path)
fh=open('best_checkpoint/best_model.txt','w',encoding='utf-8')
fh.write(f"模型保存在{global_step}步,最佳eval准确度为{best_val_acc:.8f}!")
fh.close()
globalstep10,epoch:1,batch:10,loss:2.64415,accu:0.08400,speed:0.96step/s
globalstep20,epoch:1,batch:20,loss:2.48083,accu:0.09050,speed:0.98step/s
globalstep30,epoch:1,batch:30,loss:2.36845,accu:0.10933,speed:0.98step/s
globalstep40,epoch:1,batch:40,loss:2.24933,accu:0.13750,speed:1.00step/s
globalstep50,epoch:1,batch:50,loss:2.14947,accu:0.15380,speed:0.97step/s
globalstep60,epoch:1,batch:60,loss:2.03459,accu:0.17100,speed:0.96step/s
globalstep70,epoch:1,batch:70,loss:2.23222,accu:0.18414,speed:1.01step/s
visualdl 可视化训练,时刻掌握训练走势,不浪费算力
六、预测
训练完成后,重启环境,释放显存,开始预测
1.test数据集读取
数据读取
importpandasaspd
frompaddlenlp.datasetsimportload_dataset
frompaddle.ioimportDataset,Subset
frompaddlenlp.datasetsimportMapDataset
test=pd.read_csv('/home/mw/input/Twitter4903/test.csv')
数据读取
defread_test(pd_data):
forindex,iteminpd_data.iterrows():
yield{'text':item['content'],'label':0,'qid':item['tweet_id'].strip('tweet_')}
test_ds=load_dataset(read_test,pd_data=test,lazy=False)
#在转换为MapDataset类型
test_ds=MapDataset(test_ds)
print(len(test_ds))
defconvert_example(example,
tokenizer,
max_seq_length=512,
is_test=False):
#将原数据处理成model可读入的格式,enocded_inputs是一个dict,包含input_ids、token_type_ids等字段
encoded_inputs=tokenizer(
text=example["text"],max_seq_len=max_seq_length)
# input_ids:对文本切分token后,在词汇表中对应的token id
input_ids=encoded_inputs["input_ids"]
# token_type_ids:当前token属于句子1还是句子2,即上述图中表达的segment ids
token_type_ids=encoded_inputs["token_type_ids"]
ifnotis_test:
# label:情感极性类别
label=np.array([example["label"]],dtype="int64")
returninput_ids,token_type_ids,label
else:
# qid:每条数据的编号
qid=np.array([example["qid"]],dtype="int64")
returninput_ids,token_type_ids,qid
defcreate_dataloader(dataset,
trans_fn=None,
mode='train',
batch_size=1,
batchify_fn=None):
iftrans_fn:
dataset=dataset.map(trans_fn)
shuffle=Trueifmode=='train'elseFalse
ifmode=="train":
sampler=paddle.io.DistributedBatchSampler(
dataset=dataset,batch_size=batch_size,shuffle=shuffle)
else:
sampler=paddle.io.BatchSampler(
dataset=dataset,batch_size=batch_size,shuffle=shuffle)
dataloader=paddle.io.DataLoader(
dataset,batch_sampler=sampler,collate_fn=batchify_fn)
returndataloader
2.模型加载
frompaddlenlp.transformersimportSkepForSequenceClassification,SkepTokenizer
指定模型名称,一键加载模型
model=SkepForSequenceClassification.from_pretrained(pretrained_model_name_or_path="skep_ernie_2.0_large_en",num_classes=13)
同样地,通过指定模型名称一键加载对应的Tokenizer,用于处理文本数据,如切分token,转token_id等。
tokenizer=SkepTokenizer.from_pretrained(pretrained_model_name_or_path="skep_ernie_2.0_large_en")
fromfunctoolsimportpartial
importnumpyasnp
importpaddle
importpaddle.nn.functionalasF
frompaddlenlp.dataimportStack,Tuple,Pad
batch_size=16
max_seq_length=166
#处理测试集数据
trans_func=partial(
convert_example,
tokenizer=tokenizer,
max_seq_length=max_seq_length,
is_test=True)
batchify_fn=lambdasamples,fn=Tuple(
Pad(axis=0,pad_val=tokenizer.pad_token_id),#input
Pad(axis=0,pad_val=tokenizer.pad_token_type_id),#segment
Stack()#qid
):[datafordatainfn(samples)]
test_data_loader=create_dataloader(
test_ds,
mode='test',
batch_size=batch_size,
batchify_fn=batchify_fn,
trans_fn=trans_func)
加载模型
importos
#根据实际运行情况,更换加载的参数路径
params_path='best_checkpoint/best_model.pdparams'
ifparams_pathandos.path.isfile(params_path):
#加载模型参数
state_dict=paddle.load(params_path)
model.set_dict(state_dict)
print("Loadedparametersfrom%s"%params_path)
3.数据预测
results=[]
#切换model模型为评估模式,关闭dropout等随机因素
model.eval()
forbatchintest_data_loader:
input_ids,token_type_ids,qids=batch
#喂数据给模型
logits=model(input_ids,token_type_ids)
#预测分类
probs=F.softmax(logits,axis=-1)
idx=paddle.argmax(probs,axis=1).numpy()
idx=idx.tolist()
qids=qids.numpy().tolist()
results.extend(zip(qids,idx))
4.保存并提交
#写入预测结果,提交
withopen("submission.csv",'w',encoding="utf-8")asf:
#f.write("数据ID,评分
")
f.write("tweet_id,label
")
for(idx,label)inresults:
f.write('tweet_'+str(idx[0])+","+str(label)+"
")
七、注意事项
- 1.使用pandas读取平面文件相对方便
- 2.max_seq_length用pandas统计最大值出来较为合适
- 3.用pandas可以分析数据分布
- 4.PaddleNLP在自然语言处理方面,有特别多的积累,特别方便,可上github了解
八、PaddleNLP是什么?
1.gitee地址
https://gitee.com/paddlepaddle/PaddleNLP/blob/develop/README.md
2.简介
PaddleNLP 2.0是飞桨生态的文本领域核心库,具备易用的文本领域API,多场景的应用示例、和高性能分布式训练三大特点,旨在提升开发者文本领域的开发效率,并提供基于飞桨2.0核心框架的NLP任务最佳实践。
基于飞桨核心框架领先的自动混合精度优化策略,结合分布式Fleet API,支持4D混合并行策略,可高效地完成超大规模参数的模型训练。-
nlp
+关注
关注
1文章
464浏览量
21855 -
paddle
+关注
关注
0文章
4浏览量
1994
原文标题:八、PaddleNLP是什么?
文章出处:【微信号:zenRRan,微信公众号:深度学习自然语言处理】欢迎添加关注!文章转载请注明出处。
发布评论请先 登录
相关推荐
评论