ÎncepețiÎncepe gratuit

Mixed precision training with Accelerator

You want to simplify your PyTorch loop for mixed precision training of your language translation model by using Accelerator. Build the new training loop to leverage Accelerator!

Some objects have been preloaded: dataset, model, dataloader, and optimizer.

Acest exercițiu face parte din cursul

Efficient AI Model Training with PyTorch

Vezi cursul

Instrucțiuni pentru exercițiu

  • Enable mixed precision training using FP16 in Accelerator.
  • Prepare training objects for mixed precision training before the loop.
  • Compute the gradients of the loss for mixed precision training.

Exercițiu interactiv practic

Încearcă acest exercițiu completând acest cod de exemplu.

# Enable mixed precision training using FP16
accelerator = Accelerator(____="____")

# Prepare training objects for mixed precision training
model, optimizer, train_dataloader, lr_scheduler = ____.____(____, ____, ____, ____)

for batch in train_dataloader:
    inputs, targets = batch["input_ids"], batch["labels"]
    outputs = model(inputs, labels=targets)
    loss = outputs.loss
    # Compute the gradients of the loss
    ____.____(loss)
    optimizer.step()
    optimizer.zero_grad()
Editează și rulează codul