语言:土耳其语
数据集:interpress_news_category_tr
INTERPRESS新闻分类
数据集
该数据集下载自interpress,属于真实世界数据。原始数据量为273K条,经筛选后使用108K条数据训练模型。更多数据集信息请访问此链接。
模型
模型在训练数据和验证数据上的准确率达97%。数据按80%训练集和20%验证集划分,结果如下所示:
分类报告

混淆矩阵

PyTorch调用方式
pip install transformers 或 pip install transformers==4.3.3
from transformers import AutoTokenizer, AutoModelForSequenceClassification
tokenizer = AutoTokenizer.from_pretrained("serdarakyol/interpress-turkish-news-classification")
model = AutoModelForSequenceClassification.from_pretrained("serdarakyol/interpress-turkish-news-classification")
import torch
if torch.cuda.is_available():
device = torch.device("cuda")
model = model.cuda()
print('检测到%d块可用GPU。' % torch.cuda.device_count())
print('GPU型号:', torch.cuda.get_device_name(0))
else:
print('未检测到GPU,将使用CPU运行。')
device = torch.device("cpu")
import numpy as np
def prediction(news):
news=[news]
indices=tokenizer.batch_encode_plus(
news,
max_length=512,
add_special_tokens=True,
return_attention_mask=True,
padding='max_length',
truncation=True,
return_tensors='pt')
inputs = indices["input_ids"].clone().detach().to(device)
masks = indices["attention_mask"].clone().detach().to(device)
with torch.no_grad():
output = model(inputs, token_type_ids=None,attention_mask=masks)
logits = output[0]
logits = logits.detach().cpu().numpy()
pred = np.argmax(logits,axis=1)[0]
return pred
news = r"美国宣布不对萨勒曼王储实施制裁 白宫发言人普萨基辩称不制裁穆罕默德·本·萨勒曼是\"正确决定\"。普萨基表示:\"我国历史上,无论是民主党还是共和党执政时期,都未曾对建交国家的领导人实施过制裁。\""
新闻原文参见此链接(发布日期:2021年3月2日)
labels = {
0 : "文化艺术",
1 : "经济",
2 : "政治",
3 : "教育",
4 : "国际",
5 : "体育",
6 : "科技",
7 : "杂志",
8 : "健康",
9 : "时事"
}
pred = prediction(news)
print(labels[pred])
TensorFlow调用方式
pip install transformers 或 pip install transformers==4.3.3
import tensorflow as tf
from transformers import BertTokenizer, TFBertForSequenceClassification
import numpy as np
tokenizer = BertTokenizer.from_pretrained('serdarakyol/interpress-turkish-news-classification')
model = TFBertForSequenceClassification.from_pretrained("serdarakyol/interpress-turkish-news-classification")
inputs = tokenizer(news, return_tensors="tf")
inputs["labels"] = tf.reshape(tf.constant(1), (-1, 1))
outputs = model(inputs)
loss = outputs.loss
logits = outputs.logits
pred = np.argmax(logits,axis=1)[0]
labels[pred]
特别感谢@yavuzkomecoglu的贡献
如有任何疑问,欢迎通过以下方式联系我:

