Neural Networks Explained from First Principles: Layers, Activations, and Backpropagation
Learn how multilayer perceptrons actually compute: weighted sums, nonlinear activations, forward pass, loss, and the backpropagation algorithm that trains them.
Neural networks can look mysterious because diagrams show many circles and arrows while software hides the arithmetic. At their core, however, they are carefully arranged mathematical functions whose parameters are adjusted from examples. A network receives numbers, repeatedly combines and transforms them, produces a prediction, measures how wrong that prediction is, and sends information about the error backward so that the next prediction can improve. This article develops that process from first principles. By the end, layers, activations, losses, and backpropagation should feel like connected parts of one system rather than isolated vocabulary.
From Linear Models to Nonlinear Function Approximation
A linear regression model predicts a scalar output from an input vector using
The weights determine how strongly each feature influences the prediction, and the bias shifts the result. For two inputs, every prediction lies on a plane. Linear models are useful precisely because they are constrained: they are fast, interpretable, and often statistically efficient. But the same constraint prevents them from representing curved or fragmented relationships.
Consider the XOR problem. It has two binary inputs, and its output is one when exactly one input is one:
No single straight line can separate the two positive cases from the two negative cases. Changing the weights only rotates or shifts the line; it does not make the boundary bend. A linear model therefore cannot solve XOR in its original feature space.
One response is manual feature engineering. For example, adding the interaction gives a linear model a feature capable of describing the needed relationship. Neural networks generalize this idea: hidden layers learn useful intermediate features rather than requiring us to specify all of them in advance.
It is important to understand what makes this possible. Stacking linear transformations alone does not add expressive power. If one layer computes
and another computes
then substitution gives
This is still one linear transformation plus a bias. The crucial ingredient is a nonlinear activation function placed between layers:
The activation prevents the entire stack from collapsing into one linear map. With enough hidden units, a network can approximate a broad class of continuous functions. This universal approximation property does not say that every network is easy to train, that a shallow network is always practical, or that the learned function will generalize. It says that nonlinear networks have the representational capacity to model highly varied relationships.
The Artificial Neuron
An artificial neuron has three conceptual operations. First, it forms a weighted sum of its inputs. Second, it adds a bias. Third, it applies an activation:
Here, is the pre-activation or logit, while is the neuron's output or activation. A positive weight means that increasing the corresponding input tends to increase ; a negative weight tends to decrease it. Larger absolute weights imply greater local influence, although interpretation becomes more complicated across many layers.
The bias deserves attention. Without it, the neuron's transition is anchored at the origin. For a threshold-like unit, that would force the decision boundary through the origin. The bias lets the model learn where an activation should turn on. One can mathematically treat it as a weight connected to a constant input of one, but keeping it separate is often clearer.
A single neuron divides input space with a hyperplane before applying its activation. Multiple neurons in a hidden layer can detect different regions or patterns. Later layers combine those detections. For an image, early units might react to oriented edges, middle units to textures or parts, and later units to class-relevant structures. For tabular data, hidden features are usually less visually obvious, but the compositional principle remains.
In vector form, a whole layer is
The superscript identifies the layer. Each row of contains the incoming weights for one neuron. Vectorization is not merely compact notation: numerical libraries process matrix operations much faster than Python loops over individual neurons.
Activation Functions: Strengths and Failure Modes
An activation should be chosen with both representation and optimization in mind. Hidden-layer activations create nonlinear features. Output activations also encode assumptions about the prediction target.
Sigmoid
The sigmoid function is
It maps every real input into , so it is natural for the output of a binary classifier. Its derivative has a convenient form:
The sigmoid's main hidden-layer weakness is saturation. For a large positive or negative , the output approaches one or zero and the derivative approaches zero. Backpropagated gradients are then multiplied by tiny values, causing learning in earlier layers to slow dramatically. Sigmoid outputs are also not zero-centered, which can make optimization less direct. Modern networks therefore rarely use sigmoid throughout their hidden layers, though it remains appropriate for independent binary probabilities and gates in architectures such as LSTMs.
Hyperbolic Tangent
The hyperbolic tangent is
It maps into and is zero-centered. Its derivative is
Tanh often works better than sigmoid for centered hidden representations, and it has historically been common in recurrent networks. Yet it still saturates for large , so deep stacks can suffer from vanishing gradients. It can be a sensible choice when bounded, signed activations are useful, but it is no universal default.
ReLU
The rectified linear unit is
For positive inputs its derivative is one; for negative inputs it is zero. ReLU is inexpensive, produces sparse activations, and avoids positive-side saturation. These properties made much deeper networks substantially easier to train.
Its characteristic failure is the dying ReLU. If updates move a neuron into a region where all observed pre-activations are negative, its output and gradient remain zero. The unit may never recover. Excessively high learning rates and poorly scaled inputs make this more likely. ReLU is also unbounded, so activations can become large if weights are unstable.
At , ReLU is not differentiable. In practice this causes no serious problem: implementations choose a subgradient, commonly zero, and exact zeros occupy a negligible part of continuous input space.
Leaky ReLU
Leaky ReLU keeps a small negative slope:
where might be . Because the derivative on the negative side is rather than zero, a unit can continue learning even when its input is negative. This reduces, but does not eliminate, dead-unit behavior. The slope is another design choice, and leaky ReLU remains unbounded. Variants such as PReLU learn the negative slope from data, while GELU and SiLU use smooth gates.
Softmax
Softmax converts a vector of logits into a probability distribution:
Every is positive and the probabilities sum to one. Softmax is therefore appropriate when exactly one of mutually exclusive classes is correct. Increasing one class's logit raises its probability while reducing the relative probabilities of others.
A numerically naive implementation can overflow when evaluating . Stable implementations subtract the largest logit:
This changes neither the ratios nor the probabilities. Another failure is conceptual: softmax is wrong for multi-label tasks where several classes can be true simultaneously. Such tasks generally use one sigmoid per class. Softmax probabilities can also be overconfident and should not automatically be interpreted as calibrated uncertainty.
Network Architecture: Inputs, Hidden Layers, and Outputs
The input layer represents features rather than performing learned computation. If a record has ten numeric features, the input usually has ten components. Preprocessing matters: continuous features often need standardization, categorical values need an encoding, and image pixels may be rescaled.
Hidden layers transform the inputs into learned representations. In a dense or fully connected layer, each neuron receives every activation from the previous layer. If a layer has inputs and neurons, it contains weights and biases.
The output layer must match the task:
- Regression often uses one linear output, or several linear outputs for multiple targets.
- Binary classification uses one sigmoid output.
- Mutually exclusive multiclass classification uses logits followed by softmax.
- Multi-label classification uses independent sigmoid outputs.
Width is the number of neurons in a layer; depth is the number of successive learned layers. A wide shallow network can theoretically approximate many functions, but it may require an enormous number of units. Depth allows hierarchical reuse: a deep network can combine simple features into intermediate features and then combine those into more complex ones. This often represents compositional functions more efficiently.
Depth also makes optimization harder. Gradients must travel through more operations, and the model gains more opportunities for unstable scaling. Width raises memory and computation costs and can increase parameter count dramatically. Architecture selection is therefore an empirical design problem, not a rule that deeper or wider is always better.
Forward Propagation in a Tiny 2-2-1 Network
Forward propagation evaluates the network from input to output. Consider two inputs, one hidden layer containing two sigmoid neurons, and one sigmoid output. Let
The first hidden pre-activation is
The second is
Applying sigmoid gives approximately
Now choose output weights and bias
The output pre-activation is
The final prediction is
Nothing random happens during this forward pass. Given fixed inputs and parameters, every value is determined. Training changes the weights and biases so that future forward passes produce better predictions.
The same calculation in NumPy makes the correspondence between notation and code visible:
import numpy as np
def sigmoid(z):
return 1.0 / (1.0 + np.exp(-z))
x = np.array([1.0, 2.0])
W1 = np.array([[0.5, -0.4],
[0.3, 0.8]])
b1 = np.array([0.1, -0.2])
W2 = np.array([[0.7, -0.6]])
b2 = np.array([0.2])
z1 = W1 @ x + b1
a1 = sigmoid(z1)
z2 = W2 @ a1 + b2
y_hat = sigmoid(z2)
print(z1, a1, z2, y_hat)
For a batch, becomes a matrix with one example per row or column, depending on the library convention. The underlying operations remain matrix multiplication, bias addition, and activation.
Loss Functions: Defining What “Wrong” Means
A loss converts a prediction and target into a scalar penalty. Training cannot improve “accuracy” in the abstract; it needs a differentiable objective whose gradient indicates how parameters should move.
Mean Squared Error
For scalar regression examples, mean squared error is
Squaring makes all errors nonnegative and penalizes large errors strongly. MSE corresponds to a Gaussian noise assumption with constant variance. It is smooth and natural for many regression problems, but it can be sensitive to outliers. Mean absolute error or robust losses such as Huber loss may be preferable when extreme residuals should have less influence.
MSE can technically train a classifier, but it is usually a poor fit. A sigmoid combined with MSE can produce weak gradients when the output saturates. Cross-entropy better reflects probabilistic classification and generally provides a more useful gradient.
Binary Cross-Entropy
For a binary target and predicted probability ,
If , the loss is ; assigning a tiny probability to the true outcome is heavily penalized. If , the loss is . Binary cross-entropy is the negative log-likelihood of a Bernoulli model.
In practice, frameworks often combine the sigmoid and cross-entropy into a “from logits” operation. This is more numerically stable than explicitly computing probabilities and then taking logarithms.
Categorical Cross-Entropy
For mutually exclusive classes with one-hot target vector and softmax probabilities ,
Only the log probability of the correct class contributes when the target is one-hot. Sparse categorical cross-entropy accepts an integer class label but represents the same objective. Class weighting can compensate when rare classes deserve more emphasis, although evaluation metrics should also reflect the real deployment costs.
The loss used for optimization and the metric reported to humans need not be identical. A classifier may optimize cross-entropy while reporting accuracy, precision, recall, F1, and calibration. The differentiable loss guides learning; metrics describe behavior.
Backpropagation: Credit Assignment with the Chain Rule
Forward propagation tells us the prediction. Backpropagation tells us how each parameter contributed to the loss. It is an efficient application of the chain rule, reusing intermediate derivatives from the output back toward the input.
Suppose a quantity affects , which affects , which affects loss . The chain rule says
Each factor answers a local question. How does loss change with activation? How does activation change with pre-activation? How does pre-activation change with the weight? Multiplying them connects the local effects.
For one sigmoid output trained with binary cross-entropy, an especially useful simplification occurs:
Call this output error signal . The gradient for an output weight is
and the output bias gradient is simply .
To send credit into the hidden layer, multiply by the outgoing weights and by the hidden activation derivative:
where denotes elementwise multiplication. Then
This backward recurrence is why the algorithm is efficient. A naive method could perturb every parameter separately and re-evaluate the network, but a large model may have millions of parameters. Backpropagation computes all gradients in roughly the same order of cost as a few forward passes.
Once a gradient is available, basic gradient descent updates a weight by
where is the learning rate. If the derivative is positive, decreasing the weight locally decreases loss; if it is negative, increasing the weight decreases loss. The learning rate controls the step size. Too small means slow progress. Too large can overshoot good regions, oscillate, or diverge.
Modern optimizers modify this basic update. Momentum smooths directions across steps. RMSProp rescales updates using recent squared gradients. Adam combines momentum-like estimates with adaptive scaling. These methods can accelerate optimization, but they do not remove the need for suitable data scaling, architecture, loss, and learning-rate selection.
Backpropagation is not a separate learning goal and it is not biologically realistic learning. It is a computational procedure for differentiating the chosen loss through the network. Automatic differentiation frameworks construct a graph during the forward pass and apply equivalent chain-rule operations backward.
Initialization and Gradient Stability
If every weight in a layer starts at the same value, every neuron receives the same gradient and remains identical. Random initialization breaks this symmetry. But the scale of randomness matters.
Weights that are too small can shrink activations and gradients as they pass through layers. Weights that are too large can make activations or gradients explode, or drive sigmoid and tanh units into saturation. Because backpropagation repeatedly multiplies derivatives and weight matrices, modest scaling problems can compound exponentially with depth.
Vanishing gradients leave early layers learning extremely slowly. They are especially associated with saturated sigmoid or tanh activations and long computational paths. Exploding gradients create huge unstable updates, non-finite losses, or wildly changing predictions.
Xavier or Glorot initialization chooses variance based on both fan-in and fan-out and is commonly paired with tanh or sigmoid:
He initialization is designed for ReLU-like activations:
These rules aim to keep signal variance reasonably stable across layers. Batch normalization or layer normalization can further stabilize internal scales. Residual connections provide shorter routes for signals and gradients in very deep networks. Gradient clipping, which caps a gradient's norm or value, is useful when occasional explosions occur, particularly in recurrent models.
Initialization does not determine the final model by itself, but it determines the starting optimization landscape. Different random seeds can lead to different results, so serious experiments should report variability rather than celebrating one fortunate run.
Batches, Epochs, and the Training Loop
A dataset with examples can be processed in three broad ways. Full-batch gradient descent computes one gradient from all examples before updating. Stochastic gradient descent uses one example per update. Mini-batch training uses a subset, such as 32, 64, or 256 examples.
Mini-batches dominate practice because matrix hardware can process them efficiently and because their gradients are less expensive than full-dataset gradients. Their sampling noise can also help optimization move out of narrow or unhelpful regions. Very small batches produce noisy estimates; very large batches require more memory and may need learning-rate adjustments.
An iteration or step is one parameter update. An epoch is one pass through the training set. With 10,000 examples and batch size 100, one epoch contains about 100 steps. Data are usually shuffled between epochs so that batches do not preserve accidental ordering.
A typical loop is:
- Select a mini-batch.
- Run forward propagation.
- Compute the batch loss.
- Backpropagate gradients.
- Update parameters.
- Repeat, while periodically evaluating untouched validation data.
The number of epochs is not a measure of model quality. Training too briefly leaves the model underfit; training too long can overfit. Validation curves, not an arbitrary round number, should guide the stopping point.
Regularization and Generalization
A model generalizes when it performs well on new examples drawn from the relevant population. Neural networks can contain enough parameters to memorize a training set, so low training loss alone is weak evidence.
Weight Decay
Weight decay discourages large weights. L2 regularization adds
to the objective. Its gradient pulls weights toward zero, encouraging smoother, less parameter-sensitive functions. In plain stochastic gradient descent, L2 regularization and weight decay are closely related. In adaptive optimizers they are not always equivalent, which motivates decoupled weight decay such as AdamW.
The coefficient controls strength. Too little has no practical effect; too much prevents the model from fitting real structure.
Dropout
During training, dropout randomly sets a fraction of activations to zero. The surviving activations are rescaled so that their expected magnitude remains stable:
The dropout rate is . Because units cannot rely on every collaborator being present, the network learns representations that are less co-adapted. At inference time, dropout is disabled.
Dropout is not always beneficial. High rates can cause underfitting, and architectures with normalization or abundant data may need little of it. Spatial networks often use structured dropout variants rather than independently removing every scalar activation.
Early Stopping
Early stopping monitors validation performance and keeps the parameters from the best epoch. If validation loss fails to improve for a chosen patience period, training ends. This both saves computation and limits the model's opportunity to fit training-specific noise.
The validation set must not become a disguised training set. Repeatedly choosing architectures from the same validation results can overfit the validation set. A final test set should remain untouched until major choices are complete.
Data augmentation, stronger data collection, and domain-aware preprocessing are also powerful regularizers. Often the best way to improve generalization is to expose the model to more realistic variation.
Overfitting and Underfitting
An underfit neural network performs poorly on both training and validation data. It may be too small, too strongly regularized, trained for too few epochs, optimized with an unsuitable learning rate, or deprived of useful features. Optimization failure can resemble underfitting, so check whether training loss is actually decreasing.
An overfit network performs well on training data but substantially worse on validation or test data. Its training loss may continue falling while validation loss reaches a minimum and rises. Common causes include too many parameters for the available data, leakage-prone features, too many epochs, weak regularization, and a distribution mismatch between training and validation sets.
The generalization gap between training and validation metrics is informative but must be interpreted carefully. Training-time dropout and augmentation can make training batches harder than validation examples, occasionally making validation performance appear better. A representative split and consistent evaluation mode are essential.
Learning curves help distinguish the cases. If both curves plateau at poor performance, increase useful capacity, improve features, or fix optimization. If training performance is excellent and validation performance is weak, collect more data, simplify the model, increase regularization, or stop earlier. If both are poor because labels are noisy or features contain little signal, a larger network will not manufacture information.
When a Neural Network Is Overkill
Neural networks are attractive, but model selection should begin with the problem rather than the trend. A linear or logistic model is often better when the relationship is roughly additive, the dataset is small, coefficients must be explained, inference must be extremely cheap, or calibrated uncertainty and statistical diagnostics are central.
Tree ensembles such as random forests and gradient-boosted trees are powerful baselines for medium-sized tabular datasets. They naturally handle thresholds and interactions, require less feature scaling, and often reach strong accuracy with less tuning than a dense neural network. A neural network may win on very large tabular datasets or when embeddings and multimodal inputs are important, but that should be demonstrated rather than assumed.
Neural networks become compelling for unstructured, high-dimensional data such as images, audio, natural language, and long sequences. Their representation learning can exploit spatial, temporal, and semantic structure. Specialized architectures—convolutional networks, transformers, recurrent networks, and graph neural networks—encode useful inductive biases.
Always establish a simple baseline. If logistic regression achieves the required performance, replacing it with a deep network adds training cost, monitoring burden, latency, and explanation difficulty without necessarily adding value. Complexity is justified by measurable improvements in the metrics that matter.
A Practical Training Checklist
Before training
- Define the target, unit of observation, and deployment decision.
- Choose a metric aligned with real error costs, not merely convenience.
- Split data by the way the future will arrive; use time-based or group-based splits when random splitting would leak information.
- Inspect class balance, missing values, duplicate records, outliers, and label quality.
- Fit preprocessing only on training data, then apply the learned transformation to validation and test data.
- Build a simple linear or tree baseline.
While designing the network
- Match the output activation and loss to the target type.
- Start with a modest architecture and add capacity only when evidence indicates underfitting.
- Use ReLU or a modern relative for ordinary hidden layers, with a compatible initializer.
- Standardize continuous inputs and verify tensor shapes.
- Calculate parameter count; accidental huge dense layers are common.
During training
- Plot training and validation loss by epoch.
- Track task-relevant metrics in addition to loss.
- Watch for NaN values, exploding activation scales, dead units, and gradients that are consistently near zero.
- Tune learning rate before performing a large architecture search.
- Save checkpoints and restore the best validation model.
- Use early stopping, appropriate weight decay, and augmentation or dropout when justified.
- Record random seeds, package versions, preprocessing choices, and split definitions.
Before deployment
- Evaluate once on an untouched test set.
- Break metrics down by meaningful subgroups and difficult cases.
- Check calibration and choose a decision threshold from operational costs.
- Measure latency, memory use, and batch behavior in the actual serving environment.
- Test malformed, missing, extreme, and out-of-distribution inputs.
- Plan monitoring for data drift, prediction drift, failures, and delayed ground truth.
This checklist cannot guarantee success, but it prevents many failures that architecture experimentation alone will never solve.
Visualizing a Network with Solver360
Equations become easier to retain when you can change a number and immediately see its effect. The free interactive Neural Network Calculator is designed for that purpose.
Start with a small architecture so every connection is interpretable. Choose two input features, a narrow hidden layer, and an output suitable for classification or regression. Observe how the diagram distinguishes input, hidden, and output layers. Increasing width adds parallel feature detectors; adding a layer creates another stage of composition.
Next, change weights and biases one at a time. A larger positive weight increases how strongly an input pushes a neuron's pre-activation upward. A negative weight reverses that influence. Changing the bias shifts the activation threshold without changing the input itself.
Compare sigmoid, tanh, and ReLU on the same pre-activation values. Notice sigmoid's bounded probability-like output, tanh's signed output, and ReLU's flat negative region. Extreme values make saturation and dead-ReLU behavior concrete. Then follow a sample through forward propagation, recording each weighted sum and activation before comparing the calculator's values with hand calculations.
Finally, experiment with architecture rather than treating the visualization as decoration. Ask what changes when a hidden neuron is removed, when all activations are linear, or when output logits are passed through softmax. The tool is most educational when each adjustment begins with a prediction: write down what you expect, make the change, and explain any discrepancy.
Frequently Asked Questions
1. Does every hidden neuron learn a human-readable feature?
No. Some early features, especially in image and audio models, may have recognizable interpretations such as edges or frequency patterns. But networks are not required to align one neuron with one human concept. Information can be distributed across many activations, and one neuron can participate in several behaviors. Even when a unit appears correlated with a concept, correlation does not prove that the unit alone causes the model's decision.
Interpretability methods can inspect activations, gradients, feature importance, and counterfactual behavior, but each method has assumptions and limitations. A useful mental model is that a hidden layer defines a learned coordinate system. Individual coordinates may be understandable, yet the represented information often lives in directions formed from combinations of coordinates.
2. Why are nonlinear activations necessary if the loss is already nonlinear?
The loss is applied after the network produces its output; it guides parameter learning but does not change the functional form of the network's forward mapping. If every layer in that mapping is linear, all layers collapse algebraically into one linear transformation, regardless of how nonlinear the loss is.
A nonlinear loss can create a complicated optimization surface over the parameters, but the final input-output boundary remains linear. Hidden activations are what let the model bend boundaries and construct interactions. The distinction is between the function used to make predictions and the function used to score those predictions.
3. How many hidden layers and neurons should I use?
There is no formula based only on input count. Required capacity depends on function complexity, data volume, noise, regularization, and architecture type. Start with the smallest plausible model, verify that the training pipeline works, and examine learning curves. If training and validation performance are both inadequate and optimization is healthy, add capacity. If training is strong but validation is weak, adding capacity is usually the wrong first move.
For a small tabular problem, one or two hidden layers with tens or hundreds of units may be enough, though tree ensembles deserve comparison. Images and language require specialized architectures whose depth and width are usually chosen from established families. Hyperparameter search is useful only after the split, metric, preprocessing, and baseline are trustworthy.
4. What exactly is a gradient, and why does its sign matter?
A gradient is a vector of partial derivatives. Each component estimates how rapidly the loss changes if one parameter changes slightly while the others remain fixed. A positive derivative means a small parameter increase raises loss locally, so gradient descent moves that parameter downward. A negative derivative means increasing the parameter lowers loss locally.
The gradient is local, not an instruction that remains valid for an arbitrarily large step. Neural-network loss surfaces curve, so a direction useful near the current point can become poor farther away. This is why learning rate matters and why optimizers update repeatedly rather than computing one gradient and jumping directly to a final answer.
5. Is backpropagation the same as gradient descent?
No. Backpropagation computes derivatives of the loss with respect to parameters. Gradient descent uses those derivatives to update parameters. They are complementary but distinct.
One could use backpropagated gradients with SGD, momentum, Adam, L-BFGS, or another gradient-based optimizer. Conversely, finite differences could approximate gradients without backpropagation, but doing so for millions of parameters would be prohibitively expensive and numerically fragile. Backpropagation is the efficient differentiation engine; the optimizer is the update policy.
6. Why can training loss decrease while accuracy stays unchanged?
Accuracy only changes when a prediction crosses a decision boundary. Suppose a binary model changes the true-class probability from to . Both predictions are correct, so accuracy is unchanged, but cross-entropy improves substantially. Similarly, moving a wrong probability from to reduces loss even if a threshold still labels it incorrectly.
This smooth sensitivity is why cross-entropy is useful for optimization. It supplies information before and after threshold crossings. If loss falls for a long time while validation accuracy does not improve, inspect calibration, class imbalance, threshold choice, and whether the selected metric reflects the desired behavior.
7. Can a neural network memorize the entire training set?
Often, yes. Large networks can fit random labels under some conditions, demonstrating that parameter count and optimization power can support memorization. Yet practical networks can still generalize because architecture, data structure, optimization, regularization, and augmentation bias learning toward some solutions over others.
Memorization capacity is not proof that a particular model memorized, nor is low training error proof of understanding. Generalization must be measured on representative unseen data. Privacy also matters: models can sometimes reveal rare training examples, so sensitive applications may require deduplication, access controls, privacy-preserving training, and explicit leakage testing.
8. Why do two training runs produce different models?
Random weight initialization, shuffled mini-batches, dropout masks, data augmentation, and some parallel hardware operations introduce randomness. Neural-network optimization is non-convex, so different runs can follow different paths and settle in different parameter regions. Their predictions may be similarly accurate overall while differing on individual cases.
Fixing seeds improves reproducibility but may not remove every source of nondeterminism. More importantly, one seed should not define a scientific conclusion. Compare several runs, report averages and variation, and investigate whether deployment decisions are stable across trained models.
9. Should I always use Adam?
Adam is a strong default for rapid experimentation because it adapts update scales and often works with less tuning than plain SGD. It is not automatically optimal. Momentum SGD can generalize better in some vision settings, AdamW handles weight decay more cleanly than naive Adam with L2 penalties, and specialized tasks may favor other optimizers.
Optimizer comparisons must use tuned learning rates and schedules. A poorly tuned SGD run does not prove Adam is intrinsically superior. Begin with a documented baseline, monitor validation behavior, and change optimizers only when the experiment answers a concrete question.
10. What does it mean when the loss becomes NaN?
NaN means invalid numerical arithmetic entered the computation. Common causes include an excessive learning rate, exploding weights or gradients, taking in an unstable custom loss, dividing by zero, invalid input values, or mixed-precision overflow.
Check the raw data and intermediate tensors for finite values. Lower the learning rate, use stable framework losses that accept logits, standardize inputs, inspect gradient norms, and consider clipping when explosions are expected. Find the first operation that becomes non-finite; restarting with a different seed may hide rather than fix the cause.
Next Steps: Gradient Descent and Convolutional Networks
The central story is now complete: weighted sums create pre-activations, nonlinear functions create expressive hidden representations, forward propagation produces predictions, a loss measures error, and backpropagation computes gradients for parameter updates. Training succeeds when this machinery is paired with representative data, stable optimization, suitable regularization, and honest evaluation.
Two natural topics deepen the picture. First, study gradient descent in detail: learning-rate schedules, momentum, adaptive methods, curvature, saddle points, and convergence diagnostics explain much of practical training behavior. Second, move from dense networks to convolutional neural networks. CNNs reuse local filters across space, dramatically reducing parameters while encoding the structure of images and other grid-like signals.
Keep returning to small numerical examples while learning advanced architectures. A transformer with millions of parameters and a two-neuron network use the same chain rule. Scale changes the engineering challenge, but the first-principles logic remains recognizable.
Continue reading
Calculus for Machine Learning: Derivatives, Gradients, and the Chain Rule
How derivatives, partials, gradients, Jacobians, and the chain rule turn a loss into parameter updates — the calculus behind backpropagation.
Linear Algebra for Machine Learning: Vectors, Matrices, and Transformations
The core linear algebra used in AI: vectors, matrix multiplication, rank, projections, eigenvalues, and why neural networks are mostly matrix multiplies.