模型:
cardiffnlp/tweet-topic-latest-single
这是一个基于RoBERTa-base的模型,训练于2022年9月底之前的1.6886亿个推文,并在6,997个语料库上进行了单标签主题分类的微调。原始的RoBERTa-base模型可以在此处找到。该模型适用于英文。
标签:
from transformers import AutoModelForSequenceClassification, TFAutoModelForSequenceClassification from transformers import AutoTokenizer import numpy as np from scipy.special import softmax MODEL = f"cardiffnlp/tweet-topic-latest-single" tokenizer = AutoTokenizer.from_pretrained(MODEL) # PT model = AutoModelForSequenceClassification.from_pretrained(MODEL) class_mapping = model.config.id2label text = "Tesla stock is on the rise!" encoded_input = tokenizer(text, return_tensors='pt') output = model(**encoded_input) scores = output[0][0].detach().numpy() scores = softmax(scores) # TF #model = TFAutoModelForSequenceClassification.from_pretrained(MODEL) #class_mapping = model.config.id2label #text = "Tesla stock is on the rise!" #encoded_input = tokenizer(text, return_tensors='tf') #output = model(**encoded_input) #scores = output[0][0] #scores = softmax(scores) ranking = np.argsort(scores) ranking = ranking[::-1] for i in range(scores.shape[0]): l = class_mapping[ranking[i]] s = scores[ranking[i]] print(f"{i+1}) {l} {np.round(float(s), 4)}")
输出:
1) business_&_entrepreneurs 0.8929 2) sports_&_gaming 0.0478 3) science_&_technology 0.0185 4) daily_life 0.0178 5) arts_&_culture 0.0128 6) pop_culture 0.0102