Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to fine-tune cappy ? #3

Open
Chaochao2020 opened this issue May 29, 2024 · 0 comments
Open

How to fine-tune cappy ? #3

Chaochao2020 opened this issue May 29, 2024 · 0 comments

Comments

@Chaochao2020
Copy link

Chaochao2020 commented May 29, 2024

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')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant