Differientiable Sampling and Argmax

WIP, last updated: 2019.12.6

Introduction

Softmax is a commonly used function for turning an unnormalized log probability into a normalized probability (or categorical distribution).

After softmax, we usually sample from this categorical distribution, or taking an argmax function to select the index. However, one can notice that neither the sampling nor the argmax is differientiable.

Researchers have proposed several works to make this possible. I am going to discuss them here.

Sampling

I will introduce Gumbel Softmax [1611.01144], which have made the sampling procedure differentiable.

Gumbel Max

First, we need to introduce Gumbel Max. In short, Gumbel Max is a trick to use gumbel distribution to sample a categorical distribution.

The Gumbel Max trick provides an alternative way of doing this. It use Reparameterization Trick to avoid the stochastic node during backpropagation.

Prove that

Prerequisites

Proof

which is exactly a softmax probablity. QED.

Reference: https://lips.cs.princeton.edu/the-gumbel-max-trick-for-discrete-distributions/****

Gumbel Softmax

Notice that there is still an argmax in Gumbel Max, which still makes it indifferentiable. Therefore, we use a softmax function to approximate this argmax procedure.

We note that the output of Gumbel Softmax function here is a vector which sum to 1, which somewhat looks like a one-hot vector (but it's not). So by far, this does not actually replace the argmax function.

To actually get a pure one-hot vector, we need to use a Straight-Through (ST) Gumbel Trick. Let's directly see an implementation of Gumbel Softmax in PyTorch (We use the hard mode, soft mode does not get a pure one-hot vector).

def gumbel_softmax(logits, tau=1, hard=False, eps=1e-10, dim=-1):
    # type: (Tensor, float, bool, float, int) -> Tensor
    r"""
    Samples from the Gumbel-Softmax distribution (`Link 1`_  `Link 2`_) and optionally discretizes.

    Args:
      logits: `[..., num_features]` unnormalized log probabilities
      tau: non-negative scalar temperature
      hard: if ``True``, the returned samples will be discretized as one-hot vectors,
            but will be differentiated as if it is the soft sample in autograd
      dim (int): A dimension along which softmax will be computed. Default: -1.

    Returns:
      Sampled tensor of same shape as `logits` from the Gumbel-Softmax distribution.
      If ``hard=True``, the returned samples will be one-hot, otherwise they will
      be probability distributions that sum to 1 across `dim`.

    .. note::
      This function is here for legacy reasons, may be removed from nn.Functional in the future.

    .. note::
      The main trick for `hard` is to do  `y_hard - y_soft.detach() + y_soft`

      It achieves two things:
      - makes the output value exactly one-hot
      (since we add then subtract y_soft value)
      - makes the gradient equal to y_soft gradient
      (since we strip all other gradients)

    Examples::
        >>> logits = torch.randn(20, 32)
        >>> # Sample soft categorical using reparametrization trick:
        >>> F.gumbel_softmax(logits, tau=1, hard=False)
        >>> # Sample hard categorical using "Straight-through" trick:
        >>> F.gumbel_softmax(logits, tau=1, hard=True)

    .. _Link 1:
        https://arxiv.org/abs/1611.00712
    .. _Link 2:
        https://arxiv.org/abs/1611.01144
    """
    if eps != 1e-10:
        warnings.warn("`eps` parameter is deprecated and has no effect.")

    gumbels = -torch.empty_like(logits).exponential_().log()  # ~Gumbel(0,1)
    gumbels = (logits + gumbels) / tau  # ~Gumbel(logits,tau)
    y_soft = gumbels.softmax(dim)

    if hard:
        # Straight through.
        index = y_soft.max(dim, keepdim=True)[1]
        y_hard = torch.zeros_like(logits).scatter_(dim, index, 1.0)
        ret = y_hard - y_soft.detach() + y_soft
    else:
        # Reparametrization trick.
        ret = y_soft
    return ret

When fowarding, the code use an argmax to get an actual one-hot vector. And it uses ret = y_hard - y_soft.detach() + y_soft, y_hard has no grad, and by minusing y_soft.detach() and adding y_soft, it achieves a grad from y_soft without modifying the forwarding value.

So eventually, we are able to get a pure one-hot vector in forward pass, and a grad when back propagating, which makes the sampling procedure differientiable.

from Eric Jang. https://blog.evjang.com/2016/11/tutorial-categorical-variational.html

Argmax

How to make argmax differentiable?

Intuitively, the Straight-Through Trick is also applicable for softmax+argmax (or softargmax + argmax). I am still not sure, needs more digging in the literature.

Some have introduced the soft-argmax function. It doesn't actually makes it differentiable, but use a continuous function to approximate the softmax+argmax procedure.

Discussion

  • Goal

    • softmax + argmax is used for classification, we only want the index with the highest probability.

    • gumbel softmax + argmax is used for sampling, we may want to sample an index not with the highest probability.

  • Deterministic

    • softmax + argmax is deterministic. Get the index with the highest probablity.

    • gumbel softmax + argmax is stochastic. We need to sample from a gumbel distribution in the beginning.

  • Output vector

    • softmax and gumbel softmax aboth output a vector sum to 1.

    • softmax outputs a normalized probability distribution.

  • Straight-Through Trick can actually be applied to both softmax + argmax and gumbel softmax + argmax, which can make both of them differentiable. (?)

Reference

Last updated