Get startedGet started for free

RNN training loop

It's time to train the electricity consumption forecasting model!

You will use the LSTM network you have defined previously, which has been instantiated and assigned to net, as is the dataloader_train you built before. You will also need to use torch.nn which has already been imported as nn.

In this exercise, you will train the model for only three epochs to make sure the training progresses as expected. Let's get to it!

This exercise is part of the course

Intermediate Deep Learning with PyTorch

View Course

Exercise instructions

  • Set up the Mean Squared Error loss and assign it to criterion.
  • Reshape seqs to (batch size, sequence length, num features), which in our case is (32, 96, 1), and re-assign the result to seqs.
  • Pass seqs to the model to get its outputs.
  • Based on previously computed quantities, calculate the loss, assigning it to loss.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

net = Net()
# Set up MSE loss
criterion = ____
optimizer = optim.Adam(
  net.parameters(), lr=0.0001
)

for epoch in range(3):
    for seqs, labels in dataloader_train:
        # Reshape model inputs
        seqs = ____
        # Get model outputs
        outputs = ____
        # Compute loss
        loss = ____
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
    print(f"Epoch {epoch+1}, Loss: {loss.item()}")
Edit and Run Code