-
Notifications
You must be signed in to change notification settings - Fork 1
/
bert_sentiment_analysis.py
187 lines (148 loc) · 5.52 KB
/
bert_sentiment_analysis.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import argparse
import evaluate
import numpy as np
import os
import re
import sys
import torch
import transformers
from datasets import load_dataset
from peft import LoraConfig, TaskType, get_peft_model
from transformers import (
BertTokenizer,
BertForSequenceClassification,
BertConfig,
Trainer,
TrainingArguments,
)
def get_device():
if torch.cuda.is_available():
return "cuda"
elif torch.backends.mps.is_available():
return "mps"
else:
return "cpu"
def clean_text(sample):
def remove_unicode_regex(text):
# Define the regex pattern to match Unicode characters
pattern = r"[\u0080-\uFFFF]"
# Use regex substitution to remove the matched Unicode characters
cleaned_text = re.sub(pattern, "", text)
return cleaned_text
sample["text"] = sample["text"].replace("@user", "")
sample["text"] = sample["text"].replace("#", "")
sample["text"] = sample["text"].replace("&", "&")
sample["text"] = sample["text"].replace("<", "<")
sample["text"] = sample["text"].replace(">", ">")
sample["text"] = sample["text"].strip()
sample["text"] = remove_unicode_regex(sample["text"])
return sample
def load_tweet_eval_dataset(tokenizer: BertTokenizer, seed: int = 42):
tweet_eval_dataset = load_dataset("tweet_eval", name="sentiment")
tweet_eval_dataset = (
tweet_eval_dataset.map(clean_text).map(
lambda sample: tokenizer(sample["text"], padding="max_length")
)
).shuffle(seed=seed)
train_dataset = tweet_eval_dataset["train"].with_format("torch")
test_dataset = tweet_eval_dataset["test"].with_format("torch")
return train_dataset, test_dataset
def main(args):
parser = argparse.ArgumentParser()
parser.add_argument("--use-pretrained", action="store_true", default=True)
parser.add_argument("--epochs", type=int, default=1)
parser.add_argument("--batch-size", type=int, default=16)
parser.add_argument("--lr", type=float, default=5e-5)
parser.add_argument("--weight-decay", type=float, default=0.01)
parser.add_argument("--warmup", type=float, default=0.1)
parser.add_argument("--r", type=int, default=4)
parser.add_argument(
"--fine-tuning",
type=str,
choices=["full", "top", "top2", "lora"],
default="full",
)
parser.add_argument("--output-dir", type=str, required=True)
args = parser.parse_args(args)
os.makedirs(args.output_dir, exist_ok=True)
print("Loading BERT model...")
if args.use_pretrained:
model = BertForSequenceClassification.from_pretrained(
"bert-base-cased", num_labels=3
)
else:
bert_config = BertConfig.from_pretrained("bert-base-cased")
bert_config.num_labels = 3
model = BertForSequenceClassification(bert_config)
# freeze all but the final encoder
if args.fine_tuning == "top":
for param in model.bert.parameters():
param.requires_grad = False
for param in model.bert.encoder.layer[-1].parameters():
param.requires_grad = True
# freeze all but the last two encoders
elif args.fine_tuning == "top2":
for param in model.bert.parameters():
param.requires_grad = False
for param in model.bert.encoder.layer[-2:].parameters():
param.requires_grad = True
elif args.fine_tuning == "lora":
lora_config = LoraConfig(
task_type=TaskType.SEQ_CLS,
inference_mode=False,
r=args.r,
lora_alpha=args.r,
lora_dropout=0.1,
)
model = get_peft_model(model, lora_config)
model.print_trainable_parameters()
tokenizer = BertTokenizer.from_pretrained("bert-base-cased")
precision_metric = evaluate.load("precision")
f1_metric = evaluate.load("f1")
print("Loading Tweet Eval dataset...")
train_dataset, test_dataset = load_tweet_eval_dataset(tokenizer)
training_args = TrainingArguments(
output_dir=args.output_dir,
num_train_epochs=args.epochs,
per_device_train_batch_size=args.batch_size,
per_device_eval_batch_size=args.batch_size,
weight_decay=args.weight_decay,
learning_rate=args.lr,
warmup_ratio=args.warmup,
evaluation_strategy="steps",
save_steps=1_000,
eval_steps=1_000,
save_total_limit=5,
use_mps_device=get_device() == "mps",
)
trainer = Trainer(
model=model,
args=training_args,
train_dataset=train_dataset,
eval_dataset=test_dataset,
compute_metrics=lambda pred: {
"precision": precision_metric.compute(
predictions=np.argmax(pred.predictions, axis=-1),
references=pred.label_ids,
average="macro",
)["precision"],
"f1": f1_metric.compute(
predictions=np.argmax(pred.predictions, axis=-1),
references=pred.label_ids,
average="macro",
)["f1"],
},
)
print("Starting to train...")
transformers.logging.set_verbosity_info()
trainer.train()
final_checkpoint = os.path.join(args.output_dir, "final-model".format(args.epochs))
os.makedirs(final_checkpoint, exist_ok=True)
if args.fine_tuning == "lora":
model.save_pretrained(final_checkpoint)
else:
trainer.save_model(final_checkpoint)
final_metrics = trainer.evaluate()
trainer.save_metrics("eval", final_metrics)
if __name__ == "__main__":
main(sys.argv[1:])