美文网首页
simpletransformers 实现文本分类

simpletransformers 实现文本分类

作者: 三方斜阳 | 来源:发表于2021-05-19 22:07 被阅读0次

本文主要是基于英文垃圾信息分类比赛,讲解如何 通过几行代码简单使用simpletransformers 调用预训练模型实现分类任务及其他。
数据集下载:https://static.leiphone.com/sms_spam.zip

数据处理

  • 将数据读入数组 sentence 保存,label 通过 LabelBinarizer 进行标签二值化处理,也就是转换为 0/1,0代表不是垃圾信息,1代表是垃圾信息
from sklearn.preprocessing import LabelBinarizer
from simpletransformers.classification import ClassificationModel
encoder = LabelBinarizer()
label,sentence=[],[]
with open('sms_train.txt','r',encoding='utf-8') as Inp:
    for line in Inp:
            head=line.strip().split('\t',1)
            label.append(head[0])
            sentence.append(head[1])
    target = encoder.fit_transform(label)
    target=[int(item) for item in target]
>>
target:[0, 0, 1, 0, 0, 0, 0, 0, 0, 0]
  • 将数据sentence 和 target进行对应,并且读入 test 数据:
dic1 = {'label': sentence, 'labels':target}
train = pd.DataFrame(dic1)
test=[]
with open('sms_test.txt','r',encoding='utf-8') as Inp:
   for line in Inp:
       test.append(line.strip())

导入模型

#导入分类模型
from simpletransformers.classification import ClassificationModel
model = ClassificationModel('roberta', 'roberta-base', num_labels=2)
model.train_model(train)
predictions, _ = model.predict(test)
pd.DataFrame({'ID':[i for i in range(len(predictions))], 'labels': predictions}).to_csv('submission.csv', index=False, header=False)
  • 目前 simpletransformers 封装实现了以下任务模型,使用细节见:Simple Transformers
simpletransformers
  • 模型部分:
  1. 这将创建一个TransformerModel,用于训练,评估和预测。第一个参数是model_type,第二个参数是model_name,第三个参数是数据中的标签数
  2. model_type可以是['bert','xlnet','xlm','roberta','distilbert']之一
from simpletransformers.model import ClassificationModel
# Create a TransformerModel
model = ClassificationModel('roberta', 'roberta-base', num_labels=3)
  1. 也可以加载以前保存的模型,而不是默认模型的模型,将model_name更改为包含已保存模型的目录的路径。
model = TransformerModel('xlnet', 'path_to_model/', num_labels=4)
  1. ClassificationArgs传递模型参数:
from simpletransformers.classification import ClassificationModel, ClassificationArgs

model_args = ClassificationArgs()
model_args.num_train_epochs = 5
model_args.learning_rate = 1e-4
model = ClassficationModel("bert", "bert-base-cased", args=model_args)

使用另一种格式

from simpletransformers.classification import ClassificationModel

model_args = {
    "num_train_epochs": 5,
    "learning_rate": 1e-4,
}
model = ClassficationModel("bert", "bert-base-cased", args=model_args)
  1. 定义好模型之后只需要一行代码开启train/evaluate/test:
# Train the model
model.train_model(train_df)

# Evaluate the model
result, model_outputs, wrong_predictions = model.eval_model(eval_df)

# Make predictions with the model
predictions, raw_outputs = model.predict(test_df)

更多细节和其他模型用法查看官方文档:github:https://github.com/ThilinaRajapakse/simpletransformers

相关文章

  • simpletransformers 实现文本分类

    本文主要是基于英文垃圾信息分类比赛,讲解如何 通过几行代码简单使用simpletransformers 调用预训练...

  • 2018-06-29

    python实现文本分类 - CSDN博客; 根据这个实现分类吧; 编码问题真的很烦;彻底搞懂Python的字符编...

  • 分类算法-朴素贝叶斯分类器

    前言 此程序基于新闻文本分类实验 使用朴素贝叶斯(Naive Bayes Classifier)模型实现分类任务。...

  • 基于Tensorflow2.0 Keras简单实现Attenti

    背景:文本分类,我们项目中自己标注了一些语句文本,希望将来可以自动对语句实现分类功能 最早的模型就是简单的bert...

  • 短文本分类在商品分类下的应用

    最近发现从电商网站获取到的商品,有一些是没有分类信息的想到用商品标题文本解析,用文本分类的方法来初步实现未分类商品...

  • iOS 修改 UIAlertController 内部文本对齐方

    实现AlertController内文本任意对其方式. 给UIAlertController添加分类 .h文件 ....

  • NLP之文本分类

    前言 作为NLP领域最经典的使用场景之一,文本分类积累了许多的实现方法。这里我们根据是否使用深度学习方法将文本分类...

  • 中文文本分类对比(经典方法和CNN)

    背景介绍 笔者实验室项目正好需要用到文本分类,作为NLP领域最经典的场景之一,文本分类积累了大量的技术实现方法,如...

  • The Basic Concepts of Summarizat

    文本摘要技术是利用计算机自动实现文本分析、内容归纳和摘要自动生成的技术。 文本自动摘要的基本分类 文本自动摘要的分...

  • 文本分类V1

    outline 什么是文本分类 特征工程+分类器 TextCNN 什么是文本分类 文本分类是自然语言处理的一个基本...

网友评论

      本文标题:simpletransformers 实现文本分类

      本文链接:https://www.haomeiwen.com/subject/fsrmjltx.html