cappy icon indicating copy to clipboard operation
cappy copied to clipboard

How to fine-tune cappy ?

Open Chaochao2020 opened this issue 9 months ago • 0 comments

Hello, actually I want to use my own data to fine-tune cappy to get better output, but after I fine-tune cappy, the output is quite different from label. I would like to ask if there is something wrong with my fine-tuning method?

Dataset format{ "instruction": "xx", "response": "xx", "label": xx(Here is the credibility score of the answer) },

Complete code:

from transformers import AutoTokenizer, AutoModelForSequenceClassification, TrainingArguments, Trainer
from datasets import load_dataset



dataset = load_dataset('json', data_files='data.json')
dataset = dataset['train'].train_test_split(test_size=0.2)


tokenizer = AutoTokenizer.from_pretrained("btan2/cappy-large")
model = AutoModelForSequenceClassification.from_pretrained("btan2/cappy-large")


def preprocess_function(examples):
    inputs = [f"{q} {a}" for q, a in zip(examples['instruction'], examples['response'])]
    return tokenizer(inputs, truncation=True, padding='max_length', max_length=128)

encoded_dataset = dataset.map(preprocess_function, batched=True)


def convert_labels(examples):
    examples["labels"] = examples["label"]
    return examples

encoded_dataset = encoded_dataset.map(convert_labels, batched=True)
encoded_dataset = encoded_dataset.remove_columns(["instruction", "response", "label"])
print(encoded_dataset)


training_args = TrainingArguments(
    output_dir='./results',
    evaluation_strategy="epoch",
    learning_rate=2e-5,
    per_device_train_batch_size=16,
    per_device_eval_batch_size=16,
    num_train_epochs=3,
    weight_decay=0.01,
)


trainer = Trainer(
    model=model,
    args=training_args,
    train_dataset=encoded_dataset['train'],
    eval_dataset=encoded_dataset['test']
)


trainer.train()


model.save_pretrained('./fine-tuned-model')
tokenizer.save_pretrained('./fine-tuned-model')

Chaochao2020 avatar May 29 '24 02:05 Chaochao2020