-
Notifications
You must be signed in to change notification settings - Fork 0
/
fine_tune_sentiment.py
77 lines (63 loc) · 2.48 KB
/
fine_tune_sentiment.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
import logging
import pandas as pd
from sklearn.model_selection import train_test_split
from transformers import TrainingArguments, Trainer, AutoModelForSequenceClassification, AutoTokenizer
from datasets import Dataset
from utils.helpers import setup_logging, load_config
import torch
def load_crypto_sentiment_data(file_path):
df = pd.read_csv(file_path)
return Dataset.from_pandas(df)
def tokenize_function(examples):
return tokenizer(examples["text"], padding="max_length", truncation=True)
def compute_metrics(eval_pred):
logits, labels = eval_pred
predictions = torch.argmax(torch.Tensor(logits), dim=-1)
return {"accuracy": (predictions == torch.Tensor(labels)).float().mean().item()}
def main():
setup_logging()
logger = logging.getLogger(__name__)
config = load_config()
logger.info("Starting sentiment model fine-tuning process")
# Load crypto-specific sentiment data
dataset = load_crypto_sentiment_data('data/crypto_sentiment.csv')
train_dataset, eval_dataset = train_dataset.train_test_split(test_size=0.2, seed=42).values()
# Load pre-trained model and tokenizer
model_name = "finiteautomata/bertweet-base-sentiment-analysis"
global tokenizer
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name)
# Tokenize datasets
tokenized_train = train_dataset.map(tokenize_function, batched=True)
tokenized_eval = eval_dataset.map(tokenize_function, batched=True)
# Set up training arguments
training_args = TrainingArguments(
output_dir="./results",
num_train_epochs=3,
per_device_train_batch_size=16,
per_device_eval_batch_size=64,
warmup_steps=500,
weight_decay=0.01,
logging_dir='./logs',
logging_steps=10,
evaluation_strategy="epoch",
save_strategy="epoch",
load_best_model_at_end=True,
)
# Initialize Trainer
trainer = Trainer(
model=model,
args=training_args,
train_dataset=tokenized_train,
eval_dataset=tokenized_eval,
compute_metrics=compute_metrics,
)
# Fine-tune the model
logger.info("Fine-tuning sentiment model...")
trainer.train()
# Save the fine-tuned model
model.save_pretrained('models/fine_tuned_sentiment_model')
tokenizer.save_pretrained('models/fine_tuned_sentiment_tokenizer')
logger.info("Fine-tuning complete. Model saved.")
if __name__ == "__main__":
main()