Lightning AI Studios: Never set up a local environment again →

Log in or create a free Lightning.ai account to track your progress and access additional course materials  

5.2 Training a Multilayer Perceptron using the Lightning Trainer

Slides

Code

What we covered in this video lecture

After introducing the main components of the Lightning API for PyTorch in Unit 5.1, we saw a hands-on example of how we can use it to train a PyTorch model. As you’ve seen, the LightningModule offers a structure approach to replacing our manually-coded PyTorch training loops.

In future units, we will introduce additional features which will hopefully make you appreciate the LightningModule and Trainer when you are training more complicated models later.

Log in or create a free Lightning.ai account to access:

  • Quizzes
  • Completion badges
  • Progress tracking
  • Additional downloadable content
  • Additional AI education resources
  • Notifications when new units are released
  • Free cloud computing credits

Quiz: 5.2 Training a Multilayer Perceptron using the Lightning Trainer (Part 1)

The data prepartion code for loading and inspecting the MNIST dataset is the same code we already used in Unit 4.

Correct. We have already seen this code before in Unit 4.

Incorrect. We have already seen this code before in Unit 4.

Please answer all questions to proceed.

Quiz: 5.2 Training a Multilayer Perceptron using the Lightning Trainer (Part 2)

Before we are moving on to using the LightningModule and Trainer, suppose you want to implement a PyTorch training loop for comparison. Can you put the following code into the right order?
(1) loss.backward()
(2) logits = model(features)
(3) optimizer.zero_grad()
(4) optimizer.step()
(5) loss = F.cross_entropy(logits, labels)

Incorrect. We definitely have to compute the loss before we can call .backward().

Incorrect. We can’t update the weights via optimizer.step() before we computed the gradients.

Correct!

Incorrect. We can’t compute the loss before we have obtained the model outputs.

Please answer all questions to proceed.

Quiz: 5.2 Training a Multilayer Perceptron using the Lightning Trainer (Part 3)

Which of the following codes is not valid?

Incorrect. This code is valid.

Incorrect. This code is valid.

Correct. max_epochs is a Trainer constructor parameter, not a .fit() parameter

Incorrect. This code is valid.

Please answer all questions to proceed.
Watch Video 1

Unit 5.2

Videos
Follow along in a Lightning Studio

DL Fundamentals 5: PyTorch Lightning

Sebastian
Launch Studio →
Questions or Feedback?

Join the Discussion