def linear_range_transform(tensor, from_range, to_range):
    """
    Linearly interpolate from the from_range into to_range.
    For example, if from_range = (0,1), and to_range=(-2, 5), then 0 is mapped
    to -2 and 1 is mapped to 5, and all the values in-between are linearly
    interpolated.

    * tensor: a numpy or Pytorch tensor, or a scalar.
    """
    fmin, fmax = from_range
    tmin, tmax = to_range
    return (tensor - fmin) / float(fmax - fmin) * (tmax - tmin) + tmin