A neuron from tensors — and how the lesson climbs
In lesson one we defined the neuron in words: inputs, weights, sum, output. In lesson two you saw that "a weighted sum" is a matrix product. In this lesson you build the neuron yourself, from bare tensors, with no nn.Module.
Why without the framework? So that when you get it you know what it does for you. For now PyTorch gives you only arithmetic on tensors — and that is enough.
What you will build
The whole lesson fits in four functions in one file, neuron.py:
| function | what it does | shape |
|---|---|---|
neuron(x, w, b) |
weighted sum + bias | [n, 784] @ [784, 10] + [10] → [n, 10] |
softmax(z) |
scores → probabilities | [n, 10] → [n, 10], every row sums to 1 |
forward(x, W, b) |
both together | [n, 784] → [n, 10] |
accuracy(probs, y) |
how many are right | a number 0–1 |
One more (sigmoid) is given — it is there to show what one "switch-on" function looks like.
"One" neuron has ten outputs
The lesson title says "a single neuron", but the classifier has to tell ten classes apart. One neuron can only say one number, so we use ten neurons side by side: one per class. They all look at the same 784 pixels, but each has its own 784 weights and its own bias.
Ten neurons = one [784, 10] weight tensor. A single neuron — one column of it, W[:, 0]. In the lesson you will see one, and then all ten.
The lesson climbs in steps
As in the last one, every idea comes in three steps:
- Watch. One neuron, one picture, real numbers.
- Fill the holes.
neuron.pywith two empty places. - Write it yourself.
forwardandaccuracy.
Then: the first accuracy number, and two silent mistakes that raise no message at all.
Guess before reading on
The neurons have not learned yet: their weights are small random numbers. Write down:
- What will their accuracy be on 10,000 pictures? Why exactly that?
- Will some class be a "favourite"? If so — why, when the weights are random?
The answers are in the next step.