Project 5 of 14, for Paul
PyTorch Fundamentals
Take It Home: the digit recogniser rebuilt in PyTorch on your own computer, with tensors, autograd, nn.Module and a training loop, checked by a real pytest suite.
Sign up free to open itLog in
Act 7 needs a free account, so your work is saved as you go. Act 1 is open to everyone.
The brief
- PaulNearly everyone out there trains their networks in PyTorch, and I'd like to know if they're right to.
- PaulYour NumPy network reads the digits. Build the same one in PyTorch at home and see if it agrees.
- CaitlinWait, PyTorch does the backward pass for you? Then the cards were basically a waste of a week.
- PaulThe cards are how you'll know what it's doing when it goes wrong, though.
On your own computer, in reader.py:
to_tensors(X, labels): the greys as afloat32tensor and the labels asint64(torch.long).blackboard_gradients(): the blackboard chain from Neural Network from Scratch, in tensors made withrequires_grad=True. Afterbackward(), return dL/dw1 and dL/dw2 as plain floats.DigitReader(hidden=32), annn.Module:self.hiddenisnn.Linear(64, hidden)andself.outisnn.Linear(hidden, 10), with a ReLU between.forwardreturns the raw scores, with no softmax.train(model, X, labels, epochs, batch_size, rate, seed): Adam andnn.CrossEntropyLoss, in mini-batches shuffled by atorch.Generatorseeded withseed. It returns the loss on every row ofXafter each epoch, as floats.accuracy(model, X, labels): the fraction of rows whose biggest score is on the right digit, as a float, worked out undertorch.no_grad().python reader.pylearns from the same 1500 digits as the Digit Recogniser, and reads the same 297.
Hand in the pytest report when every test passes.
(.venv) $ python reader.py Loss: 0.481 after the first epoch, 0.035 after the last Read 291 of 297 digits it had never seen
What you learn
What's in it
- Tensors remember too
- A network as an object
- Zero, forward, loss, backward, step
- Softmax twice
- Your network, in PyTorch's words
- How Paul would do it
- The digit reader, at home
- Ship it