Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- for e in range(epoch):
- loss_epoch = 0
- ################################################################################
- # TODO: #
- # Loop through the dataloader and train your model with nn.BCELoss. #
- ################################################################################
- for x, y in train_loader:
- optim.zero_grad()
- y_pred = model(x)
- # Compute and print loss
- loss = criterion(y_pred, y.float().unsqueeze(1))
- loss.backward()
- optim.step()
- loss_epoch += loss.item()
- ################################################################################
- # END OF YOUR CODE #
- ################################################################################
- if e % print_every == 0:
- y_pred = (model(X_train.float()) > 0.5)
- train_acc = get_acc(y_pred, y_train)
- y_val_pred = (model(X_val.float()) > 0.5)
- val_acc = get_acc(y_val_pred, y_val)
- print(f'Epcoh {e}: {loss_epoch}, Training accuracy: {train_acc}, Validation accuracy: {val_acc}')
- return model
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement