(tensor)
return float(t.nonzero().numel()) / float(t.numel())
Computes the ratio of nonzeros to total elements in a tensor.

    :param tensor: PyTorch tensor
    :type tensor: `torch.Tensor`
    :return: Ratio of nonzeros to total elements
    :rtype: `float`
Computes the ratio of nonzeros to total elements in a tensor.
Computes the ratio of nonzeros to total elements in a tensor .
def density(tensor):
    """
    Computes the ratio of nonzeros to total elements in a tensor.

    :param tensor: PyTorch tensor
    :type tensor: `torch.Tensor`
    :return: Ratio of nonzeros to total elements
    :rtype: `float`
    """
    t = tensor.view(-1)
    return float(t.nonzero().numel()) / float(t.numel())
def density ( tensor ) : t = tensor . view ( - 1 ) return float ( t . nonzero ( ) . numel ( ) ) / float ( t . numel ( ) )