num_batches = 0
for sample, target in dataset:
    out = model(sample)
    loss = loss_fn(out, target)
    loss.backward()
    num_batches += 1
    if num_batches == 10: # optimize every 10 mini-batches
        optimizer.step()
        model.zero_grad() # or optimizer.zero_grad()
        num_batches = 0