(tensor, density)
return topk.data[-1]
Computes a magnitude-based threshold for given tensor.

    :param tensor: PyTorch tensor
    :type tensor: `torch.Tensor`
    :param density: Desired ratio of nonzeros to total elements
    :type density: `float`
    :return: Magnitude threshold
    :rtype: `float`
Computes a magnitude-based threshold for given tensor.
Computes a magnitude - based threshold for given tensor .
def threshold(tensor, density):
    """
    Computes a magnitude-based threshold for given tensor.

    :param tensor: PyTorch tensor
    :type tensor: `torch.Tensor`
    :param density: Desired ratio of nonzeros to total elements
    :type density: `float`
    :return: Magnitude threshold
    :rtype: `float`
    """
    tf = tensor.abs().view(-1)
    numel = int(density * tf.numel())
    if numel == 0:
        raise RuntimeError('Provided density value causes model to be zero.')

    topk, _ = torch.topk(tf.abs(), numel, sorted=True)
    return topk.data[-1]
def threshold ( tensor , density ) : tf = tensor . abs ( ) . view ( - 1 ) numel = int ( density * tf . numel ( ) ) if numel == 0 : raise RuntimeError ( 'Provided density value causes model to be zero.' ) topk , _ = torch . topk ( tf . abs ( ) , numel , sorted = True ) return topk . data [ - 1 ]