Explanation of the Neural Network Simulator — What the Coloring and the Line Thickness Compute

This page explains what the “Answer map,” the “line thickness,” and the “motion of the signal and the mistake” seen in the simulator correspond to in the language of neural networks, and by what formulas they are computed, in the order words → formulas → calculation procedure. The procedure for the detailed view, the conditions of the calculation, and questions for checking are also collected here.

1. About this page

The Neural Network Simulator is a device for watching how a small network with one hidden layer and one output looks at data and fixes its weights little by little. It covers the content of Chapter 14 (representations in neural networks) and Chapter 15 (backpropagation and learning).

The simulator has two views.

Scene A of the simple view performs the same calculation as the classification (concentric circles, two moons, XOR) in ② of the detailed view, and Scene B the same calculation as ① of the detailed view. We recommend reading in this order: try the simple view, check what it really shows in Section 2, read the formulas in Section 3, and operate the detailed view with the procedure in Section 4.

2. What the simple view really shows

The “lines” in the simple view are the weights of the network, and the “balls” are the units (neurons). The values of the input balls are multiplied by the weights of the lines and added (a weighted sum), and the result passed through the activation function becomes the value of the next ball (Sections 14.1 and 14.3 of the text). A line is drawn thicker the larger the absolute value |w| of its weight; positive weights are drawn as solid lines and negative weights as dotted (dashed) lines. This is the same convention as in the figure of ① in the detailed view. The “Answer (redness)” is the value ŷ of the sigmoid in the output layer and can be read as “the probability that this point is red (y = 1),” p(y = 1 | x).

“Learning” means moving all the weights little by little in the direction that makes the loss (the size of the mistakes) L on the training data smaller. “Mistake returns” means computing the partial derivatives (gradient) ∂L/∂w of the loss with respect to the weights in order from the output side, which is backpropagation (Section 3).

2.1 Scene A: Can a network separate points of two colors?

Red points (y = 1) and blue points (y = 0) placed on a plane are learned by a network with 2 inputs, H hidden units, and 1 output (2–H–1). The “Answer map” on the right shows, at each point x = (x1, x2) of the plane, the shading of the network output ŷ(x) (the same colors as in the classification figure of ② in the detailed view), and the black line is the contour ŷ = 0.5, that is, the decision boundary.

Table 1: Words on the screen in Scene A and the corresponding terms and symbols
Words on the screenTermSymbol or formula
(point position), Input, ↔ ↕input (two-dimensional)x = (x1, x2)
red points ● / blue points ▲class (target value)y = 1 / y = 0
How the points are arranged: Ring / Moons / Diagonalsynthetic data for teaching purposes: concentric circles / two moons / XORthe same generating formulas as the tasks in ② of the detailed view (Section 5.1)
Lines, line thicknessweights, absolute value of a weightWji(1), Wj(2), |w|
solid / dotted linepositive weight / negative weightw > 0 / w < 0
Middle balls, Number of middle balls (hidden units)hidden units (activation function tanh), number of hidden unitszj = tanh a(1)j, H = 1, 3, 8
pattern in ballthe output of a hidden unit drawn on the input plane (light = close to −1, dark = close to +1)zj(x)
Answer (redness), Answer mapoutput (sigmoid), its shadingŷ(x) = σ(a(2))
black line = borderdecision boundaryŷ = 0.5, that is, a(2) = 0
gray dotted = before the pressthe decision boundary before the button was pressed—
Times learned (1 time)epoch (using the 100 training data points once each; 13 updates with minibatches of 8)⌈100/8⌉ = 13
Points colored correctlyaccuracy on the training data (answers red if ŷ ≥ 0.5)number correct out of 100
Correct on new pointsaccuracy on the validation data (200 points not used for learning)number correct out of 200
Start over with other datachange the data seed and the random-number seedboth 20261112 + k

The output of middle ball j, on the input plane, has the form

zj(x)=tanh⁡(wjTx+bj(1))

tanh is a step that changes smoothly from −1 to +1, so zj(x) is a “ridge”-like function that approaches −1 on one side of the straight line wjTx + b(1)j = 0 and +1 on the other side (Section 14.5.2 of the text). The edge between light and dark in the pattern inside a ball is this straight line. The output is the value obtained by multiplying these by the weights W(2)j, adding them, and passing the sum through the sigmoid:

