22.02.2024 Views

Daniel Voigt Godoy - Deep Learning with PyTorch Step-by-Step A Beginner’s Guide-leanpub

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

It’s printing the training and validation losses, and the validation accuracy, every

three hundred mini-batches as configured. To check the final validation figures,

though, we need to call the evaluate() method, which, guess what, actually runs a

validation loop! Thank you, HuggingFace :-)

trainer.evaluate()

Output

{'eval_loss': 0.142591193318367,

'eval_accuracy': 0.9610894941634242,

'eval_runtime': 1.6634,

'eval_samples_per_second': 463.51,

'epoch': 1.0,

'eval_mem_cpu_alloc_delta': 0,

'eval_mem_gpu_alloc_delta': 0,

'eval_mem_cpu_peaked_delta': 0,

'eval_mem_gpu_peaked_delta': 8132096}

That’s 96.11% accuracy on the validation set after one epoch, roughly the same as

our own implementation ("Model IV"). Nice!

Once the model is trained, we can save it to disk using the save_model() method

from Trainer:

trainer.save_model('bert_alice_vs_wizard')

os.listdir('bert_alice_vs_wizard')

Output

['training_args.bin', 'config.json', 'pytorch_model.bin']

It creates a folder with the provided name, and it stores the trained model

(pytorch_model.bin) along with its configuration (config.json) and training

arguments (training_args.bin).

Later on, we can easily load the trained model using the from_pretrained()

method from the corresponding AutoModel:

998 | Chapter 11: Down the Yellow Brick Rabbit Hole

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!