import torch


def cap_fn(value):
    """Returns a function which takes a Tensor as input and returns a new Tensor
    where any elements with magnitude larger than `value` have been replaced
    with +/-`value`."""
    def apply_cap(tensor):
        return torch.clamp(tensor, -value, value)
    
    return apply_cap