import torch


def neglogpxz_bernoulli(x, x2):
    """
    Reconstruction loss in a Bernoulli posterior

    Args:
        x (torch.Tensor): :math:`\\mathbf{x}`
        x2 (torch.Tensor): :math:`\\mathbf{\\hat{x}}`
    
    Returns:
        torch.Tensor: :math:`-\\log\\mathrm{Bernoulli}(\\mathbf{x}|\\mathbf{\\hat{x}})` (without const.)
    """

    x = x.view(x.shape[0], -1)
    x2 = x2.view(x2.shape[0], -1)
    return (torch.xlogy(x, x2) + torch.xlogy(1 - x, 1 - x2)).sum(dim=1)