import torch


def tvr_loss(y):
    """Calculate Total Variation Regularization Loss of Generated Image"""
    loss = torch.sum(torch.abs(y[:, :, :, :-1] - y[:, :, :, 1:])) + torch.sum(
        torch.abs(y[:, :, :-1, :] - y[:, :, 1:, :])
    )
    return loss