So i've designed my MLP and trained it, and now I get to play around with hyperparameter…
Private research annotation
Sign in to add a private research label or note.
- User
So i've designed my MLP and trained it, and now I get to play around with hyperparameter tuning. What do you think is the best way to do that?
class TitanicMLP(nn.Module): def init(self): super(TitanicMLP, self).init() # TODO: Define Layers # layer 1 self.layer1 = nn.Linear(7, 64) self.relu1 = nn.ReLU()
code# layer 2 self.layer2 = nn.Linear(64,32) self.relu2 = nn.ReLU() # layer 3 self.layer3 = nn.Linear(32,1) self.sigmoid = nn.Sigmoid() def forward(self, x): # TODO: Complete implemenation of forward # pass through first layer x = self.layer1(x) x = self.relu1(x) # activation # second layer x = self.layer2(x) x = self.relu2(x) # activation # third layer x = self.layer3(x) x = self.sigmoid(x) return xmodel = TitanicMLP()
TODO: Move the model to GPU if possible
device = torch.device('mps' if torch.mps.is_available() else 'cpu') model = model.to(device) print(model)
def train_model(train_loader, num_epochs, learning_rate):
we have provided the loss and optimizer below
criterion = nn.BCELoss() optimizer = optim.Adam(model.parameters(), lr=learning_rate)
train_losses = []
for epoch in range(num_epochs): # TODO: Compute the Gradient and Loss by iterating train_loader for i, (inputs, labels) in enumerate(train_loader): # make sure everything is on the gpu inputs = inputs.to(device) labels = labels.float().unsqueeze(1).to(device)
code# forward pass: Compute model predictions outputs = model(inputs) loss = criterion(outputs,labels) # backwards pass and optimization optimizer.zero_grad() # reset gradients before backprop loss.backward() # Compute gradients of the loss with respect to model parameters optimizer.step() # update parameters using new gradient # TODO: Print and store loss at each epoch train_losses.append(loss.item()) # update loss print(loss) # print loss - Assistant