How to train a CNN model?
When trying to train the CNN model, I came across a code shown below:

def train(n_epochs, loaders, model, optimizer, criterion):

    for epoch in range(1,n_epochs):
        train_loss = 0
        valid_loss = 0

        model.train()
        for i, (data,target) in enumerate(loaders['train']):

            # zero the parameter (weight) gradients
            optimizer.zero_grad()            
            # forward pass to get outputs
            output = model(data)            
            # calculate the loss
            loss = criterion(output, target)            
            # backward pass to calculate the parameter gradients
            loss.backward()            
            # update the parameters
            optimizer.step()


Can someone please tell me why is the second for loop used? 
i.e; for i, (data,target) in enumerate(loaders['train']):

And why optimizer.zero_grad() and optimizer.step() is used?


torch.utils.data.DataLoader comes in handy when you need to prepare data batches (and perhaps shuffle them before every run). 

data_train_loader = DataLoader(data_train, batch_size=64, shuffle=True)

In the above code, first for-loop iterates through the number of epochs while second loop iterates through the training dataset converted into batches via above code. For example:

for batch_idx, samples in enumerate(data_train_loader):
    # samples will be a 64 x D dimensional tensor
    # batch_idx is each batch index


Learn more about torch.utils.data.DataLoader from here.
Optimizer.zero_gradient(): Before the backward pass, use the optimizer object to zero all of the gradients for the tensors it will update (which are the learnable weights of the model)
optimizer.step(): We generally use optimizer.step() to make the gradient descent step. Calling the step function on an Optimizer makes an update to its parameters. 

Learn more about these from here.

