How Neural Networks Work and Learn from Data
Machine LearningModel answerLast updated
Explain the structure and computation of a neural network, the role of non-linear activations, and how parameters are learned through loss minimization and backpropagation.
A technical interviewer asks you to describe what a neural network is and how it learns. Your answer should be thorough enough for someone with ML knowledge to accept as complete.
Specifically, cover the following:
-
Forward computation: How does a single neuron compute its output from inputs? How are neurons organized into layers, and what does a full forward pass through the network produce?
-
Non-linearity: Why are activation functions necessary? What would happen if a network used only linear transformations?
-
Learning: How does the network adjust its weights during training? Explain the role of a loss function, gradient computation via backpropagation, and an optimization method such as stochastic gradient descent.
-
Generalization: What is overfitting, and how can it be detected or mitigated? When might a simpler model be preferable to a neural network?
You may use a small concrete example to illustrate your explanation. Both intuitive and mathematical descriptions are welcome.
Hints
Hint 1
Think about what a single neuron computes: a weighted sum of inputs plus a bias, passed through a non-linear activation function. Stack many of these and you have a layer.
Hint 2
Without non-linear activations, stacking layers just collapses into a single linear transform — depth becomes meaningless.
Hint 3
Backpropagation applies the chain rule layer by layer to compute how the loss changes with respect to each weight. An optimizer like SGD then nudges weights in the direction that reduces the loss.
Hint 4
If the network memorizes training data but performs poorly on unseen data, that's overfitting. Validation sets and regularization techniques help detect and control this.
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
A neural network is a parametric model built from interconnected units called neurons, organized into layers. Each neuron computes a weighted sum of its inputs, adds a bias term, and passes the result through a non-linear activation function. Mathematically, for a neuron with input vector x, weight vector w, bias b, and activation function σ:
z = wᵀx + b a = σ(z)
A layer is a collection of neurons that each receive the same input vector but have their own weights and biases. The output of one layer becomes the input to the next. A feed-forward network with an input layer, one or more hidden layers, and an output layer computes a series of transformations:
h₁ = σ₁(W₁x + b₁) h₂ = σ₂(W₂h₁ + b₂) ŷ = WL h_{L-1} + bL
The final layer's activation depends on the task: softmax for multi-class classification, sigmoid for binary classification, or identity for regression.
Non-linear activations are essential because composing linear operations only produces another linear function. Without non-linearity, a multi-layer network collapses into a single linear transformation regardless of depth, making the extra layers pointless. Common activations include ReLU (max(0, z)), which is computationally cheap and helps with vanishing gradient issues, and tanh or sigmoid for certain output layers.
Training adjusts the weights to minimize a loss function that measures the gap between predictions and targets. For regression this might be mean squared error; for classification, cross-entropy loss. The network learns by computing the gradient of the loss with respect to every weight and bias, then updating parameters in the opposite direction of the gradient.
Backpropagation is the algorithm that computes these gradients efficiently. It works by applying the chain rule from the output layer backward through the network. At each layer, the gradient of the loss flows backward, combining the local derivative of the activation with the upstream gradient from the next layer. This gives the partial derivative of the loss with respect to each parameter in a single backward pass.
An optimizer such as stochastic gradient descent (SGD) uses these gradients to update weights. In mini-batch SGD, the network processes a small batch of examples, computes the average loss and gradients, and updates parameters:
W ← W − η · ∂L/∂W
where η is the learning rate. Variants like Adam adapt the learning rate per parameter and often converge faster.
Overfitting occurs when the network fits noise or spurious patterns in the training data rather than learning generalizable structure. It shows up as low training loss but high loss on a held-out validation set. Mitigation strategies include keeping the network architecture simple relative to the amount of data, using L1 or L2 weight regularization, applying dropout, and early stopping based on validation performance. Data augmentation can also help when applicable.
A concrete example: suppose you want to classify 28×28 grayscale images of handwritten digits into 10 categories. The input is a 784-dimensional vector. A small network might have two hidden layers of 128 and 64 ReLU units, followed by a 10-unit softmax output layer. Cross-entropy loss measures prediction error. Mini-batch SGD with a batch size of 32 and a learning rate of 0.01 trains the network over multiple epochs. After each epoch, you evaluate on a validation set and stop when validation loss stops improving.
When the dataset is small or the relationship between features and target is simple, a linear model, decision tree, or logistic regression may perform just as well while being faster to train, easier to interpret, and less prone to overfitting. Neural networks shine when there is enough data and the underlying patterns are complex enough to justify the added capacity.