Skip to chapter content
navan✳THE BUILD LABMACHINE LEARNING / GUIDE
0/28 read
CHAPTER 16 / 28 · SUPERVISED MODELS

Neural networks without mystique.

Follow layers, weights, activations, and gradients while keeping data limits in view.

BY NAVAN SAMILONG READTHEORY FIRST
01 / FROM THE BEGINNING

Start with the meaning.

A neural network is a composition of adjustable mathematical functions. A unit takes weighted input values, adds a bias, and applies an activation function. A layer contains many such units, and later layers receive the outputs of earlier ones. The whole network maps an input to a prediction. Its weights and biases are parameters learned from training examples. The word “neural” is a historical analogy; the program is not a little brain with human understanding.

Without nonlinear activations, stacking linear layers would still amount to one linear transformation. Nonlinearity lets a network represent bends and interactions. A hidden layer is simply an intermediate layer whose values are not the raw input or final output. More layers and more units increase representational capacity, but also increase the amount of data, computation, and careful tuning often required.

02 / THE MODEL

How the idea works.

Training uses a loss function to compare predictions with targets. Backpropagation computes how the loss changes with respect to each parameter by applying the chain rule through layers. An optimiser then updates parameters, often in batches of examples. A batch is a subset used for one update; an epoch is one pass over the training set. Learning rate, architecture, regularisation, and stopping time all affect the final model.

Networks can overfit, especially when many parameters are trained on a small or narrow dataset. Regularisation, early stopping, and representative validation data help, but none substitutes for a sound task definition. A neural network can discover complex feature relationships, yet it can also learn shortcuts from background, data source, or annotation artifacts. The more flexible the system, the more carefully its behavior needs to be examined outside training.

For ordinary tabular datasets, a neural network is one candidate among several, not an automatic first choice. Linear models and tree ensembles are often simpler to train and explain. Deep learning becomes especially important when inputs are high-dimensional and structured, such as images, audio, or language, where learned representations can replace extensive hand-designed features. This course explains the principle without pretending a small example covers modern large-scale architectures.

03 / PYTHON, EXPLAINED

Follow the code.

neural-networks.py
from sklearn.neural_network import MLPClassifier
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import make_pipeline

X = [[0, 0], [0, 1], [1, 0], [1, 1]]
y = [0, 1, 1, 0]
model = make_pipeline(
    StandardScaler(),
    MLPClassifier(hidden_layer_sizes=(8,), max_iter=2000, random_state=0),
)
model.fit(X, y)
print(model.predict([[1, 0]]))

The four inputs describe the XOR pattern: one active input corresponds to class 1, while zero or two active inputs correspond to class 0. A single straight boundary cannot separate those labels. The pipeline first scales numeric features, then fits an MLPClassifier with one hidden layer of eight units. The final line asks for a prediction on a row already present in training, so the output only illustrates interface and capacity. Convergence and the exact prediction can depend on optimisation settings; the code is not evidence of generalisation.

The tuple (8,) means one hidden layer containing eight units. max_iter limits optimisation iterations, and random_state fixes initial random choices. Scaling is fitted inside the pipeline, preserving one object that can later transform new rows consistently. A meaningful evaluation would use separate cases from the intended population and compare this network with simpler baselines.

04 / THE BOUNDARY

What the result does not say.

Large language and image models involve architectures, data scales, and objectives far beyond this small multilayer perceptron. The common thread is still learned parameters, a loss, optimisation, and evaluation on relevant unseen data.

A network's output can look confident even on unfamiliar inputs. Uncertainty, distribution shift, and human oversight are system-level concerns, not problems solved merely by adding layers.

05 / KEEP THIS

One clear idea.

A neural network composes trainable layers; its flexibility is useful only when data and evaluation support it.

THE CODE IN CONTEXT

The ML cookbook

Complete, explained Python workflows sit alongside this reading course.

Browse the cookbook
Finished reading?
Built with curiosity. By Navan Sami. ↗Machine learning with Python · From examples to trustworthy models.