import torch


def clip_tensor(x, lb=0., ub=1.):
    """
    Clip a tensor to be within lb and ub
    :param x:
    :param lb: lower bound (scalar)
    :param ub: upper bound (scalar)
    :return: clipped version of x
    """
    return torch.clamp(x, min=lb, max=ub)