How to calculate gradient of the whole dataset?

I am trying to implement a model with two losses, say loss1 and loss2. Loss1 can be calculated per batch like regular SGD algorithm, but loss2 need the whole dataset to be calculated (for example, the average of all data’s reconstructions of the autoencoder). How to implement this model in pytorch? Thanks.

Use two data loaders where batch size for one is the size of the training set.