Exercise: Neural networks ans
Exercise: Neural Networks – Answers
This page contains the answers and explanations for the Neural Networks exercise, covering data preparation, encoding schemes, overfitting, early stopping, hyperparameter optimisation and network architecture.
1. Window size and cleavage site
Question 1: Why do we want to use a window where the cut takes place at position K-2?
Hint: Look at the logo plot of the cleavage site on the original exercise page: Neural Networks Exercise.
Answer:
The window contains K-2 residues before the cleavage site and 2 residues after it.
The window is not centred because most of the biological signal determining the cleavage site is located upstream of the cut, as illustrated in the sequence logo plot.
2. Training, validation and test datasets
In this exercise, we use three independent datasets to train and evaluate artificial neural networks (ANNs).
Question 2: What are the three datasets used for?
Answer:
| Dataset | Purpose |
|---|---|
| Training set | The network learns its weights from this dataset. |
| Validation set | Used to select hyperparameters and determine which model performs best. |
| Test set | Used for the final evaluation of the selected model on unseen data. |
Key concept: The validation set exists so that the test set stays independent.
If we used the test set to select the window size (K), our reported performance could be overly optimistic because we would have indirectly optimised the model on the test data.
Question 3: What does homology reduction mean, and why do we do it?
Answer:
The three datasets are homology-reduced against each other, meaning that highly similar or closely related protein sequences are excluded across the different datasets according to a predefined similarity criterion.
This prevents information leakage between the training, validation and test sets and provides a more realistic evaluation of the network's ability to generalise to unseen protein sequences.
3. Amino acid encoding schemes
Question 4: Which encoding scheme do you think will be more useful for our network?
Make your prediction before completing the exercise and compare it with the final results.
Answer:
We expect BLOSUM62 encoding to potentially produce better results than one-hot encoding because it incorporates biological information about amino acid substitutions.
Comparison of encoding schemes
The following table illustrates the similarity between selected amino acids:
| Amino acid comparison | One-hot similarity (dot product) | BLOSUM62 score |
|---|---|---|
| L → L | 1 | 4 |
| L → I | 0 | 2 |
| L → D | 0 | −4 |
- One-hot encoding: Treats different amino acids as equally dissimilar.
- BLOSUM62 encoding: Incorporates evolutionary substitution information.
- The substitution L → I is relatively common.
- The substitution L → D is relatively uncommon.
Key concept: BLOSUM62 provides information about biologically meaningful relationships between amino acids, whereas one-hot encoding does not explicitly encode these relationships.
4. Overfitting and model generalisation
Question 5: Explain the plot in your own words
What are the red and blue curves? What happens after the green line? Is there a name for this effect?
Answer:
| Curve | Interpretation |
|---|---|
| Blue curve | Training loss |
| Red curve | Validation loss |
| Green line | Approximate point of optimal generalisation, where validation loss is minimised. |
The blue curve continues to decrease because the network becomes increasingly good at predicting the proteins it has already seen during training.
However, the red curve eventually starts increasing, indicating that the model is becoming worse at predicting unseen data.
After the green line, the network increasingly learns patterns that are specific to the training dataset rather than patterns that generalise to new data.
This phenomenon is called overfitting.
Overfitting occurs when a model learns the training data too specifically, including noise and dataset-specific patterns, resulting in reduced generalisation performance.
Question 6: Do you notice something unusual about the results?
Answer:
The difference between training and test performance is particularly interesting.
A large decrease in performance from the training set to the test set can indicate that the model has overfitted the training data.
This difference is known as the generalisation gap.
5. Early stopping
Question 7: What happens if we add early stopping?
Remove the # symbols in the next code cell and rerun the cells in points 5 and 6.
Can you observe any differences? Why?
Answer:
Early stopping monitors the performance of the model on the validation dataset during training.
When validation performance stops improving, training is terminated after a predefined stopping criterion is met.
If the best weights are restored, the model retains the weights corresponding to the best validation performance, approximately at the position of the green line in the previous plot.
The advantages of early stopping include:
- Reduced overfitting.
- Improved generalisation to unseen data.
- Reduced unnecessary training time.
- Less learning of training-specific noise.
Key concept: The best model is not necessarily the model that has been trained for the longest time.
6. Hyperparameter optimisation
Question 8: Which parameters performed best on your validation set?
Was one-hot or BLOSUM encoding better?
Tip: If the best model is located at one end of the grid search, consider extending the search space in that direction.
Grid search results
| Window size (K) | One-hot encoding | BLOSUM62 encoding | ||
|---|---|---|---|---|
| Validation | Test | Validation | Test | |
| 4 | 16.0% | 19.5% | 19.0% | 18.3% |
| 6 | 33.0% | 37.7% | 29.5% | 33.8% |
| 8 | 38.7% | 45.2% | 35.9% | 47.0% |
| 10 | 45.8% | 53.0% | 44.1% | 51.8% |
| 15 | 65.8% | 71.6% | 67.0% | 73.9% |
| 17 | 64.9% | 71.2% | 69.5% | 73.9% |
| 21 | 64.9% | 69.7% | 68.0% | 72.8% |
| 25 | 63.9% | 72.5% | 68.9% | 72.2% |
Best model
| Parameter | Selected value |
|---|---|
| Encoding scheme | BLOSUM62 |
| Window size (K) | 17 |
| Validation performance | 69.5% |
| Test performance | 73.9% |
Based on the validation dataset, the best combination of hyperparameters is BLOSUM62 encoding with a window size of K = 17.
The corresponding performance on the test dataset is 73.9%.
The optimal parameters may vary between runs due to random weight initialisation and stochastic training. In our experiments, however, BLOSUM62 with K = 17 performed best on the validation set.
Important: Hyperparameter selection should always be based on validation performance, not test performance.
7. Neural network architecture
Answer:
Without hidden layers, the neural network connects the input features directly to the output.
The model is therefore limited to learning a linear decision boundary in the encoded input space, making it similar to a logistic regression model for binary classification.
Expected characteristics:
- Fewer trainable parameters.
- Faster training.
- Lower risk of overfitting.
- Limited ability to learn complex, non-linear relationships.
- Potential risk of underfitting.
However, if the biological signal is relatively simple, a network without hidden layers may still perform surprisingly well.
How does the number of hidden neurons affect runtime and accuracy?
Answer:
Increasing the number of hidden layers and neurons increases the capacity of the neural network to learn more complex and non-linear relationships between input features.
However, increasing model complexity does not necessarily improve generalisation.
Experimental results
| Hidden layer architecture | Number of hidden layers | Reported weights | Validation | Test |
|---|---|---|---|---|
| () | 0 | 340 | 67.7% | 74.3% |
| (4,) | 1 | 1,364 | 68.1% | 73.6% |
| (16,) | 1 | 5,456 | 69.6% | 74.9% |
| (32,) | 1 | 10,912 | 69.5% | 73.9% |
| (64,) | 1 | 21,824 | 70.7% | 74.5% |
| (32, 16) | 2 | 11,408 | 68.4% | 74.3% |
| (64, 32, 16) | 3 | 24,336 | 63.4% | 71.0% |
Interpretation of results
- No hidden layers: The model already achieves relatively good performance with very few parameters.
- One hidden layer: Increasing the number of neurons generally improves validation performance in this experiment.
- Two hidden layers: Additional complexity does not improve performance compared with the best single-layer architecture.
- Three hidden layers: Validation and test performance decrease, potentially due to overfitting or difficulties during optimisation.
Effects of increasing network complexity
| Property | Expected effect |
|---|---|
| Number of trainable parameters | Increases |
| Training time | Generally increases |
| Computational cost | Increases |
| Ability to learn complex patterns | Increases |
| Risk of overfitting | Generally increases |
| Generalisation performance | May improve or decrease |
Best network architecture
Based on validation performance, the best architecture in our experiment is:
- Number of hidden layers: 1
- Number of hidden neurons: 64
- Validation performance: 70.7%
- Corresponding test performance: 74.5%
Although the architecture with 16 hidden neurons achieves a slightly higher test performance, the architecture with 64 neurons should be selected because model selection must be based on the validation dataset.
8. Summary and conclusions
| Concept | Main learning point |
|---|---|
| Window size | The window must capture the relevant biological signal around the cleavage site. |
| Dataset splitting | Training, validation and testing serve different purposes. |
| Homology reduction | Prevents information leakage between related protein sequences. |
| Encoding | BLOSUM62 incorporates evolutionary substitution information. |
| Overfitting | Better training performance does not necessarily imply better generalisation. |
| Early stopping | Helps prevent overfitting by monitoring validation performance. |
| Hyperparameter optimisation | Model selection must be based on the validation dataset. |
| Network architecture | More layers and neurons do not necessarily result in better predictions. |
Overall conclusion: The objective of training a neural network is not to achieve the best possible performance on the training dataset, but to achieve the best possible generalisation to previously unseen data.