What about this one:
correct = total = 0
for batch_idx, (inputs, targets) in enumerate(trainloader):
    inputs, targets = inputs.to(device), targets.to(device)
    optimizer.zero_grad()
    outputs = model(inputs)
    loss = criterion(outputs, targets)
    loss.backward()
    optimizer.step()
    train_loss += loss.item()
    _, predicted = outputs.max(1)
    """
    if batch accuracy required
    total_batch = targets.size(0)
    correct_batch = predicted.eq(targets).sum().item()
    batch_acc = total_batch / correct_batch
    """
    correct += predicted.eq(targets).sum().item()
    total +=  targets.size(0)