Homework — two spirals
The rings turned out too easy? Try harder data — two twisted spirals.
Write a new file spiral.py that:
-
generates 1 000 points from two spiral arms. For each point:
y = a random class, 0 or 1 t = a random number in [0, 3π] angle = t + π · y # the second arm is turned by half a revolution radius = t / (3π) point = (radius · cos(angle), radius · sin(angle)) + 0.02 · noise -
trains three models on the same data (seed 42, batch 32, Adam
lr = 0.01, 100 epochs):LinearClassifier(2, 2),MLP(2, (16,), 2)andMLP(2, (32, 32), 2); -
prints each one's parameter count and training accuracy.
Use your model.py and train.py; fit prints a line per epoch — silence it with
contextlib.redirect_stdout(io.StringIO()).
My machine:
linear 6 parameters train accuracy 63.7%
mlp (16,) 82 parameters train accuracy 97.0%
mlp (32, 32) 1218 parameters train accuracy 99.6%
A few things to notice:
- The linear model gets 63.7 % here, not 50 % — spirals are not circles and a line can "cut off" part of them. Still, most points are wrong.
- From 6 numbers to 82 is a huge jump (+33 points). From 82 to 1 218 it is barely +2.6.
- Try
(16,)with other seeds and see how much 97.0 % "jumps". One number is not yet a conclusion.
My generator is not necessarily identical to yours (it depends on the order in
which you call torch.rand and torch.randn), so your numbers may differ by a few
points. If the linear model gives 50–70 % and the MLP > 95 % — all is well.