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.

Moreover, let’s use a ten times higher learning rate; after all, we’re in full control of

the gradients now:

Model Configuration

1 torch.manual_seed(42)

2 with torch.no_grad():

3 model.apply(weights_init)

4

5 optimizer = optim.SGD(model.parameters(), lr=0.1)

Before training it, let’s use set_clip_grad_value() to make sure no gradients are

ever above 1.0:

Model Training

1 sbs_reg_clip = StepByStep(model, loss_fn, optimizer)

2 sbs_reg_clip.set_loaders(train_loader)

3 sbs_reg_clip.set_clip_grad_value(1.0)

4 sbs_reg_clip.capture_gradients(['fc1'])

5 sbs_reg_clip.train(10)

6 sbs_reg_clip.remove_clip()

7 sbs_reg_clip.remove_hooks()

fig = sbs_reg_clip.plot_losses()

No more exploding gradients, it seems. The loss is being minimized even after

choosing a much higher learning rate to train the model.

Vanishing and Exploding Gradients | 581

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

Saved successfully!

Ooh no, something went wrong!