Add example training script etc.
Browse files
README.md
CHANGED
|
@@ -59,6 +59,70 @@ The data fields are the same among all splits.
|
|
| 59 |
|---------|----:|---------:|---:|
|
| 60 |
|tomaarsen/setfit-absa-semeval-restaurants|3693|96|1134|
|
| 61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
### Citation Information
|
| 63 |
|
| 64 |
```bibtex
|
|
|
|
| 59 |
|---------|----:|---------:|---:|
|
| 60 |
|tomaarsen/setfit-absa-semeval-restaurants|3693|96|1134|
|
| 61 |
|
| 62 |
+
### Training ABSA models using SetFit ABSA
|
| 63 |
+
|
| 64 |
+
To train using this dataset, first install the SetFit library:
|
| 65 |
+
|
| 66 |
+
```bash
|
| 67 |
+
pip install setfit
|
| 68 |
+
```
|
| 69 |
+
|
| 70 |
+
And then you can use the following script as a guideline of how to train an ABSA model on this dataset:
|
| 71 |
+
|
| 72 |
+
```python
|
| 73 |
+
from setfit import AbsaModel, AbsaTrainer, TrainingArguments
|
| 74 |
+
from datasets import load_dataset
|
| 75 |
+
from transformers import EarlyStoppingCallback
|
| 76 |
+
|
| 77 |
+
# You can initialize a AbsaModel using one or two SentenceTransformer models, or two ABSA models
|
| 78 |
+
model = AbsaModel.from_pretrained("sentence-transformers/all-MiniLM-L6-v2")
|
| 79 |
+
|
| 80 |
+
# The training/eval dataset must have `text`, `span`, `polarity`, and `ordinal` columns
|
| 81 |
+
dataset = load_dataset("tomaarsen/setfit-absa-semeval-restaurants")
|
| 82 |
+
train_dataset = dataset["train"]
|
| 83 |
+
eval_dataset = dataset["validation"]
|
| 84 |
+
|
| 85 |
+
args = TrainingArguments(
|
| 86 |
+
output_dir="models",
|
| 87 |
+
use_amp=True,
|
| 88 |
+
batch_size=256,
|
| 89 |
+
eval_steps=50,
|
| 90 |
+
save_steps=50,
|
| 91 |
+
load_best_model_at_end=True,
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
trainer = AbsaTrainer(
|
| 95 |
+
model,
|
| 96 |
+
args=args,
|
| 97 |
+
train_dataset=train_dataset,
|
| 98 |
+
eval_dataset=eval_dataset,
|
| 99 |
+
callbacks=[EarlyStoppingCallback(early_stopping_patience=5)],
|
| 100 |
+
)
|
| 101 |
+
trainer.train()
|
| 102 |
+
|
| 103 |
+
metrics = trainer.evaluate(eval_dataset)
|
| 104 |
+
print(metrics)
|
| 105 |
+
|
| 106 |
+
trainer.push_to_hub("tomaarsen/setfit-absa-restaurants")
|
| 107 |
+
```
|
| 108 |
+
|
| 109 |
+
You can then run inference like so:
|
| 110 |
+
```python
|
| 111 |
+
from setfit import AbsaModel
|
| 112 |
+
|
| 113 |
+
# Download from Hub and run inference
|
| 114 |
+
model = AbsaModel.from_pretrained(
|
| 115 |
+
"tomaarsen/setfit-absa-restaurants-aspect",
|
| 116 |
+
"tomaarsen/setfit-absa-restaurants-polarity",
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
# Run inference
|
| 120 |
+
preds = model([
|
| 121 |
+
"The best pizza outside of Italy and really tasty.",
|
| 122 |
+
"The food here is great but the service is terrible",
|
| 123 |
+
])
|
| 124 |
+
```
|
| 125 |
+
|
| 126 |
### Citation Information
|
| 127 |
|
| 128 |
```bibtex
|