import torch


def threshold_fn(value):
    """Returns a function which takes a Tensor as input and returns a new Tensor
    in which all values with magnitudes less than `value` have been replaced
    with 0."""
    # I don't think there's a way to apply a threshold from below, so I cheat.
    def apply_threshold(tensor):
        signs = torch.sign(tensor)
        magnitudes = torch.abs(tensor)
        magnitudes = torch.nn.functional.threshold(magnitudes, value, 0.0)
        return magnitudes * signs
    
    return apply_threshold