Update README.md
Browse files
README.md
CHANGED
|
@@ -1,3 +1,103 @@
|
|
| 1 |
-
---
|
| 2 |
-
license: cc-by-4.0
|
| 3 |
-
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: cc-by-4.0
|
| 3 |
+
---
|
| 4 |
+
# Fine-Tuning BERT for Italian Public Communication Classification
|
| 5 |
+
|
| 6 |
+
This repository provides code and documentation for fine-tuning a BERT-based transformer model (`dbmdz/bert-base-italian-cased`) to classify Italian texts into public communication categories.
|
| 7 |
+
|
| 8 |
+
---
|
| 9 |
+
|
| 10 |
+
## π§ Task: Multiclass Text Classification
|
| 11 |
+
|
| 12 |
+
Each input text is associated with **one and only one category** out of a predefined set (e.g., institutional communication, participatory communication, branding, etc.).
|
| 13 |
+
|
| 14 |
+
Unlike multilabel classification (where multiple categories may apply), this is a **multiclass classification task**.
|
| 15 |
+
|
| 16 |
+
---
|
| 17 |
+
|
| 18 |
+
## π Dataset
|
| 19 |
+
|
| 20 |
+
The input data is expected in an Excel format (`.xlsx`) with at least the following columns:
|
| 21 |
+
- `Text`: the input text
|
| 22 |
+
- `ricercatoreX`: the annotated category (one per row)
|
| 23 |
+
|
| 24 |
+
Categories are label-encoded before training. The script uses `sklearn.preprocessing.LabelEncoder`.
|
| 25 |
+
|
| 26 |
+
---
|
| 27 |
+
|
| 28 |
+
## ποΈ Model
|
| 29 |
+
|
| 30 |
+
- Base model: [`dbmdz/bert-base-italian-cased`](https://huggingface.co/dbmdz/bert-base-italian-cased)
|
| 31 |
+
- Classification head: dropout + dense layer (8 output classes)
|
| 32 |
+
- Loss function: `CrossEntropyLoss`
|
| 33 |
+
- Activation: `Softmax` at inference time
|
| 34 |
+
|
| 35 |
+
---
|
| 36 |
+
|
| 37 |
+
## π Training
|
| 38 |
+
|
| 39 |
+
To train the model:
|
| 40 |
+
|
| 41 |
+
```bash
|
| 42 |
+
pip install transformers torch scikit-learn pandas openpyxl
|
| 43 |
+
python bert_multiclass_classification.py
|
| 44 |
+
```
|
| 45 |
+
|
| 46 |
+
Training is done over 5 epochs with a batch size of 8 and learning rate of 1e-5.
|
| 47 |
+
|
| 48 |
+
The script:
|
| 49 |
+
- Loads and filters the dataset
|
| 50 |
+
- Encodes labels
|
| 51 |
+
- Prepares `CustomDataset` and PyTorch `DataLoader`
|
| 52 |
+
- Trains the model using BERT + dropout + classification layer
|
| 53 |
+
- Evaluates using accuracy and F1-score (micro, macro, weighted)
|
| 54 |
+
- Saves the model with `torch.save()`
|
| 55 |
+
|
| 56 |
+
---
|
| 57 |
+
|
| 58 |
+
## π Inference Example
|
| 59 |
+
|
| 60 |
+
Once trained, you can load the model and predict the class of a new text like this:
|
| 61 |
+
|
| 62 |
+
```python
|
| 63 |
+
text = "Aperte le iscrizioni"
|
| 64 |
+
input_data = prepare_input(text, tokenizer, max_len)
|
| 65 |
+
with torch.no_grad():
|
| 66 |
+
outputs = model(input_data['ids'], input_data['mask'], input_data['token_type_ids'])
|
| 67 |
+
predicted_class = torch.argmax(outputs, dim=1).cpu().numpy()[0]
|
| 68 |
+
predicted_label = label_encoder.inverse_transform([predicted_class])[0]
|
| 69 |
+
print(f"Predicted label: {predicted_label}")
|
| 70 |
+
```
|
| 71 |
+
|
| 72 |
+
---
|
| 73 |
+
|
| 74 |
+
## π Evaluation
|
| 75 |
+
|
| 76 |
+
Evaluation metrics printed at each epoch:
|
| 77 |
+
- Accuracy
|
| 78 |
+
- F1-score (micro, macro, weighted)
|
| 79 |
+
|
| 80 |
+
Also includes a normalized confusion matrix using `seaborn`.
|
| 81 |
+
|
| 82 |
+
---
|
| 83 |
+
|
| 84 |
+
## πΎ Model Saving & Loading
|
| 85 |
+
|
| 86 |
+
The trained model is saved with:
|
| 87 |
+
|
| 88 |
+
```python
|
| 89 |
+
torch.save(model.state_dict(), "model_epoch_X.pth")
|
| 90 |
+
```
|
| 91 |
+
|
| 92 |
+
To reload:
|
| 93 |
+
|
| 94 |
+
```python
|
| 95 |
+
model.load_state_dict(torch.load("model_epoch_X.pth"))
|
| 96 |
+
```
|
| 97 |
+
|
| 98 |
+
---
|
| 99 |
+
|
| 100 |
+
## π§βπ» Author
|
| 101 |
+
|
| 102 |
+
Developed by [anucita](https://huggingface.co/anucita) for research on public communication on social media.
|
| 103 |
+
|