Workspace/Lesson workspace
Loading progress
Foundations40 min

What a neural layer actually computes

Lesson 1 of 3
How to study this lesson

Read one section, trace the worked example, and try the knowledge check. Bookmark saves a shortcut; notes appear in My notebook. Mark lesson complete is your own assessment and does not mark its lab passed.

By the end, you can

  • Trace affine transforms and nonlinear activations.
  • Explain why depth without nonlinearity remains linear.
  • Recognize saturation, dead units, and shape errors.

A neuron is a parameterized calculation

A simple neuron computes a weighted sum, adds a bias, and applies an activation: h = activation(w dot x + b). The weights determine which input directions matter; the bias shifts the point at which the activation changes behavior. Training adjusts those parameters to reduce a chosen loss. Nothing in this formula requires the neuron to correspond to a human-readable concept.

Take x = [2, -1], w = [0.5, 1], and b = 0.2. The affine value is 0.5 times 2 plus 1 times -1 plus 0.2, or 0.2. ReLU returns 0.2 because the input is positive. A second neuron with different weights can react to another direction of the same input. Stacking their outputs produces a learned feature vector.

Nonlinearity changes the family of functions

Two affine layers with no nonlinear activation between them can be collapsed into one affine layer. If h = W1x + b1 and y = W2h + b2, substitution gives y = W2W1x + W2b1 + b2. More parameters and more computation do not, by themselves, create a nonlinear decision boundary. The activation between layers is what prevents this collapse.

A small construction demonstrates the difference. For binary inputs a and b, set h1 = ReLU(a+b) and h2 = ReLU(a+b-1). Then output h1 - 2h2 is zero for [0,0] and [1,1], but one for [0,1] and [1,0]. This implements XOR on those four points. The network does not discover the weights in the example; they are deliberately chosen to expose the mechanism.

Shape accounting is an engineering tool

If each input has d features and a layer has h units, its weight matrix has h rows and d columns in the convention used here. Its bias has h entries. For a batch of n examples, frameworks often arrange data as n by d and multiply by the transpose of that weight matrix. Other conventions are valid, but changing conventions halfway through an implementation creates hard-to-diagnose bugs.

Count parameters before discussing model size. A dense layer from 3 inputs to 4 outputs has 12 weights and 4 biases, for 16 parameters. A second layer from 4 to 2 adds 8 weights and 2 biases. Batch size changes how many examples are processed together, not how many learned parameters the layer contains.

Activation choices influence optimization

ReLU outputs zero for negative inputs and passes positive inputs through. Its derivative is zero on the negative side and one on the positive side, with a convention needed at zero. A unit that remains negative for every training example may receive no useful gradient through its activation. Smooth bounded activations such as tanh avoid that exact dead region but can saturate, producing small derivatives at large magnitudes.

Initialization, normalization, learning rate, and architecture interact with these effects. Starting every hidden unit with identical weights can preserve symmetry, so the units learn redundant features. Extremely large initial weights can push bounded activations into saturation. These are mechanisms to investigate when training stalls, rather than reasons to declare one activation universally superior.

Representation and objective must fit the task

The last layer should match the output you intend to model. A scalar real-valued regression target needs a different interpretation from a vector of class logits. A softmax turns class logits into a normalized categorical distribution; independent sigmoid outputs instead permit multiple labels to be active at once. Choosing the wrong output interpretation can impose an unintended constraint before training even begins.

The runnable example verifies the XOR construction and counts the parameters of a separate two-layer architecture. It gives you a forward-pass reference with no library hiding the arithmetic. The exercise changes the target so you must reason about the features the hidden layer created. Later, backpropagation will automate how parameters change, but it will not decide whether the network's output contract matches the product's objective.

Attention, under your control

One query [q, 1], four fixed keys, and a two-dimensional head. Change the query or temperature and inspect where attention goes.

sensorkey [1, 0]
23.4%
alarmkey [0, 1]
23.4%
servicekey [1, 1]
47.5%
unrelatedkey [-1, 0]
5.7%

scoreᵢ = (query · keyᵢ) / (√2 × temperature). Then apply softmax. Lower temperature concentrates weight; it does not add evidence. These four labels are synthetic teaching tokens, not a trained model.

Work through the code

The two ReLU features implement XOR using hand-selected weights; there is no training here. A separate parameter-count calculation shows that a 3-to-4-to-2 dense network has 26 parameters including biases. Change one hidden bias and inspect all four binary inputs to understand its role.

m03_lesson_1.py
python
def relu(value):
    return max(0.0, value)

def xor_network(a, b):
    first = relu(a + b)
    second = relu(a + b - 1)
    return first - 2 * second

def dense_parameters(inputs, outputs):
    return inputs * outputs + outputs

for a, b in ((0, 0), (0, 1), (1, 0), (1, 1)):
    result = xor_network(a, b)
    print(f"{a} xor {b} -> {result:.0f}")

sizes = (3, 4, 2)
counts = [dense_parameters(a, b) for a, b in zip(sizes, sizes[1:])]
print("layer parameters:", counts)
print("total parameters:", sum(counts))
EXPECTED / ILLUSTRATIVE OUTPUT
0 xor 0 -> 0
0 xor 1 -> 1
1 xor 0 -> 1
1 xor 1 -> 0
layer parameters: [16, 10]
total parameters: 26

Run Python snippets locally with the prerequisites named above. The module coding lab runs directly in your browser.

Pause and reason

Using the hidden features h1=ReLU(a+b) and h2=ReLU(a+b-1), construct AND and OR outputs for binary inputs without adding another hidden unit.

Check your understanding

What does inserting several affine layers without nonlinear activations add to the function family?

Your notes

Explain the mechanism in your own words. Add a failure you want to test.

Saved notes appear in your notebook

Go deeper with primary sources

Practice this module