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

Compute metrics while using SFT trainer #34

Open
shubhamagarwal92 opened this issue Sep 19, 2023 · 1 comment
Open

Compute metrics while using SFT trainer #34

shubhamagarwal92 opened this issue Sep 19, 2023 · 1 comment

Comments

@shubhamagarwal92
Copy link

Hi @philschmid!

If I would like to compute metrics while training (similar to https://www.philschmid.de/fine-tune-flan-t5), how should I change the compute metrics function to pass to SFT trainer for Llamav2 (https://www.philschmid.de/instruction-tune-llama-2)

Would this be same as:

def compute_metrics(eval_pred, tokenizer):
    predictions, labels = eval_pred
    decoded_preds = tokenizer.batch_decode(predictions, skip_special_tokens=True)
    labels = np.where(labels != -100, labels, tokenizer.pad_token_id)
    decoded_labels = tokenizer.batch_decode(labels, skip_special_tokens=True)

    result = rouge.compute(predictions=decoded_preds, references=decoded_labels, use_stemmer=True)

    prediction_lens = [np.count_nonzero(pred != tokenizer.pad_token_id) for pred in predictions]
    result["gen_len"] = np.mean(prediction_lens)

    return {k: round(v, 4) for k, v in result.items()}

Specifically, do I still need to do

labels = np.where(labels != -100, labels, tokenizer.pad_token_id)

Is there a way to save the predictions decoded_preds while also passing the filename=f"{savedir}/{preds}.csv" to Trainer?

@philschmid
Copy link
Owner

You can write a callback for the Trainer which is executed after an evaluation phase. https://huggingface.co/docs/transformers/main_classes/callback#transformers.TrainerCallback.on_evaluate

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

2 participants