def masked_average(tensor, mask, eps=1e-8):
    """
    Compute the average of the tensor while ignoring some masked elements.

    Args:
        tensor (Tensor): tensor to be averaged.
        mask (Tensor): a mask indicating the element-wise weight.
        eps (float): eps for numerical stability.

    Returns:
        FloatTensor: the average of the input tensor.

    """
    tensor = tensor.float()
    mask = mask.float()
    masked = tensor * mask
    return masked.sum() / mask.sum().clamp(min=eps)