Two normalized sensor readings describe a machine, and a score decides whether to send it to inspection. Suppose a machine should be inspected when the two readings disagree by a lot, whichever of them is larger. A linear score with a threshold splits the plane of readings along one straight line, so it cannot flag both sides of the diagonal while passing the middle. This lesson builds a small neural network that does, computes its forward pass by hand with the shape of every array, matches the output layer to the task, and states what the universal approximation theorem promises and what it leaves open. The readings and weights are constructed for teaching and do not come from a real inspection system.
Key terms
Neural network and hidden layer
Definition A feed-forward neural network maps an input in to an output through a fixed sequence of layers. Set and . For , layer computes
where the weight matrix is , the bias lies in , and the activation function acts on each entry of the preactivation . The output layer forms and applies an output function chosen for the task. Layers to are the hidden layers, each entry of is a unit, and is the width of layer . Every unit receives every value of the previous layer, so these layers are called fully connected. The parameters are all the weights and biases, so the architecture defines a family of functions , and training picks by minimizing a loss on training data. A hidden layer is hidden because the data contain no target values for its units. The loss is computed from the output alone.
In plain words Each hidden unit forms a weighted sum of the previous layer’s values, adds its bias and passes the result through the activation. A hidden layer therefore builds new features from the inputs, and the output layer combines them. Unlike features designed by hand, these features are set by parameters, and training decides what they become.
Example The network of this lesson reads two dimensionless readings , has one hidden layer of width with ReLU activation, and returns one score. Its parameters were chosen by hand to make the geometry visible, not fitted to data,
The hidden layer computes and the output layer computes the score . The hidden layer has weights and biases and the output layer weights and bias, so the network has parameters.
Activation function and ReLU
Definition An activation function is a fixed function from to that a layer applies to each entry of its preactivation vector. The rectified linear unit, ReLU, is
which equals for and for . It is continuous and piecewise linear but not linear. Its derivative is for and for , and at it has no derivative, so software uses a convention there, usually . Other common activations are the logistic sigmoid and . Hidden layers add flexibility only through a nonlinear activation. With the identity in place of , two layers compute
which is again an affine function of , and by induction any number of such layers computes a single affine map.
In plain words ReLU passes a positive signal unchanged and replaces a negative one by zero, so each hidden unit is switched on in one half of the input space and off in the other. The bends where units switch let a network build functions that are not linear. Without a nonlinear activation, extra layers change how the parameters are written but not the set of functions the model can compute.
Example In the network above the hidden units compute and . For every real number the identity holds, so the score is
With the declared rule “inspect when ”, the network inspects exactly the inputs whose readings differ by at least . That region consists of two closed half-planes, one on each side of the band around the diagonal . If both ReLUs are replaced by the identity, the hidden values and cancel in the output, the score is for every input, and nothing is ever inspected.
Forward propagation
Definition Forward propagation, also called the forward pass, evaluates a network on a given input with fixed parameters. It computes each layer’s preactivation and activation in dependency order, from the input through the hidden layers to the output, using the layer equations above. It changes no parameters. Training code keeps the intermediate values and , because the backward pass that computes gradients reuses them.
In plain words Push the numbers through the network one layer at a time. Each layer needs only the output of the layer before it, and writing down every intermediate value makes an arithmetic or coding error easy to locate.
Example For the input the first unit computes and the second , so
The score is nonnegative, so this machine is sent to inspection. For the preactivations are and , the hidden vector is and , so it is not inspected. For the score is exactly , and the declared rule inspects it. A rule written with would decide this case the other way, so the comparison operator is part of the model’s specification.
Output layer
Definition The output layer maps the last hidden vector to the quantity the task needs. Its output function, the format of the labels and the training loss have to describe the same task.
- A real-valued target uses an identity output, the prediction , with a regression loss such as squared error.
- A binary label in uses one score , the modeled probability and binary cross-entropy. The score is then called a logit.
- A task with mutually exclusive classes uses scores, the softmax of the score vector as the class probabilities and multiclass cross-entropy, with one class label per example.
- A task where several of labels can hold at once uses sigmoids, one binary cross-entropy for each label, and probabilities that need not add to .
A number between and produced by a sigmoid or softmax is the model’s probability. Whether such numbers match observed frequencies on new data is calibration, a separate property that must be checked on held-out data.
In plain words The hidden layers build features, and the output layer decides what kind of answer comes out. A regression output, a single binary probability and a probability vector over classes need different output functions, different label formats and different losses, and mixing them up gives a model that trains on the wrong question.
Example If the score of the inspection network is read as a logit, the input gets
The sigmoid is increasing with , so the rule is the same rule as , and the boundary input gets exactly . These weights were set by hand and never trained with cross-entropy, so is a score passed through the sigmoid, not an estimate of how often such machines actually need inspection.
Tensor shape
Definition In machine learning code a tensor is a multidimensional array, and its shape lists the length of each axis. For one input written as a column, is , a first layer of width has of shape , the vectors and are , and a scalar output with weight vector of shape gives of shape . For a batch of examples stored as the rows of an matrix , the same layer is
an array whose row is the transposed hidden vector of example . Here is the vector of ones, so copies the bias into every row, and libraries make this copy implicitly by broadcasting. Each layer’s input axis must have the length of the previous layer’s output axis, and which axis holds examples, features or classes is part of the specification.
In plain words Writing the shape next to every array is a cheap check that catches most wiring mistakes before any number is computed. Matching shapes do not prove the code is right, though. A square weight matrix used in the wrong orientation passes every shape check, and broadcasting can silently combine arrays whose shapes differ.
Example Stack the inputs , , and as the rows of a matrix . Then and are , and the scores form a vector with entries , , and , the same results as four separate forward passes. In NumPy, if the scores are stored with shape (4,) and the labels with shape (4, 1), their difference broadcasts to a array without any error, and a loss averaged over it is meaningless. Asserting the expected shape of each intermediate array catches this at once. Testing only the final decisions would not, since a wrong score can still fall on the right side of the threshold.
Universal approximation theorem
Definition Let be a continuous function from to that is not a polynomial, such as ReLU, the sigmoid or . Let be compact, that is, closed and bounded, and let be a continuous real-valued function on . Then for every there are a width , weight vectors in and numbers , and such that the one-hidden-layer network
with the preactivations , satisfies
If is a polynomial of degree , every such is a polynomial of degree at most , and such polynomials cannot approximate every continuous function on the cube , so the condition on cannot be dropped. A function with several outputs is approximated one output at a time. The theorem only asserts existence. It gives no practical bound on in general, no method that finds the parameters, no number of training examples, and no guarantee about accuracy on new data or outside . It also does not say that a network of some fixed width represents every function exactly.
In plain words With enough hidden units, a network with one hidden layer can stay within any chosen tolerance of a continuous target at every point of a bounded region, not only at the training points. That shows the family of functions is rich enough. Whether training finds such parameters from a finite, noisy sample, and whether they predict well on new cases, are separate questions answered by optimization and by evaluation on held-out data.
Example On take . The four-unit ReLU network
equals at , , , and and is linear between these points. Its largest error on is , reached halfway between neighboring points, for example against . Joining equal pieces in the same way takes units and has largest error , so ten units bring it to . The guarantee stops at the edge of . For the network is linear with slope , so against , and for it is constant, so against . These parameters were built from the known formula of , which is not how a network is fitted to data.
Check yourself
- Using the inspection network of this lesson, compute the hidden vector, the score and the decision for the input . Then find the score when both activations are replaced by the identity.
Show the answer
The preactivations are and , so and . The score is , which is at least , so the machine is inspected. The formula agrees. Read as a logit, the score gives .
With the identity, the hidden values and add to and the score is . The same happens for every input, because the two hidden units have opposite weights and equal output weights, so the linear version of this network inspects nothing.
- A network for three mutually exclusive classes has features, one hidden layer of width , and a batch of examples stored as rows. Give the shapes of the two weight matrices, the hidden array, the score array and the output, count the parameters, and name a suitable output function and loss.
Show the answer
With the convention of this page the first weight matrix is with a bias of length , so the hidden array is . The output layer needs one score per class, so its weight matrix is with a bias of length , and the score array is . Softmax applied to each row gives a output whose rows are probability vectors adding to , trained with multiclass cross-entropy against one class label per example. The parameter count is .
A single sigmoid output would have the wrong shape. Three separate sigmoids would answer three separate yes or no questions whose probabilities need not add to , which does not describe classes that exclude each other.
- Show that no linear rule “inspect when ” agrees with the network’s decisions at the three inputs , and . Would a second linear layer without an activation help?
Show the answer
The network’s scores are , and , so it passes the first input and inspects the other two. A linear rule that agrees needs , and . Adding the last two inequalities gives , which contradicts , so no weights work. Geometrically, the inspected set of a linear rule is a closed half-plane, the whole plane or empty, and each of these contains the midpoint of any two of its points. The network inspects and but not their midpoint .
A second linear layer does not help. A composition of affine maps is affine, so two linear layers without an activation compute one linear score and face the same contradiction. The ReLU between the layers is what makes the two-sided region possible.