Spaces:
Running
Running
File size: 1,272 Bytes
c07a03d |
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 |
from datasets import load_dataset
from transformers import AutoTokenizer, AutoModelForCausalLM, Trainer, TrainingArguments
# Load dataset
dataset = load_dataset('json', data_files='flirty_dataset.json')
# Tokenizer and model
model_name = "gpt2"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)
# Tokenize dataset
def tokenize_function(examples):
return tokenizer(examples['prompt'], truncation=True, padding="max_length", max_length=128)
tokenized_dataset = dataset.map(tokenize_function, batched=True)
# Training arguments
training_args = TrainingArguments(
output_dir="./fine_tuned_gpt2",
evaluation_strategy="epoch",
save_strategy="epoch",
learning_rate=5e-5,
num_train_epochs=3,
per_device_train_batch_size=8,
save_total_limit=2,
logging_dir="./logs",
logging_steps=10,
fp16=True
)
# Trainer
trainer = Trainer(
model=model,
args=training_args,
train_dataset=tokenized_dataset["train"],
eval_dataset=tokenized_dataset["validation"],
tokenizer=tokenizer
)
# Train the model
trainer.train()
# Save model
trainer.save_model("./fine_tuned_gpt2")
tokenizer.save_pretrained("./fine_tuned_gpt2")
|