import torch


def sig(x, a):
    """
    Applies a sigmoid function on data in [0-1] range. Then rescales
    the result so 0.5 will be mapped to itself.
    """

    # Apply Sigmoid
    y = 1. / (1 + torch.exp(-a * x)) - 0.5

    # Re-scale
    y05 = 1. / (1 + torch.exp(-torch.tensor(a * 0.5, dtype=torch.float32))) - 0.5
    y = y * (0.5 / y05)

    return y