PyTorch Model
You will use the OOP approach to define the model architecture. Recall that this requires setting up a model class and defining two methods inside it:
.__init__(), in which you define the layers you want to use;forward(), in which you define what happens to the model inputs once it receives them; this is where you pass inputs through pre-defined layers.
Let's build a model with three linear layers and ReLU activations. After the last linear layer, you need a sigmoid activation instead, which is well-suited for binary classification tasks like our water potability prediction problem. Here's the model defined using nn.Sequential(), which you may be more familiar with:
net = nn.Sequential(
nn.Linear(9, 16),
nn.ReLU(),
nn.Linear(16, 8),
nn.ReLU(),
nn.Linear(8, 1),
nn.Sigmoid(),
)
Let's rewrite this model as a class!
Deze oefening maakt deel uit van de cursus
Intermediate Deep Learning with PyTorch
Oefeninstructies
- In the
.__init__()method, define the three linear layers with dimensions corresponding to the model definition provided and assign them toself.fc1,self.fc2, andself.fc3, respectively. - In the
forward()method, pass the model inputxthrough all the layers, remembering to add activations on top of them, similarly how it's already done for the first layer.
Praktische interactieve oefening
Probeer deze oefening eens door deze voorbeeldcode in te vullen.
import torch.nn as nn
import torch.nn.functional as F
class Net(nn.Module):
def __init__(self):
super().__init__()
# Define the three linear layers
self.fc1 = ____
self.fc2 = ____
self.fc3 = ____
def forward(self, x):
# Pass x through linear layers adding activations
x = nn.functional.relu(self.fc1(x))
x = ____
x = ____
return x