Repository: Lightning-AI/pytorch-lightning

Issue:
Incorrect total number of batches
**Describe the bug**
Sometimes total number of batches is computed wrong.

**To Reproduce**
Run the following code with current master branch:
```
from time import sleep
import torch
from torch.utils.data import DataLoader, Dataset

import pytorch_lightning as pl


class DummyDataset(Dataset):
    def __init__(self, n):
        super().__init__()
        self.n = n

    def __len__(self):
        return self.n

    def __getitem__(self, idx):
        return torch.rand(10)


class CoolSystem(pl.LightningModule):
    def __init__(self):
        super(CoolSystem, self).__init__()
        self.layer = torch.nn.Linear(10, 10)

    def forward(self, x):
        return self.layer(x)

    def training_step(self, batch, batch_nb):
        sleep(1)
        return {'loss': torch.mean(self.forward(batch) ** 2)}

    def validation_step(self, batch, batch_nb):
        sleep(1)
        return {}

    def validation_end(self, outputs):
        return {}

    def configure_optimizers(self):
        return [torch.optim.Adam(self.layer.parameters())]

    @pl.data_loader
    def train_dataloader(self):
        return DataLoader(DummyDataset(10), batch_size=1)

    @pl.data_loader
    def val_dataloader(self):
        return DataLoader(DummyDataset(5), batch_size=1)

model = CoolSystem()
trainer = pl.Trainer(weights_summary=None, nb_sanity_val_steps=0, early_stop_callback=False,
                     val_percent_check=1.0, val_check_interval=0.5)
trainer.fit(model)
```
At first output will look like:
`67%|█████▋                    | 10/15 [00:10<00:05,  1.05s/it, batch_nb=4, epoch=0, loss=0.194, v_nb=0]`

But at the end of the epoch it will be like:
`20it [00:20,  1.07s/it, batch_nb=9, epoch=0, loss=0.212, v_nb=0]`

Moreover, if you run
```
trainer = pl.Trainer(weights_summary=None, nb_sanity_val_steps=0, early_stop_callback=False,
                     val_percent_check=1.0, val_check_interval=0.5, check_val_every_n_epoch=10)
```
The first epoch will end at the point 
`67%|█████▋                    | 10/15 [00:09<00:04,  1.01it/s, batch_nb=8, epoch=1, loss=0.069, v_nb=0]` 

**Expected behavior**
Correct total number of batches.

**Possible solution**
Now we have `total_batches = nb_training_batches + nb_val_batches`, where `nb_val_batches` is the number of batches of only one validation loop. And the problem arises because actually there can be several validation loops during one training epoch. Moreover there is a parameter `check_val_every_n_epoch` and thus there can be no validation loops at all.

With this in mind, it looks like the correct formula is:
```
is_val_epoch = (current_epoch + 1) % check_val_every_n_epoch == 0
val_checks_per_epoch = nb_training_batches // val_check_batch if is_val_epoch else 0
total_batches = nb_training_batches + nb_val_batches * val_checks_per_epoch
```



Discussion:
I have the same problem..
@kuynzereb good catch. The solution then is to do what you suggest
```python
total_batches = nb_training_batches + nb_val_batches * val_checks_per_epoch
```

Do you want to submit that PR? That would be very helpful! 

Resolving patch:
diff --git a/pytorch_lightning/trainer/train_loop_mixin.py b/pytorch_lightning/trainer/train_loop_mixin.py
--- a/pytorch_lightning/trainer/train_loop_mixin.py
+++ b/pytorch_lightning/trainer/train_loop_mixin.py
@@ -23,7 +23,15 @@ def train(self):
             # update training progress in trainer and model
             model.current_epoch = epoch_nb
             self.current_epoch = epoch_nb
-            self.total_batches = self.nb_training_batches + self.nb_val_batches
+
+            # val can be checked multiple times in epoch
+            is_val_epoch = (self.current_epoch + 1) % self.check_val_every_n_epoch == 0
+            val_checks_per_epoch = self.nb_training_batches // self.val_check_batch
+            val_checks_per_epoch = val_checks_per_epoch if is_val_epoch else 0
+
+            # total batches includes multiple val checks
+            self.total_batches = (self.nb_training_batches +
+                                  self.nb_val_batches * val_checks_per_epoch)
             self.batch_loss_value = 0  # accumulated grads
 
             # limit the number of batches to 1 in fast_dev_run


Test patch: