Complete the code to initialize the training loss list.
train_losses = [1]We use an empty list [] to store training loss values over epochs.
Complete the code to calculate the average training loss for an epoch.
epoch_train_loss = sum(train_losses) / [1]
To get the average loss, divide the sum by the number of loss values using len(train_losses).
Fix the error in the code to append validation loss after each epoch.
validation_losses.[1](val_loss)add which is not a list method.extend which expects an iterable.insert without specifying an index.Use append to add a single loss value to the list.
Fill both blanks to compute and store average training and validation losses.
avg_train_loss = sum(train_losses) [1] len(train_losses) avg_val_loss = sum(validation_losses) [2] len(validation_losses)
Divide the sum of losses by the count to get the average for both training and validation.
Fill all three blanks to track losses during training and validation phases.
for epoch in range(num_epochs): model.train() train_loss = 0 for data, target in train_loader: optimizer.zero_grad() output = model(data) loss = criterion(output, target) loss.[1]() optimizer.step() train_loss += loss.item() train_losses.[2](train_loss / len(train_loader)) model.eval() val_loss = 0 with torch.no_grad(): for data, target in val_loader: output = model(data) loss = criterion(output, target) val_loss += loss.item() validation_losses.[3](val_loss / len(val_loader))
step instead of backward for gradients.step or wrong methods to add losses to lists.Call loss.backward() to compute gradients, then append average losses to the lists.
