language: tr
土耳其语文本分类
该模型是基于https://github.com/stefan-it/turkish-bert微调的文本分类模型,包含以下7个类别:
code_to_label={
'LABEL_0': '世界',
'LABEL_1': '经济',
'LABEL_2': '文化',
'LABEL_3': '健康',
'LABEL_4': '政治',
'LABEL_5': '体育',
'LABEL_6': '科技'}
引用
如需引用请参考以下论文
@misc{yildirim2024finetuning,
title={针对土耳其语理解任务的基于Transformer编码器的微调研究},
author={Savas Yildirim},
year={2024},
eprint={2401.17396},
archivePrefix={arXiv},
primaryClass={cs.CL}
}
@book{yildirim2021mastering,
title={精通Transformer:用先进自然语言处理技术从零构建尖端模型},
author={Yildirim, Savas and Asgari-Chenaghlu, Meysam},
year={2021},
publisher={Packt Publishing Ltd}
}
数据
微调使用了以下土耳其语基准数据集
https://www.kaggle.com/savasy/ttc4900
快速开始
首先安装transformers库
pip install transformers
# 代码示例:
# 导入库
from transformers import pipeline, AutoModelForTokenClassification, AutoTokenizer, AutoModelForSequenceClassification
tokenizer= AutoTokenizer.from_pretrained("savasy/bert-turkish-text-classification")
# 构建并加载模型(加载时间取决于网络状况)
model= AutoModelForSequenceClassification.from_pretrained("savasy/bert-turkish-text-classification")
# 创建管道
nlp=pipeline("sentiment-analysis", model=model, tokenizer=tokenizer)
# 应用模型
nlp("示例文本")
# 输出示例: [{'label': 'LABEL_2', 'score': 0.4753005802631378}]
code_to_label={
'LABEL_0': '世界',
'LABEL_1': '经济',
'LABEL_2': '文化',
'LABEL_3': '健康',
'LABEL_4': '政治',
'LABEL_5': '体育',
'LABEL_6': '科技'}
code_to_label[nlp("示例文本")[0]['label']]
# 输出结果: '文化'
模型训练过程
## 加载土耳其语文本分类数据
import pandas as pd
# 数据集来源: https://www.kaggle.com/savasy/ttc4900
df=pd.read_csv("7allV03.csv")
df.columns=["labels","text"]
df.labels=pd.Categorical(df.labels)
traind_df=...
eval_df=...
# 模型配置
from simpletransformers.classification import ClassificationModel
import torch,sklearn
model_args = {
"use_early_stopping": True,
"early_stopping_delta": 0.01,
"early_stopping_metric": "mcc",
"early_stopping_metric_minimize": False,
"early_stopping_patience": 5,
"evaluate_during_training_steps": 1000,
"fp16": False,
"num_train_epochs":3
}
model = ClassificationModel(
"bert",
"dbmdz/bert-base-turkish-cased",
use_cuda=cuda_available,
args=model_args,
num_labels=7
)
model.train_model(train_df, acc=sklearn.metrics.accuracy_score)
其他训练模型请参考 https://simpletransformers.ai/
土耳其语文本分类的详细使用教程请查看Python笔记本