QBoard » Artificial Intelligence & ML » AI and ML - PyTorch » How to do gradient clipping in pytorch?

How to do gradient clipping in pytorch?

  • What is the correct way to perform gradient clipping in pytorch?

    I have an exploding gradients problem, and I need to program my way around it.

      December 31, 2021 12:38 PM IST
    0
  • A more complete example

    optimizer.zero_grad()        
    loss, hidden = model(data, hidden, targets)
    loss.backward()
    
    torch.nn.utils.clip_grad_norm_(model.parameters(), args.clip)
    optimizer.step()

     

    Source: https://github.com/pytorch/pytorch/issues/309

     
      January 3, 2022 11:59 AM IST
    0
  • And if you are using Automatic Mixed Precision (AMP), you need to do a bit more before clipping:

    optimizer.zero_grad()
    loss, hidden = model(data, hidden, targets)
    self.scaler.scale(loss).backward()
    
    # Unscales the gradients of optimizer's assigned params in-place
    self.scaler.unscale_(optimizer)
    
    # Since the gradients of optimizer's assigned params are unscaled, clips as usual:
    torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm)
    
    # optimizer's gradients are already unscaled, so scaler.step does not unscale them,
    # although it still skips optimizer.step() if the gradients contain infs or NaNs.
    scaler.step(optimizer)
    
    # Updates the scale for next iteration.
    scaler.update()

     

    Reference: https://pytorch.org/docs/stable/notes/amp_examples.html#gradient-clipping

     
      January 4, 2022 1:00 PM IST
    0
  • Well, I met with same err. I tried to use the clip norm but it doesn't work.

    I don't want to change the network or add regularizers. So I change the optimizer to Adam, and it works.

    Then I use the pretrained model from Adam to initate the training and use SGD + momentum for fine tuning. It is now working.

      January 14, 2022 2:04 PM IST
    0