Drills

Drills

Three tasks. Write them in the console or in a separate drills.py, and after each you are told what you should see.

Take the untrained model as in step 5:

torch.manual_seed(42)
x, y = load_fashion("train", n=10_000)
W = torch.randn(784, 10) * 0.01
b = torch.zeros(10)

1. Softmax "temperature"

With scores z = torch.tensor([[2.0, 1.0, 0.1]]) compute softmax(z). Now softmax(z * 10), softmax(z * 0.1) and softmax(z * 0).

Before you run it, guess: which comes out "very sure", which "completely undecided"?

Check:

z * 1   [0.6590, 0.2424, 0.0986]
z * 10  [1.0000, 0.0000, 0.0000]      (more exactly: 0.99995, 4.5e-05, 5.6e-09)
z * 0.1 [0.3661, 0.3312, 0.3027]
z * 0   [0.3333, 0.3333, 0.3333]

Bigger scores → a surer answer, the same order. When the neurons learn, this is exactly what they will do: increase the score gaps in the right places.


2. Accuracy per class

Write accuracy_per_class(probs, y) — it returns a list of 10 numbers: for each class c, the share of pictures labelled c for which the model said c.

Use y == c as a mask and run it on the untrained model.

Check:

T-shirt 0.0 %    Trouser 0.3 %    Pullover 0.2 %   Dress 0.0 %    Coat 0.0 %
Sandal  0.1 %    Shirt   0.9 %    Sneaker  0.0 %   Bag 30.3 %    Ankle boot 70.6 %

That is the step 5 table, only now from your own code. With ten numbers you see at a glance which class drags the overall accuracy down — in later lessons that is worth more than a single number.


3. Sigmoid versus softmax

Take the same z = [[2.0, 1.0, 0.1]]. Compute sigmoid(z) and softmax(z), and the sum of each.

Which of the two could be "the probability that the picture is class 0, 1 or 2 (one of three)"? Why not the other?

Check:

sigmoid  [0.8808, 0.7311, 0.5250]    sum = 2.137
softmax  [0.6590, 0.2424, 0.0986]    sum = 1.000

Sigmoid judges each score separately ("is this neuron on?"), so the sum can be anything. Softmax weighs them against each other, so it fits the question "which one of ten classes".