John1231983:

for i in range(iter_size): loss = criterion(output, target_var) loss.backward() loss_sum += loss.item() # Change here loss_sum = loss_sum / iter_size


Thanks. This is my full code. It may help someone.
for epoch in range (max_epoch):
    for  iter in range(iter_size):
       for i, data in enumerate(trainloader):
            img, target_var = data
            output = net(img)
            loss = criterion(output, target_var) 
            loss.backward()
            loss_sum += loss.item() # Change here
       loss_sum = loss_sum / iter_size