a(2)=∑j=1HWj(2)zj(x)+b(2),y^=σ(a(2))

With one ball (H = 1), ŷ is a monotonic function of a(1)1 alone, so the set of points where ŷ = 0.5 is a straight line on which a(1)1 takes a constant value (when |b(2)/W(2)1| < 1; otherwise the whole plane becomes one color):

y^=σ(W1(2)tanha1(1)+b(2)),a1(1)=W11(1)x1+W12(1)x2+b1(1)y^=12⟺a1(1)=artanh⁡(−b(2)W1(2))

This is why the border stays straight no matter how much the network learns, and it is the same reason that a single neuron cannot separate XOR (Section 14.4.2 of the text). With two or more balls, several ridges with straight edges are added together, so curved borders and closed borders can be made.

Numbers for “Try it” (all under the conditions when the page opens: data seed and random-number seed 20261112, learning 300 times):

The state after learning 300 times with Ring and 3 balls is the same as the state after 300 epochs of training in ② of the detailed view with the task set to “Concentric circles” and everything else at the defaults (accuracy: training 97.0 % / validation 96.0 %). If learning is continued to 2000 times (the button in the simple view advances 300 times at a time, so exactly 2000 times can be checked in ② of the detailed view), the training data reach 100 %, but the new points fall to 92 % (the same calculation as the overfitting example in Section 15.5.2 of the text).

Where the analogy fits and where it does not

Where it fits: the line thickness is the absolute value of the weight, and solid and dotted lines show the sign of the weight, exactly as they are. The answer map is made by computing the network output ŷ at each point of a grid on the plane and coloring it, and the black line is the contour ŷ = 0.5. Learning calls the same functions as ② of the detailed view (makeTask, initNet, and trainEpoch in neural-net.model.js) in the same order, and with the same seed it gives the same weights (checked by tests).

Where it does not fit: the “balls” and “lines” are a diagram of the calculation and do not copy the workings of real nerve cells. The biases b also move during learning, but they are not drawn in the picture. The points are dimensionless synthetic data for teaching purposes, not real measured values. Also, a high share of points colored correctly does not guarantee that new data will also be answered correctly (as in the 2000-times example, continued learning can lower the share on new points).

2.2 Scene B: How does a mistake reach the lines?

Using the same 2–2–1 network as in Section 15.3 of the text (tanh hidden layer, sigmoid output, cross-entropy loss) and one data point x = (1.0, 0.5), y = 1, one step of learning is shown in four stages. The initial weights are W(1) = ((0.5, −0.3), (0.8, 0.2)), b(1) = (0.1, −0.1), W(2) = (1.0, −1.0), b(2) = 0.2, and the learning rate is η = 0.5.

Table 2: Words on the screen in Scene B and the corresponding terms and symbols
Words on the screenTermSymbol or formula
Input balls 1.0, 0.5inputx = (1.0, 0.5)
Correct answer for this point (its true color): Red / Bluetarget valuey = 1 / y = 0
① Signal flowsforward propagationa(1)=W(1)x+b(1), z(1)=tanh⁡a(1), y^=σ(W(2)z(1)+b(2))
Answer (redness)output (predicted value)ŷ = 0.489467
② Compare, Gap from the correct answererror of the output layer (the pair of sigmoid and cross-entropy)δ(2) = ŷ − y = −0.510533
③ Mistake returnsbackpropagation (errors and gradients of the hidden layer)δ(1), ∂L/∂w
orange thickness = how much that line is fixedsize of the gradient (the amount actually moved is η times it)|∂L/∂w|
④ Fix the linesweight update by gradient descentw ← w − η ∂L/∂w
thicker / thinnerthe update increased / decreased the absolute value of the weight|wnew| > |wold| / |wnew| < |wold|
Answer after fixing the linesoutput of forward propagation for the same data with the updated weightsŷ = 0.709008
Times the lines were fixednumber of updates (steps)—

The amounts that come back in stage ③ are computed in order from the output side by the following formulas (the formulas of Section 3.2 for this device):

δ(2)=y^−y=0.489467−1=−0.510533δj(1)=h′(aj(1))Wj(2)δ(2),∂L∂Wji(1)=δj(1)xi,∂L∂Wj(2)=δ(2)zj

