Commit
·
0156ad2
1
Parent(s):
05b89f3
Create README.md
Browse files
README.md
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
language: ["ru"]
|
| 3 |
+
tags:
|
| 4 |
+
- russian
|
| 5 |
+
- classification
|
| 6 |
+
- sentiment
|
| 7 |
+
- multiclass
|
| 8 |
+
widget:
|
| 9 |
+
- text: "Какая гадость эта ваша заливная рыба!"
|
| 10 |
+
---
|
| 11 |
+
This is the [cointegrated/rubert-tiny](https://huggingface.co/cointegrated/rubert-tiny) model fine-tuned for classification of sentiment for short Russian texts.
|
| 12 |
+
|
| 13 |
+
The problem is formulated as multiclass classification: `negative` vs `neutral` vs `positive`.
|
| 14 |
+
## Usage
|
| 15 |
+
|
| 16 |
+
The function below estimates the sentiment of the given text:
|
| 17 |
+
```python
|
| 18 |
+
# !pip install transformers sentencepiece --quiet
|
| 19 |
+
import torch
|
| 20 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 21 |
+
|
| 22 |
+
model_checkpoint = 'cointegrated/rubert-tiny-sentiment-balanced'
|
| 23 |
+
tokenizer = AutoTokenizer.from_pretrained(model_checkpoint)
|
| 24 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_checkpoint)
|
| 25 |
+
if torch.cuda.is_available():
|
| 26 |
+
model.cuda()
|
| 27 |
+
|
| 28 |
+
def get_sentiment(text, return_type='label'):
|
| 29 |
+
""" Calculate sentiment of a text. `return_type` can be 'label', 'score' or 'proba' """
|
| 30 |
+
with torch.no_grad():
|
| 31 |
+
inputs = tokenizer(text, return_tensors='pt', truncation=True, padding=True).to(model.device)
|
| 32 |
+
proba = torch.sigmoid(model(**inputs).logits).cpu().numpy()[0]
|
| 33 |
+
if return_type == 'label':
|
| 34 |
+
return model.config.id2label[proba.argmax()]
|
| 35 |
+
elif return_type == 'score':
|
| 36 |
+
return proba.dot([-1, 0, 1])
|
| 37 |
+
return proba
|
| 38 |
+
|
| 39 |
+
text = 'Какая гадость эта ваша заливная рыба!'
|
| 40 |
+
# classify the text
|
| 41 |
+
print(get_sentiment(text, 'label')) # negative
|
| 42 |
+
# score the text on the scale from -1 (very negative) to +1 (very positive)
|
| 43 |
+
print(get_sentiment(text, 'score')) # -0.5894946306943893
|
| 44 |
+
# calculate probabilities of all labels
|
| 45 |
+
print(get_sentiment(text, 'proba')) # [0.7870447 0.4947824 0.19755007]
|
| 46 |
+
```
|
| 47 |
+
|
| 48 |
+
## Training
|
| 49 |
+
|
| 50 |
+
We trained the model on [the datasets collected by Smetanin](https://github.com/sismetanin/sentiment-analysis-in-russian). We have converted all training data into a 3-class format and have up- and downsampled the training data to balance both the sources and the classes. The training code is available as [a Colab notebook](https://gist.github.com/avidale/e678c5478086c1d1adc52a85cb2b93e6). The metrics on the balanced test set are the following:
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
| Source | Macro F1 |
|
| 54 |
+
| ----------- | ----------- |
|
| 55 |
+
| SentiRuEval2016_banks | 0.83 |
|
| 56 |
+
| SentiRuEval2016_tele | 0.74 |
|
| 57 |
+
| kaggle_news | 0.66 |
|
| 58 |
+
| linis | 0.50 |
|
| 59 |
+
| mokoron | 0.98 |
|
| 60 |
+
| rureviews | 0.72 |
|
| 61 |
+
| rusentiment | 0.67 |
|
| 62 |
+
|