File size: 2,030 Bytes
15a81c9
 
dbd3896
15a81c9
c8dcfa1
 
dbd3896
 
 
45ab08c
15a81c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dbd3896
95c0d20
 
 
 
 
 
 
 
 
dbd3896
 
15a81c9
dbd3896
15a81c9
 
 
dbd3896
 
 
 
 
4839cae
15a81c9
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
from transformers import AutoModelForSequenceClassification, AutoTokenizer, Trainer, TrainingArguments
import torch
from datasets import load_dataset
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = AutoModelForSequenceClassification.from_pretrained("google/flan-t5-small").to(device)
tokenizer = AutoTokenizer.from_pretrained("google/flan-t5-small")



dataset = load_dataset("athirdpath/DPO_Pairs-Roleplay-Alpaca-NSFW")
train_dataset = dataset["train"]

max_length = 512 # You can change this according to your needs
def preprocess(example):
  # Tokenize the inputs and outputs
  input_tokens = tokenizer(example["prompt"], truncation=True, max_length=max_length, padding="max_length", return_tensors="pt")
  output_tokens = tokenizer(example["chosen"], truncation=True, max_length=max_length, padding="max_length", return_tensors="pt")
  # Convert the tokens to tensors and move them to the device
  input_ids = input_tokens["input_ids"].squeeze().to(device)
  attention_mask = input_tokens["attention_mask"].squeeze().to(device)
  output_ids = output_tokens["input_ids"].squeeze().to(device)
  # Return a dictionary of tensors
  return {"input_ids": input_ids, "attention_mask": attention_mask, "output_ids": output_ids}

# Apply the preprocess function to the train, validation, and test sets
train_dataset = train_dataset.map(preprocess, batched=True)



# Define the training arguments
training_args = TrainingArguments(
  output_dir="output",
  num_train_epochs=3,
  learning_rate=5e-5,
  per_device_train_batch_size=8,
  per_device_eval_batch_size=8,
  evaluation_strategy="steps", # Change this to steps
  save_strategy="steps", # Change this to steps
  logging_dir="logs",
  load_best_model_at_end=True,
)

# Define the trainer
trainer = Trainer(
  model=model, # The model to train
  args=training_args, # The training arguments
  train_dataset=train_dataset, # The training dataset
)

# Train the model
trainer.train()

# Evaluate the m odel on the test set
trainer.evaluate(test_dataset)