The key point is that the output-layer error δ(2) multiplies every gradient. The “gap” from the correct answer comes back, and each line is given “in which direction and by how much to fix it.” The orange thickness is drawn in proportion to |∂L/∂w| (line width 2 px + 22 × |∂L/∂w| px, at most 12 px).

Numbers for “Try it” (values checked by running trace in neural-net.model.js with Node; the same as Tables 15.1–15.3 and Example 15.3 in Section 15.3 of the text):

Where the analogy fits and where it does not

Where it fits: “the signal flows from left to right” is exactly the order of the forward-propagation calculation (input → hidden layer → output), and “the mistake goes back from right to left” is exactly the order of the backpropagation calculation (δ(2) → δ(1) → gradients on the input side). The numbers are computed with the same function trace as ① of the detailed view.

Where it does not fit: no actual particles flow along the lines; the motion of the particles is staging to show the order of the calculation. The size and speed of the particles have no meaning. The update of the biases b is not drawn in the picture (it is included in the calculation). Also, this scene updates with a single data point, so the weights move in the direction that fits only that data point. When learning from many data points, the mean gradient over a minibatch is used (Scene A, Section 15.5 of the text).

3. How it works

3.1 In words: stack weighted sums and fix the weights with the gradient of the loss

A neural network arranges in layers the calculation unit (neuron) “form a weighted sum and pass it through an activation function,” and uses the output of one layer as the input of the next (Sections 14.1–14.3 of the text). This device is limited to one hidden layer and one output. For classification, the output layer is a sigmoid, the output ŷ is read as “the probability that y = 1,” and the loss for one data point is the cross-entropy

ℓ=−[yln⁡y^+(1−y)ln⁡(1−y^)]

When the correct answer is 1, the loss is large if ŷ is close to 0, and close to 0 if it is close to 1. Learning means searching for the weights that make the mean loss L on the training data small, by gradient descent

w←w−η∂L∂w

(η is the learning rate; Chapter 7, Section 7.2 of the text). ∂L/∂w is needed for every weight, but the loss is a composite function “weighted sum → activation function → … → loss,” so using the chain rule in order from the output side lets us compute the common parts only once and hand out all the partial derivatives. This is backpropagation (Sections 15.1–15.2 of the text).

3.2 Formulas computed

A network with one hidden layer and one output. Input x ∈ ℝD, number of hidden units H, W(1) ∈ ℝH×D, W(2) ∈ ℝ1×H, activation function h(·) (tanh, ReLU, sigmoid).

a(1)=W(1)x+b(1),z(1)=h(a(1)),a(2)=W(2)z(1)+b(2)y^=σ(a(2))=11+e−a(2)(classification),y^=a(2)(regression)

The loss for one data point is the cross-entropy for classification (y ∈ {0, 1}) and half the squared error for regression. To avoid ln 0, the classification loss computes ln(1 + ea) in a form that does not overflow.

ℓ=−[ylny^+(1−y)ln(1−y^)]=ln(1+ea(2))−ya(2),ℓ=12(y^−y)2L=1|ℬ|∑n∈ℬℓn+λ2(‖W(1)‖2+‖W(2)‖2)

ℬ is the minibatch (a set of data indices) and λ is the weight-decay coefficient (not applied to the biases). Backpropagation finds δ ≡ ∂ℓ/∂a in order from the output side.

δ(2)=y^−y,δ(1)=h′(a(1))⊙(W(2)Tδ(2))∂ℓ∂W(l)=δ(l)z(l−1)T,∂ℓ∂b(l)=δ(l),z(0)=xh′(a)=1−tanh2a(tanh),σ(a)[1−σ(a)](sigmoid),1(a>0),0(a≤0)(ReLU)

The minibatch gradient is the mean of the per-sample gradients plus λW. The update is gradient descent w ← w − η ∂L/∂w (η is the learning rate). At the start of each epoch the order of the training data is shuffled randomly, and the weights are updated for each minibatch of size B (with “All,” one update per epoch).

