model updated
Browse files- .env.example +1 -0
- README.md +18 -1
- explainableai.py +10 -10
- finetune-emotions.py +9 -6
- pytorch_model.bin +2 -2
.env.example
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
WANDB_API_KEY=<your-api-key>
|
README.md
CHANGED
@@ -1,8 +1,25 @@
|
|
1 |
---
|
2 |
license: cc-by-nc-sa-4.0
|
3 |
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
-
Notebook: https://colab.research.google.com/drive/10ZCFvlf2UV3FjU4ymf4OoipQvqHbIItG?usp=sharing
|
6 |
|
7 |
|
8 |
## Example
|
|
|
1 |
---
|
2 |
license: cc-by-nc-sa-4.0
|
3 |
---
|
4 |
+
# CITDA:
|
5 |
+
|
6 |
+
Fine-tuned `bert-base-uncased` on the `emotions` dataset
|
7 |
+
|
8 |
+
Demo Notebook: https://colab.research.google.com/drive/10ZCFvlf2UV3FjU4ymf4OoipQvqHbIItG?usp=sharing
|
9 |
+
|
10 |
+
## Packages
|
11 |
+
|
12 |
+
- Install `torch`
|
13 |
+
- Also, `pip install transformers datasets scikit-learn wandb seaborn python-dotenv`
|
14 |
+
|
15 |
+
|
16 |
+
## Train
|
17 |
+
|
18 |
+
1. Rename `.env.example` to `.env` and set an API key from [wandb](https://wandb.ai/authorize)
|
19 |
+
2. You can adjust model parameters in the `explainableai.py` file.
|
20 |
+
2. The model (`pytorch_model.bin`) is a based on the `bert-base-uncased` and already trained on the `emotions` dataset.
|
21 |
+
To re-produce the training run `finetune-emotions.py`. You can change the base model, or the dataset by changing that file's code.
|
22 |
|
|
|
23 |
|
24 |
|
25 |
## Example
|
explainableai.py
CHANGED
@@ -3,7 +3,7 @@ from sklearn.metrics import accuracy_score, f1_score
|
|
3 |
|
4 |
import numpy as np
|
5 |
|
6 |
-
CITDA_EPOCHS =
|
7 |
CITDA_WEIGHT_DECAY = 0.05 # L2 regularization
|
8 |
CITDA_BATCH_SIZE = 32
|
9 |
CITDA_LEARNINGRATE= 2e-5
|
@@ -11,7 +11,6 @@ CITDA_LEARNINGRATE= 2e-5
|
|
11 |
class CITDA:
|
12 |
def __init__(self, model, labels, base_model_name, tokenizer, encoded_data):
|
13 |
self.labels = labels
|
14 |
-
# self.device = device
|
15 |
self.tokenizer = tokenizer
|
16 |
self.model = model
|
17 |
self.encoded_data = encoded_data
|
@@ -34,25 +33,26 @@ class CITDA:
|
|
34 |
weight_decay=CITDA_WEIGHT_DECAY,
|
35 |
evaluation_strategy="epoch",
|
36 |
save_strategy="epoch",
|
37 |
-
disable_tqdm=False
|
|
|
38 |
trainer = Trainer(model=self.model, tokenizer=self.tokenizer, args=training_args,
|
39 |
compute_metrics=compute_metrics,
|
40 |
train_dataset = self.encoded_data["train"],
|
41 |
-
eval_dataset = self.encoded_data["validation"]
|
42 |
-
report_to="wandb")
|
43 |
return trainer
|
44 |
|
45 |
def train(self):
|
46 |
trainer = self._get_trainer()
|
|
|
47 |
results = trainer.evaluate()
|
48 |
-
preds_output = trainer.predict(encoded_data["validation"])
|
49 |
|
50 |
-
y_valid = np.array(encoded_data["validation"]["label"])
|
51 |
-
|
52 |
|
53 |
#Saving the fine-tuned model
|
54 |
-
self.model.save_pretrained('./
|
55 |
-
self.tokenizer.save_pretrained('./
|
56 |
|
57 |
return y_valid, y_pred
|
58 |
|
|
|
3 |
|
4 |
import numpy as np
|
5 |
|
6 |
+
CITDA_EPOCHS = 6
|
7 |
CITDA_WEIGHT_DECAY = 0.05 # L2 regularization
|
8 |
CITDA_BATCH_SIZE = 32
|
9 |
CITDA_LEARNINGRATE= 2e-5
|
|
|
11 |
class CITDA:
|
12 |
def __init__(self, model, labels, base_model_name, tokenizer, encoded_data):
|
13 |
self.labels = labels
|
|
|
14 |
self.tokenizer = tokenizer
|
15 |
self.model = model
|
16 |
self.encoded_data = encoded_data
|
|
|
33 |
weight_decay=CITDA_WEIGHT_DECAY,
|
34 |
evaluation_strategy="epoch",
|
35 |
save_strategy="epoch",
|
36 |
+
disable_tqdm=False,
|
37 |
+
report_to="wandb")
|
38 |
trainer = Trainer(model=self.model, tokenizer=self.tokenizer, args=training_args,
|
39 |
compute_metrics=compute_metrics,
|
40 |
train_dataset = self.encoded_data["train"],
|
41 |
+
eval_dataset = self.encoded_data["validation"])
|
|
|
42 |
return trainer
|
43 |
|
44 |
def train(self):
|
45 |
trainer = self._get_trainer()
|
46 |
+
trainer.train()
|
47 |
results = trainer.evaluate()
|
48 |
+
preds_output = trainer.predict(self.encoded_data["validation"])
|
49 |
|
50 |
+
y_valid = np.array(self.encoded_data["validation"]["label"])
|
51 |
+
y_pred = np.argmax(preds_output.predictions, axis=1)
|
52 |
|
53 |
#Saving the fine-tuned model
|
54 |
+
self.model.save_pretrained('./')
|
55 |
+
self.tokenizer.save_pretrained('./')
|
56 |
|
57 |
return y_valid, y_pred
|
58 |
|
finetune-emotions.py
CHANGED
@@ -1,5 +1,3 @@
|
|
1 |
-
# Modified https://github.com/bhadreshpsavani/ExploringSentimentalAnalysis/blob/main/SentimentalAnalysisWithDistilbert.ipynb
|
2 |
-
|
3 |
import torch
|
4 |
from sklearn.metrics import confusion_matrix
|
5 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
@@ -7,13 +5,17 @@ from datasets import load_dataset
|
|
7 |
#import matplotlib.pyplot as plt
|
8 |
import seaborn as sns
|
9 |
import explainableai
|
|
|
|
|
|
|
10 |
|
11 |
BASE_MODEL_NAME = "bert-base-uncased"
|
12 |
-
device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
|
13 |
|
|
|
|
|
14 |
|
15 |
-
def save_confusion_matrix(y_valid,
|
16 |
-
cm = confusion_matrix(y_valid,
|
17 |
f = sns.heatmap(cm, annot=True, fmt='d')
|
18 |
f.figure.savefig("confusion_matrix.png")
|
19 |
|
@@ -24,6 +26,7 @@ def get_encoded_data(tokenizer):
|
|
24 |
emotions_encoded = emotions.map(tokenize, batched=True, batch_size=None)
|
25 |
emotions_encoded.set_format("torch", columns=["input_ids", "attention_mask", "label"])
|
26 |
return emotions_encoded
|
|
|
27 |
if __name__ == "__main__":
|
28 |
labels = ['sadness', 'joy', 'love', 'anger', 'fear', 'surprise']
|
29 |
model = AutoModelForSequenceClassification.from_pretrained(
|
@@ -38,5 +41,5 @@ if __name__ == "__main__":
|
|
38 |
citda = explainableai.CITDA(model, labels, BASE_MODEL_NAME, tokenizer, encoded_data)
|
39 |
y_valid, y_pred = citda.train()
|
40 |
|
41 |
-
save_confusion_matrix(y_valid,
|
42 |
print("y_valid=",len(y_valid), "y_pred=", len(y_pred))
|
|
|
|
|
|
|
1 |
import torch
|
2 |
from sklearn.metrics import confusion_matrix
|
3 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
|
|
5 |
#import matplotlib.pyplot as plt
|
6 |
import seaborn as sns
|
7 |
import explainableai
|
8 |
+
import os
|
9 |
+
from dotenv import load_dotenv
|
10 |
+
load_dotenv()
|
11 |
|
12 |
BASE_MODEL_NAME = "bert-base-uncased"
|
|
|
13 |
|
14 |
+
device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
|
15 |
+
print("Device:", device)
|
16 |
|
17 |
+
def save_confusion_matrix(y_valid, y_pred):
|
18 |
+
cm = confusion_matrix(y_valid, y_pred)
|
19 |
f = sns.heatmap(cm, annot=True, fmt='d')
|
20 |
f.figure.savefig("confusion_matrix.png")
|
21 |
|
|
|
26 |
emotions_encoded = emotions.map(tokenize, batched=True, batch_size=None)
|
27 |
emotions_encoded.set_format("torch", columns=["input_ids", "attention_mask", "label"])
|
28 |
return emotions_encoded
|
29 |
+
|
30 |
if __name__ == "__main__":
|
31 |
labels = ['sadness', 'joy', 'love', 'anger', 'fear', 'surprise']
|
32 |
model = AutoModelForSequenceClassification.from_pretrained(
|
|
|
41 |
citda = explainableai.CITDA(model, labels, BASE_MODEL_NAME, tokenizer, encoded_data)
|
42 |
y_valid, y_pred = citda.train()
|
43 |
|
44 |
+
save_confusion_matrix(y_valid, y_pred)
|
45 |
print("y_valid=",len(y_valid), "y_pred=", len(y_pred))
|
pytorch_model.bin
CHANGED
@@ -1,3 +1,3 @@
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
-
oid sha256:
|
3 |
-
size
|
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:8b0409f66d9c3fe0e3aa9976265fa1f26dbd1526cd212a696fa8d97e459b71e9
|
3 |
+
size 438036351
|