(z, dim=0)
return tau_z, k_z
z: any dimension
    dim: dimension along which to apply the sparsemax
z: any dimension
    dim: dimension along which to apply the sparsemax
z : any dimension dim : dimension along which to apply the sparsemax
def threshold_and_support(z, dim=0):
    """
    z: any dimension
    dim: dimension along which to apply the sparsemax
    """
    sorted_z, _ = torch.sort(z, descending=True, dim=dim)
    z_sum = sorted_z.cumsum(dim) - 1  # sort of a misnomer
    k = torch.arange(1, sorted_z.size(dim) + 1, device=z.device).float().view(
        torch.Size([-1] + [1] * (z.dim() - 1))
    ).transpose(0, dim)
    support = k * sorted_z > z_sum

    k_z_indices = support.sum(dim=dim).unsqueeze(dim)
    k_z = k_z_indices.float()
    tau_z = z_sum.gather(dim, k_z_indices - 1) / k_z
    return tau_z, k_z
def threshold_and_support ( z , dim = 0 ) : sorted_z , _ = torch . sort ( z , descending = True , dim = dim ) z_sum = sorted_z . cumsum ( dim ) - 1 # sort of a misnomer k = torch . arange ( 1 , sorted_z . size ( dim ) + 1 , device = z . device ) . float ( ) . view ( torch . Size ( [ - 1 ] + [ 1 ] * ( z . dim ( ) - 1 ) ) ) . transpose ( 0 , dim ) support = k * sorted_z > z_sum k_z_indices = support . sum ( dim = dim ) . unsqueeze ( dim ) k_z = k_z_indices . float ( ) tau_z = z_sum . gather ( dim , k_z_indices - 1 ) / k_z return tau_z , k_z