(logits, temperature=1, hard=False)
return y_hard
ST-gumple-softmax
    input: [*, n_class]
    return: flatten --> [*, n_class] an one-hot vector
ST-gumple-softmax
    input: [*, n_class]
    return: flatten --> [*, n_class] an one-hot vector
ST - gumple - softmax input : [ * n_class ] return : flatten -- > [ * n_class ] an one - hot vector
def gumbel_softmax(logits, temperature=1, hard=False):
    """
    ST-gumple-softmax
    input: [*, n_class]
    return: flatten --> [*, n_class] an one-hot vector
    """
    y = gumbel_softmax_sample(logits, temperature)

    if not hard:
        return y

    shape = y.size()
    _, ind = y.max(dim=-1)
    y_hard = torch.zeros_like(y).view(-1, shape[-1])
    y_hard.scatter_(1, ind.view(-1, 1), 1)
    y_hard = y_hard.view(*shape)
    # Set gradients w.r.t. y_hard gradients w.r.t. y
    y_hard = (y_hard - y).detach() + y
    return y_hard
def gumbel_softmax ( logits , temperature = 1 , hard = False ) : y = gumbel_softmax_sample ( logits , temperature ) if not hard : return y shape = y . size ( ) _ , ind = y . max ( dim = - 1 ) y_hard = torch . zeros_like ( y ) . view ( - 1 , shape [ - 1 ] ) y_hard . scatter_ ( 1 , ind . view ( - 1 , 1 ) , 1 ) y_hard = y_hard . view ( * shape ) # Set gradients w.r.t. y_hard gradients w.r.t. y y_hard = ( y_hard - y ) . detach ( ) + y return y_hard