Framework for Fine-tuning Transformers for Sentiment Analysis
Project description
senda 
senda is a python package for fine-tuning transformers for
sentiment analysis (and text classification in general).
senda builds on the excellent transformers.Trainer API.
Installation guide
senda can be installed from PyPI with
pip install senda
If you want the development version then install directly from GitHub.
How to use
We will fine-tune a transformer for detecting the polarity ('positive', 'neutral' or 'negative') of Danish Tweets. For training we use more than 5,000 Danish Tweets kindly annotated and hosted by the Alexandra Institute.
First, load sentiment analysis datasets.
from senda import get_danish_tweets
df_train, df_eval, df_test = get_danish_tweets()
Note, that the datasets must be DataFrames containing the columns 'text' and 'label'.
Next, instantiate the model and set up the model.
from senda import Model
m = Model(train_dataset = df_train,
eval_dataset = df_eval,
transformer = "Maltehb/danish-bert-botxo",
labels = ['negativ', 'neutral', 'positiv'],
tokenize_args = {'padding':True, 'truncation':True, 'max_length':512},
training_args = {"output_dir":'./results', # output directory
"num_train_epochs": 4, # total # of training epochs
"per_device_train_batch_size":8, # batch size per device during training
"evaluation_strategy":"steps",
"eval_steps":100,
"logging_steps":100,
"learning_rate":2e-05,
"weight_decay": 0.01,
"per_device_eval_batch_size":32, # batch size for evaluation
"warmup_steps":100, # number of warmup steps for learning rate scheduler
"seed":42,
"load_best_model_at_end":True,
})
Now, all there is left is to initialize a transformers.Trainer and
train the model:
# initialize Trainer
m.init()
# run training
m.train()
The model can then be evaluated on the test set:
m.evaluate(df_test)
{'eval_loss': 0.5771588683128357, 'eval_accuracy': 0.7664399092970522, 'eval_f1': 0.7290485787279956, 'eval_runtime': 4.2016, 'eval_samples_per_second': 104.959}
Predict new observations:
text = "Sikke en dejlig dag det er i dag"
# in English: 'What a lovely day'
m.predict(text)
PredictionOutput(predictions=array([[-1.2986785 , -0.31318122, 1.2002046 ]], dtype=float32), label_ids=array([0]), metrics={'test_loss': 2.7630457878112793, 'test_accuracy': 0.0, 'test_f1': 0.0, 'test_runtime': 0.07, 'test_samples_per_second': 14.281})
m.predict(text, return_labels=True)
['positiv']
senda model available on Huggingface
The model above achieves an accuracy of 0.76 and a macro-averaged F1-score of 0.75 on a small test data set, that Alexandra Institute provides.
The model is published on Huggingface.
Here is how to download and use the model with PyTorch:
from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
tokenizer = AutoTokenizer.from_pretrained("larskjeldgaard/senda")
model = AutoModelForSequenceClassification.from_pretrained("larskjeldgaard/senda")
# create 'senda' sentiment analysis pipeline
senda_pipeline = pipeline('sentiment-analysis', model=model, tokenizer=tokenizer)
senda_pipeline("Sikke en dejlig dag det er i dag")
Background
senda is developed as a part of Ekstra Bladet’s activities on Platform Intelligence in News (PIN). PIN is an industrial research project that is carried out in collaboration between the Technical University of Denmark, University of Copenhagen and Copenhagen Business School with funding from Innovation Fund Denmark. The project runs from 2020-2023 and develops recommender systems and natural language processing systems geared for news publishing, some of which are open sourced like senda.
Contact
We hope, that you will find senda useful.
Please direct any questions and feedbacks to us!
If you want to contribute (which we encourage you to), open a PR.
If you encounter a bug or want to suggest an enhancement, please open an issue.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file senda-0.7.4.tar.gz.
File metadata
- Download URL: senda-0.7.4.tar.gz
- Upload date:
- Size: 9.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.7.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a2e744884e9a995207f88b31a8585ad65f4f19e3e1b9ec6fcf9fcb57f4749082
|
|
| MD5 |
68e04fce1444df3116f7f3d538dab076
|
|
| BLAKE2b-256 |
48a2d3a59a005830ece87e3581dd000abb989c414fa755aebce2c72e90f2a5b9
|
File details
Details for the file senda-0.7.4-py3-none-any.whl.
File metadata
- Download URL: senda-0.7.4-py3-none-any.whl
- Upload date:
- Size: 10.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.7.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5734f08267b055ca6822edd1baa5a8e699da093659409d8d82ee7cfef2517f1e
|
|
| MD5 |
2fd53cc9561d4fc6a6022f5f18d8a2a6
|
|
| BLAKE2b-256 |
55d90390637a2ccbaf937a6d5151f71f9f40b2dc859e04c6d8a75af7dd1e1a49
|