Implement a PyTorch MLP from scratch to predict formation energies of inorganic crystals, with early stopping and learning-rate scheduling.
What you will learn
nn.Module, nn.Linear, and nn.ReLUReduceLROnPlateau) and plot the loss and LR curvesInstructions
Open the Colab notebook using the button above. Run each cell in order; cells marked Exercise require you to fill in code.
Set up the environment. Install torch, numpy, and matplotlib. Confirm torch.cuda.is_available() and choose the appropriate device.
Load and preprocess the dataset. Download the OQMD formation energy dataset subset. Standardise input features (composition-derived descriptors) using the training set statistics only.
Build the MLP. Complete the FormationEnergyMLP class: add hidden layers, ReLU activations, and dropout. Expose layer widths and dropout rate as constructor arguments.
Implement the training loop. Fill in the train_one_epoch() and evaluate() functions. Log train and validation MAE each epoch. Implement early stopping with patience=20.
Tune and analyse. Experiment with depth (2 vs. 4 layers) and width (128 vs. 512 neurons). Plot train/val MAE curves. Identify the epoch where the best validation MAE occurs.
Questions
Answer these questions as you work through the notebook. Discuss with your neighbour — some have no single right answer.
What is the MAE of a baseline model that always predicts the training set mean? How does your MLP compare?
EasyHow does adding dropout (p=0.3) affect training vs. validation MAE at convergence? Does it help or hurt, and why?
MediumPlot the gradient norm of the first layer over training. Do you observe vanishing or exploding gradients? How would you fix either?
MediumReplace the Adam optimiser with SGD + momentum (lr=0.01, momentum=0.9) with a cosine annealing schedule. Compare convergence speed and final MAE to Adam.
ChallengeResources