Views
No views yet
bert-base-chinese and fine-tuned for sequence classification with two labels: weak (0) and strong (1) emotion. The model was trained for one epoch using a dataset of ~125k comments from Douban, a Chinese social networking service, and achieved 87.9% accuracy on a held-out test set (10k comments). The comments were at least 200-character long; those with 1 or 5 stars were labeled "strong," those with 3 stars "weak."1from transformers import BertTokenizer, BertForSequenceClassification
2import torch
3
4model_name = "qhchina/BERT-EmotionIntensity-0.1"
5tokenizer = BertTokenizer.from_pretrained(model_name)
6model = BertForSequenceClassification.from_pretrained(model_name)
7
8sentence = "到达华侨居住的地区,疯狂地捣毁华侨的商店,洗劫华侨的财物,野蛮地殴打华侨,破坏华侨的车辆和看到的一切东西。他们对华侨大抢、大烧、大杀。种种暴行,简直同当年希特勒法西斯匪徒对待犹太人,和今天南非种族主义者对待非洲人,一模一样。"
9
10inputs = tokenizer(sentence, return_tensors="pt")
11
12outputs = model(**inputs)
13logits = outputs.logits
14
15# Get the probability of the "strong emotion" class
16probability_strong_emotion = torch.nn.functional.softmax(logits, dim=-1)[0][1].item()
17
18print(f"Sentence: {sentence}")
19print(f"Probability of strong emotion: {probability_strong_emotion}")