Search code examples
pythondeep-learningpytorchhuggingface-transformers

How to plot loss when using HugginFace's Trainer?


While finetuning a model using HF's trainer.

training_args = TrainingArguments(output_dir=data_dir + "test_trainer")

metric = load_metric("accuracy")

def compute_metrics(eval_pred):
    logits, labels = eval_pred
    predictions = np.argmax(logits, axis=-1)
    return metric.compute(predictions=predictions, references=labels)

training_args = TrainingArguments(num_train_epochs=5,per_device_train_batch_size=64,per_device_eval_batch_size=32,output_dir="test_trainer", evaluation_strategy="epoch")

trainer = Trainer(
    model=model,
    args=training_args,
    train_dataset=train_dataset,
    eval_dataset=val_dataset,
    compute_metrics=compute_metrics,
)
trainer.train()

How would I be able to plot the loss in a notebook?
(Perhaps Is it possible to get a list of the loss)


Solution

  • It is possible to get a list of losses. You can access the history of logs after training is complete with: trainer.state.log_history