3.3 Calculation procedure

  1. Set the initial values (Scene A and ②: Xavier normal random numbers for tanh and sigmoid, He for ReLU, biases 0; Scene B and ①: the weights of Section 15.3 of the text).
  2. Forward propagation: for each data point, compute a(1), z(1), a(2), ŷ, and the loss ℓ, and store the intermediate values.
  3. Backpropagation: starting from δ(2) = ŷ − y, use the stored z to find δ(1) and each gradient.
  4. Minibatch gradient: average the gradients within the minibatch and add the weight decay λW (λ = 0 in Scene A).
  5. Update: w ← w − η ∂L/∂w. Scene B and ① update once with one data point. Scene A and ② shuffle the training data at the start of each epoch and update for each minibatch of 8.
  6. Draw: in Scene A, compute ŷ on a grid over the plane (41 × 41 points for widths under 520 px, 61 × 61 points otherwise), color it, and draw the contour ŷ = 0.5. The pattern inside each ball is zj at 13 × 13 points.

The calculation in the simple view calls the same functions as the detailed view (makeTask, initNet, trainEpoch, forward, gridPredict, and trace in neural-net.model.js). The only things added for the simple view are the breaks for stopping learning partway to draw, the calculation that rounds the accuracy from counts to an integer percentage, the grid for the patterns inside the balls, and the calculation in Scene B that calls trace again with the fixed weights (repeat 5 times) (neural-net.easy.js).

3.4 Gradient check (③ in the detailed view)

In the gradient check in ③, from the loss with each parameter w alone moved by ±ε

∂L∂w≈L(w+ε)−L(w−ε)2ε(central difference; error O(ε2)),L(w+ε)−L(w)ε(forward difference; error O(ε))

we form these and compare them with the backpropagation value. The per-component relative error is |gbackprop − gdiff| / max(|gbackprop|, |gdiff|, 10−8), and the relative error as a vector is ‖gbackprop − gdiff‖ / max(‖gbackprop‖, ‖gdiff‖).

4. How to use the detailed view

The detailed view has three tabs: ① (following one step numerically), ② (watching training), and ③ (gradient checking). Each has the figure and the “Putting the current numbers into the formula” box on the left, and the controls and the readout on the right.

4.1 Procedure and what to observe

  1. In ①, press “Forward.” What to look at: a(1) = (0.45, 0.8), z(1) = (0.421899, 0.664037), a(2) = −0.0421378, ŷ = 0.489467, L = 0.714438. Although y = 1, ŷ is below 0.5, so the loss is a little larger than ln 2 ≈ 0.693.
  2. Press “Backward.” What to look at: the output error δ(2) = ŷ − y = −0.510533 is multiplied by the weights W(2) = (1.0, −1.0) and the tanh slope 1 − z2 and distributed to the hidden layer, giving δ(1) = (−0.419659, 0.285416). The sign of δ for hidden unit 2 is reversed because W(2)2 is negative.
  3. Press “Update weights.” What to look at: each weight moves by −η∂L/∂w, and the loss falls from 0.714438 to 0.343888. If you repeat from “Forward” again, the loss falls further.
  4. In ①, set the learning rate η to 5 and do the same. Next set y = 0. What to look at: when η is large, a single update overshoots. Changing y flips the sign of δ(2), and the signs of all gradients reverse.
  5. In ②, press “Run” with the task left at “Regression (Data A),” and change the number of hidden units H to 1, 3, and 8. What to look at: in the lower figure, the hidden-unit outputs zj(s) are “moving basis functions” whose positions and slopes change as training proceeds. Switching to “Contribution” shows that their superposition is the prediction curve above. Data A is smooth, so with the default seed the training RMSE after 2000 epochs is 0.945, 0.927, and 0.919 % for H = 1, 3, and 8, about the noise standard deviation of 1 %, and the validation RMSE (1.24, 1.24, 1.22 %) hardly changes either. Increasing H increases the shapes that can be represented, but this data does not need them.
  6. In ②, set the task to “Concentric circles” and train for about 2000 epochs (default H = 3). What to look at: the training loss keeps falling (0.0153 at 2000 epochs), but the validation loss reaches its minimum of 0.104 at epoch 806 and then starts to rise, reaching 0.360 at 2000 epochs (overfitting). Stopping at the epoch of minimum validation loss (◆) is early stopping. With H = 4 and the weight decay λ set to 0, 10−3, and 10−2, the validation loss at 2000 epochs is 0.342, 0.218, and 0.308; when λ is too large, the training loss also stops falling (0.0129, 0.0488, 0.214).
  7. In ②, set the learning rate near its maximum and the minibatch to 1. What to look at: the loss oscillates, and when it diverges the computation stops and the reason is displayed. Set “Initial value” to “All 0” and run. What to look at: with tanh the weights stay at 0 and do not move, and the prediction is a constant (the mean of the training data). With sigmoid the weights do move, but every hidden unit keeps the same value, so increasing H does not increase the shapes that can be represented (symmetry).
  8. In ③, vary ε from 10−12 to 10−1. What to look at: the error of the central difference is smallest near ε ≈ 10−5 (about 10−12); if ε is made too small the error grows because of rounding error, and if it is made too large the error grows because of truncation error (a V shape). On the large-ε side, the central-difference error grows with slope 2 and the forward difference with slope 1.

