-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel_training.py
More file actions
66 lines (56 loc) · 2.02 KB
/
Copy pathmodel_training.py
File metadata and controls
66 lines (56 loc) · 2.02 KB
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
import torch
from datasets import load_dataset
from transformers import (
AutoModelForCausalLM,
AutoTokenizer,
TrainingArguments,
Trainer,
DataCollatorForLanguageModeling
)
# Load base model (choose a smaller model for resource efficiency)
model_name = "EleutherAI/pythia-1.4b" # A relatively small model
model = AutoModelForCausalLM.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)
# Set padding token
if tokenizer.pad_token is None:
# Use the EOS token as the padding token if it exists
if tokenizer.eos_token is not None:
tokenizer.pad_token = tokenizer.eos_token
# Otherwise, add a new padding token
else:
tokenizer.add_special_tokens({'pad_token': '[PAD]'})
# Resize the token embeddings since we added a new token
model.resize_token_embeddings(len(tokenizer))
# Load your dataset
dataset = load_dataset('json', data_files='ai_ethics_dataset.jsonl')
# Add this after loading the dataset
print(f"Dataset size: {len(dataset['train'])}")
print(f"Sample entry: {dataset['train'][0]}")
# Tokenize the dataset
def tokenize_function(examples):
# Format as instruction-response pairs
texts = [f"Question: {q}\nAnswer: {a}" for q, a in zip(examples["instruction"], examples["response"])]
return tokenizer(texts, padding="max_length", truncation=True, max_length=512)
tokenized_dataset = dataset.map(tokenize_function, batched=True)
# Set up training arguments
training_args = TrainingArguments(
output_dir="./ai_ethics_llm",
per_device_train_batch_size=8,
num_train_epochs=3,
save_steps=1000,
save_total_limit=2,
learning_rate=5e-5,
fp16=False,
)
# Initialize trainer
trainer = Trainer(
model=model,
args=training_args,
train_dataset=tokenized_dataset["train"],
data_collator=DataCollatorForLanguageModeling(tokenizer=tokenizer, mlm=False),
)
# Train the model
trainer.train()
# Save the fine-tuned model
model.save_pretrained("./ai_ethics_llm_final")
tokenizer.save_pretrained("./ai_ethics_llm_final")