-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtraining_no_mps.py
48 lines (37 loc) · 1.44 KB
/
training_no_mps.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
# Training without the use of MPS
from datasets import load_dataset
import transformers
from transformers import AutoTokenizer, TrainingArguments, Trainer, AutoModelForSequenceClassification
from datetime import datetime
transformers.logging.set_verbosity_info()
# Load dataset
dataset = load_dataset('dair-ai/emotion')
# Initialize tokenizer
tokenizer = AutoTokenizer.from_pretrained('bert-base-uncased')
# Tokenization function that moves data to MPS device
def tokenize(e):
return tokenizer(e['text'], padding='max_length', truncation=True, max_length=128)
# Tokenize the dataset
tokenized_dataset = dataset.map(tokenize, batched=True)
# Load model and move it to MPS device
model = AutoModelForSequenceClassification.from_pretrained('bert-base-uncased', num_labels=6)
# Set up training arguments
training_args = TrainingArguments(
output_dir=f"./results/training-run_{datetime.now()}",
num_train_epochs=3,
per_device_train_batch_size=8, # Reduced batch size for memory efficiency
per_device_eval_batch_size=32, # Reduced eval batch size
gradient_accumulation_steps=4, # Increased gradient accumulation for effective larger batch processing
warmup_steps=500,
weight_decay=0.01,
logging_dir='./logs',
logging_steps=10,
)
# Initialize the Trainer
trainer = Trainer(
model=model,
args=training_args,
train_dataset=tokenized_dataset["train"],
eval_dataset=tokenized_dataset["validation"],
)
trainer.train()