import torch


def sample_from_binary_logits(l, coord1, coord2):
    """
    Args:
        l: B x 2 x H x W output of NN (logits)
        coord1
        coord2

    Returns:
        pixels: B x 1 pixel samples at location (coord1, coord2) in range [-1, 1]
    """
    assert l.size(1) == 2
    l = l[:, :, coord1, coord2]
    pixels = torch.distributions.categorical.Categorical(logits=l).sample()
    pixels = pixels * 2. - 1.
    return pixels.unsqueeze(1)