Questionnaire
- Write the Python code to implement a single neuron.
- Write the Python code to implement ReLU.
- Write the Python code for a dense layer in terms of matrix multiplication.
- Write the Python code for a dense layer in plain Python (that is, with list comprehensions and functionality built into Python).
- What is the “hidden size” of a layer?
- What does the
t
method do in PyTorch? - Why is matrix multiplication written in plain Python very slow?
- In
matmul
, why isac==br
? - In Jupyter Notebook, how do you measure the time taken for a single cell to execute?
- What is “elementwise arithmetic”?
- Write the PyTorch code to test whether every element of
a
is greater than the corresponding element ofb
. - What is a rank-0 tensor? How do you convert it to a plain Python data type?
- What does this return, and why?
tensor([1,2]) + tensor([1])
- What does this return, and why?
tensor([1,2]) + tensor([1,2,3])
- How does elementwise arithmetic help us speed up
matmul
? - What are the broadcasting rules?
- What is
expand_as
? Show an example of how it can be used to match the results of broadcasting. - How does
unsqueeze
help us to solve certain broadcasting problems? - How can we use indexing to do the same operation as
unsqueeze
? - How do we show the actual contents of the memory used for a tensor?
- When adding a vector of size 3 to a matrix of size 3×3, are the elements of the vector added to each row or each column of the matrix? (Be sure to check your answer by running this code in a notebook.)
- Do broadcasting and
expand_as
result in increased memory use? Why or why not? - Implement
matmul
using Einstein summation. - What does a repeated index letter represent on the left-hand side of einsum?
- What are the three rules of Einstein summation notation? Why?
- What are the forward pass and backward pass of a neural network?
- Why do we need to store some of the activations calculated for intermediate layers in the forward pass?
- What is the downside of having activations with a standard deviation too far away from 1?
- How can weight initialization help avoid this problem?
- What is the formula to initialize weights such that we get a standard deviation of 1 for a plain linear layer, and for a linear layer followed by ReLU?
- Why do we sometimes have to use the
squeeze
method in loss functions? - What does the argument to the
squeeze
method do? Why might it be important to include this argument, even though PyTorch does not require it? - What is the “chain rule”? Show the equation in either of the two forms presented in this chapter.
- Show how to calculate the gradients of
mse(lin(l2, w2, b2), y)
using the chain rule. - What is the gradient of ReLU? Show it in math or code. (You shouldn’t need to commit this to memory—try to figure it using your knowledge of the shape of the function.)
- In what order do we need to call the
*_grad
functions in the backward pass? Why? - What is
__call__
? - What methods must we implement when writing a
torch.autograd.Function
? - Write
nn.Linear
from scratch, and test it works. - What is the difference between
nn.Module
and fastai’sModule
?
Further Research
- Implement ReLU as a
torch.autograd.Function
and train a model with it. - If you are mathematically inclined, find out what the gradients of a linear layer are in mathematical notation. Map that to the implementation we saw in this chapter.
- Learn about the
unfold
method in PyTorch, and use it along with matrix multiplication to implement your own 2D convolution function. Then train a CNN that uses it. - Implement everything in this chapter using NumPy instead of PyTorch.
In [ ]: