Skip to content

Step 1 of 5 · Reading · ~3 min

Read

Foundations

A Single Neuron

A neuron takes a list of numbers, takes a weighted vote on them, adds a bias and squashes the total. After this lesson your program evaluates one neuron exactly like the first unit of any network.

Weighted vote plus bias

z = w1*x1 + w2*x2 + ... + wn*xn + b
y = activation(z)

x are the inputs (features), w are the weights (one per input), b is the bias, and z is the pre-activation. The sum of products is the dot product of w and x. A weight says how hard its feature pushes the neuron toward "on": positive pushes up, negative pushes down, zero ignores it. The bias is the neuron's default lean before it has seen anything.

A worked trace

A toy spam neuron reads three counts: exclamation marks, ALL-CAPS words and links. Suppose training produced w = [0.8, 0.5, 1.2] and b = -2.0. For an email with x = [3, 2, 1]:

0.8*3 = 2.4
0.5*2 = 1.0
1.2*1 = 1.2
z = 2.4 + 1.0 + 1.2 - 2.0 = 2.6
sigmoid(2.6) = 0.9309   -> "93% spam"

Now feed it an empty email, x = [0, 0, 0]. Every product is 0, so z = b = -2.0 and sigmoid(-2.0) = 0.1192: a quiet default instead of the coin flip sigmoid(0) = 0.5. Without a bias, an all-zero input is stuck at 0.5 for every neuron you will ever build.

What one neuron can and cannot draw

The set of inputs where z = 0 is a straight line (a plane, in higher dimensions). On one side the neuron says "more than 0.5", on the other "less". One neuron therefore cannot compute XOR: no single line separates the points (0,0) and (1,1) from (0,1) and (1,0). Two hidden neurons draw two lines and an output neuron combines the regions. That is the entire argument for hidden layers.

The activation menu

ActivationFormulaRangeNote
sigmoid1/(1+e^-z)0..1probability-like; flat tails give tiny gradients
tanh(e^z - e^-z)/(e^z + e^-z)-1..1zero-centred; the recurrent cell uses it
ReLUmax(0, z)0..infgradient exactly 1 while active; can "die" if z stays negative
softmaxe^zi / sum(e^zj)sums to 1acts on a whole output layer, not one neuron

This exercise uses sigmoid, so reuse the overflow-safe version from the previous lesson.

Common mistakes

  • Forgetting the bias, or adding it inside the loop so it is counted once per input.
  • Reusing the name b for a loop variable over the weights. The bias you add at the end is then whatever the last weight was.
  • Printing 0.5 instead of 0.5000, or rounding an intermediate value before the final sigmoid.
  • Assuming a saturated neuron is an error. z = 200 is legal and the answer is simply 1.0000.

Your exercise

Each input line holds three fields separated by semicolons: the inputs, the weights and the bias. The starter parses them and prints your result with four decimals. Implement neuron so it returns the sigmoid of the dot product plus the bias, accumulating the dot product left to right.

Up nextLayers as Matrix OperationsFoundations

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…