liveBook · Manning

Mean squared error

As explained in Graph Neural Networks in Action

A loss function equal to the average squared difference between predicted and target values.

Mean squared error (MSE) averages the squared differences between predicted and target values. It turns regression error into a scalar loss that can guide model training: larger mistakes receive disproportionately larger penalties. section 5.4.2 section 6.2.2

Use MSE as a training and evaluation component in a straightforward pipeline:

  • Produce predictions from the model, such as molecular QED values or future sensor positions. section 5.4.2 section 6.2.2
  • Compare predictions with the corresponding target values through the criterion function. section 5.4.2 section 6.2.2
  • Backpropagate the loss during training, then calculate it on validation or test data without updating parameters. section 6.2.2

MSE inside a graph autoencoder loss

Molecular graph generation combines several objectives. Binary cross-entropy measures adjacency reconstruction, MSE measures QED prediction, and KL divergence regularizes the latent space; the three losses are summed for training. section 5.4.2

Listing 5.18 Loss for molecule graph generation
        def calculate_loss(self, pred_adj, \
   true_adj, qed_pred, qed_true, mu, logvar):
             adj_loss = F.binary_cross_entropy\
   (pred_adj, true_adj)   #1

             qed_loss = F.mse_loss\
(qed_pred.view(-1), qed_true.view(-1))     #2

             kl_loss = -0.5 * torch.mean\
(torch.sum(1 + logvar - mu.pow(2)\     #3
 - logvar.exp(), dim=1))

             return adj_loss + qed_loss + kl_loss

Inspect the PyTorch implementation where qed_loss is computed with F.mse_loss after flattening predicted and true QED values. section 5.4.2

Listing 6.3 Testing loop
   model.eval()   #1
   predictions = [] 
   test_losses = [] 
   seq_len = 40 

   with torch.no_grad():
        for i, (inputs, targets) in enumerate(testloader):
             inputs = inputs.to(device)
             targets = targets.to(device)

             preds = []
             for _ in range(seq_len):
                  output = model(inputs)
                  preds.append(output)

             inputs = torch.cat([inputs[:, 1:]\
, output.unsqueeze(1)], dim=1) \ #2

        preds = torch.cat(preds, dim=1)  #3
           loss = criterion(preds, targets)  #3
           test_losses.append(loss.item())  #3

           predictions.append(preds.detach().cpu().numpy())

    predictions = np.concatenate(predictions, axis=0)  #4
   test_loss = np.mean(test_losses)  #5

Trace how the same criterion evaluates multi-step predictions in a testing loop and contributes to the reported mean test loss. section 6.2.2

How Graph Neural Networks in Action covers it

How deep it goes
Defines MSE operationally and shows its PyTorch placement in training, validation, testing, and a composite VGAE objective.
Examples
Uses QED prediction, future sensor positions, PyTorch loss code, and multi-step testing as concrete examples.
What it assumes
Assumes model predictions, target labels, training loops, and backpropagation while using standard deep-learning workflow conventions.
What you can do after
Apply it via PyTorch to train regression models and measure validation or test prediction error.
How it connects
Connects MSE to graph autoencoders, property prediction, binary cross-entropy, KL divergence, RNNs, GRUs, and learning-rate scheduling.

Also explained in

Compare how 13 books explain mean squared error →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Graph Neural Networks in Action. The section links point to the parts of the book it draws on.