首页 > 解决方案 > Google Colab 中的 BERT 多类文本分类

问题描述

我正在研究一组社交媒体评论(包括 youtube 链接)作为输入功能,并将 Myers-Biggs 人格档案作为目标标签:

    type    posts
0   INFJ    'http://www.youtube.com/watch?v=qsXHcwe3krw|||...
1   ENTP    'I'm finding the lack of me in these posts ver...
2   INTP    'Good one _____ https://www.youtube.com/wat...
3   INTJ    'Dear INTP, I enjoyed our conversation the o...
4   ENTJ    'You're fired.|||That's another silly misconce...

但据我发现,BERT 希望 DataFrame 采用这种格式:

a   label   posts
0   a   8   'http://www.youtube.com/watch?v=qsXHcwe3krw|||...
1   a   3   'I'm finding the lack of me in these posts ver...
2   a   11  'Good one _____ https://www.youtube.com/wat...
3   a   10  'Dear INTP, I enjoyed our conversation the o...
4   a   2   'You're fired.|||That's another silly misconce...

生成的输出必须是对分成四列的测试评论集的预测,其中每一列用于每个人格档案,例如,'Mind' = 1 是 Extrovert 的标签。基本上将像 INFJ 这样的类型分为“Mind”、“Energy”、“Nature”、“Tactics”,如下所示:

    type    post    Mind    Energy  Nature  Tactics
0   INFJ    'url-web    0   1   0   1
1   INFJ    url-web 0   1   0   1
2   INFJ    enfp and intj moments url-web sportscenter n... 0   1   0   1
3   INFJ    What has been the most life-changing experienc...   0   1   0   1
4   INFJ    url-web url-web On repeat for most of today.    0   1   0   1

我已经使用以下方法安装了 pytorch-pretrained-bert:

!pip install pytorch-pretrained-bert

我已导入模型并尝试使用以下方法标记“帖子”列:

import torch
from pytorch_pretrained_bert import BertTokenizer, BertModel, BertForMaskedLM

tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')

tokenized_train = tokenizer.tokenize(train)

但收到此错误:

TypeError: ord() expected a character, but string of length 5 found

我根据 pytorch-pretrained-bert GitHub Repo 和 Youtube 视频进行了尝试。

我是一名数据科学实习生,完全没有深度学习经验。我只是想以最简单的方式来试验 BERT 模型来预测多类分类输出,这样我就可以将结果与我们目前正在研究的更简单的文本分类模型进行比较。我在 Google Colab 工作,结果输出应该是 .csv 文件。

我知道这是一个复杂的模型,并且围绕模型的所有文档和示例都很复杂(微调层等),但是对于具有最少软件的初学者数据科学家来说,对于简单实现(如果实际上有这样的事情)的任何帮助工程经验,将不胜感激。

标签: pythonpytorchdata-sciencegoogle-colaboratorybert-language-model

解决方案


我建议你从一个简单的 BERT 分类任务开始,例如遵循这个优秀的教程:https ://mccormickml.com/2019/07/22/BERT-fine-tuning/

然后您可以通过以下方式进入多标签:https ://medium.com/huggingface/multi-label-text-classification-using-bert-the-mighty-transformer-69714fa3fb3d

只有这样,我才会建议您在自己的数据集上尝试您的任务。


推荐阅读