import torch


def l1_norm(latents):
    """
    Computes the L1 norm of for a batch of input vector
    This is the sparsity loss for a Laplacian prior
    Args:
        latents: torch tensor of any shape, but where first index is always batch
    Returns:
        sparse_loss: sum of abs of latents, averaged over the batch
    """
    reduc_dim = list(range(1, len(latents.shape)))
    sparse_loss = torch.mean(torch.sum(torch.abs(latents), dim=reduc_dim, keepdim=False))
    return sparse_loss