Exemple #1
0
        model.train()
        total_loss = 0
        
        for batch in train_loader:         
            batch_X, batch_y = batch
            
            batch_X = batch_X.to(device)
            batch_y = batch_y.to(device)
            
            # TODO: Complete this train method to train the model provided.
​
            # zero accumulated gradients
            optimizer.zero_grad()
​
            # get the output from the model
            output = model.forward(batch_X)
​
            # calculate the loss and perform backprop
            loss = loss_fn(output, batch_y)
            loss.backward()
            optimizer.step()
​
            total_loss += loss.data.item()
        print("Epoch: {}, BCELoss: {}".format(epoch, total_loss / len(train_loader)))
​
​
if __name__ == '__main__':
    # All of the model parameters and training parameters are sent as arguments when the script
    # is executed. Here we set up an argument parser to easily access the parameters.
​
    parser = argparse.ArgumentParser()