Start learning free

LESSON 138 OF 165 · MACHINE LEARNING: DEEP LEARNING WITH PYTORCH

nn.Linear and nn.Module

Practice

Last lesson you kept w and b as two tensors and wrote x * w + b yourself. PyTorch has this ready as a layer. nn.Linear(n_in, n_out) gives back a layer that takes n_in numbers per row and gives back n_out numbers per row. It holds its own weight and bias, which start as small random numbers, and calling the layer on a tensor works out x * w + b for every row.

layer = nn.Linear(2, 1)                    # 2 numbers in, 1 number out
out = layer(torch.tensor([[1.0, 3.0]]))    # one row of 2 numbers
print(out.shape)                           # torch.Size([1, 1])

Your own model is a class that inherits from nn.Module. In __init__, after super().__init__(), you store its layers on self. In a method forward(self, x) you say what the model does with an input, and return the result. You never call forward by name: model(x) runs it for you.

print(model) lists the layers the model holds, and model.parameters() gives back every weight and bias in them, each with requires_grad already on. With the layer in place, your code prints the model, its two parameters (a weight of shape [1, 1] and a bias of shape [1]) and the shape of the predictions, one per row of x:

Net(
  (linear): Linear(in_features=1, out_features=1, bias=True)
)
torch.Size([1, 1]) -0.007
torch.Size([1]) 0.536
Predictions: torch.Size([5, 1])

Your task: the class Net, its __init__ and the loop over model.parameters() are written for you. Store an nn.Linear(1, 1) in self.linear, add a forward method that returns self.linear(x), then store model(x) in pred and print "Predictions:" with pred.shape.


Assignment
Follow the Coding Tutorial to give Net a layer and a forward method, then run the model on x.


Hint
def forward(self, x): sits inside the class, at the same indent as def __init__. Call model(x), not model.forward(x).


STEP 1 OF 5
    Ctrl + Enter to check