With the keyboard, Tab moves to buttons and sliders, Enter (or Space) presses a button, and the arrow keys move a slider. The tabs can be switched with ← and →.

5. Conditions of the calculation

5.1 Data and random-number conditions

5.2 Conditions fixed in the simple view

5.3 Simplifications

5.4 Conditions under which results become unstable

6. Questions for checking

Questions 1 and 2 can be answered with the simple view alone. Questions 3–6 use the detailed view.

  1. In Scene A of the simple view, with one middle ball the border stays straight no matter how many times the network learns. Why?
    Sample answer

    With one ball, the answer ŷ is determined only by a single weighted sum a(1)1 = W(1)11x1 + W(1)12x2 + b(1)1, and as a(1)1 increases, ŷ either only increases or only decreases. Therefore ŷ = 0.5 holds where a(1)1 takes one fixed value, that is, on a single straight line. Learning changes only the direction and position of the line. To surround only the inside of the ring, you need more balls so that several ridges with straight edges can be combined.

  2. In Scene B of the simple view, when you press “Repeat 5 times?”, the answer approaches 1, but the amount it moves in one round gradually gets smaller. Why?
    Sample answer

    Because the output gap δ(2) = ŷ − y multiplies the amount by which every line is fixed. The closer the answer gets to the correct 1, the closer the gap gets to 0 and the smaller the amount that comes back (the gradient), so even at the same learning rate the amount moved in one round decreases. The sum of squared gradients falls from 1.00151 in the first round to 0.282656 in the second.

  1. With the default values in ①, why are the signs of δ(1) for hidden units 1 and 2 opposite? Also, why is ∂L/∂W(1)12 exactly half of ∂L/∂W(1)11?
    Sample answer

    δ(1)j = (1 − zj2) W(2)j δ(2), and 1 − z2 > 0 and δ(2) = −0.510533 are common to both, so the sign is determined by W(2)j = +1.0, −1.0 (−0.419659 and +0.285416). ∂L/∂W(1)ji = δ(1)j xi, and x2/x1 = 0.5/1.0 (−0.209829 = 0.5 × (−0.419659)).

  2. In ①, one update changes the loss from 0.714438 to 0.343888. When η is made very small, what determines how much the loss decreases?
    Sample answer

    By the first-order Taylor expansion, L(w − ηg) ≈ L − η‖g‖2, so the decrease is proportional to the square of the gradient magnitude, ‖g‖2 = 1.00151 (over all 9 parameters). The actual decrease at η = 0.5, 0.370550, is smaller than 0.5 × 1.00151 ≈ 0.501, so it deviates from the first-order approximation (η = 0.5 cannot be called “small”).

  3. In ② with concentric circles, why does the validation loss rise while the training loss keeps falling? Where is it good to stop training?
    Sample answer

    Once the training data can be separated perfectly, the training loss keeps falling as the weights are enlarged to raise the confidence even for training points near the boundary, but if a validation point is misclassified with high confidence, the cross-entropy becomes large. Either stop at the epoch of minimum validation loss (◆ in the figure) (early stopping), or use the weight decay λ to keep the weights from growing.

  4. In ③, when ε is made smaller, why does the error decrease up to a point and then increase beyond it?
    Sample answer

    The truncation error of the central difference is about (ε2/6)|L‴| and decreases with ε, but L(w + ε) and L(w − ε) each carry a rounding error of about 10−16 × L, and dividing it by 2ε makes the rounding error grow in proportion to 1/ε. The error is smallest near ε ≈ 10−5, where the two balance.

7. Corresponding sections of the text and references

References (from the end of Chapters 14 and 15):

Back to the Neural Network Simulator | Machine Learning — From Probability to GPT (Contents) | Equations are typeset with the browser’s built-in MathML (no external library).