# Gumbel Softmax in Julia

**URL:** https://discourse.julialang.org/t/gumbel-softmax-in-julia/26190
**Category:** General Usage
**Tags:** question, package
**Created:** [July 9, 2019, 3:12pm UTC](https://discourse.julialang.org/t/gumbel-softmax-in-julia/26190 "2019-07-09T15:12:08Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Elita\_Lobo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elita_lobo/32/9267_2.png) [@Elita\_Lobo](https://discourse.julialang.org/u/Elita_Lobo)
#### Post date: [July 9, 2019, 3:12pm UTC](https://discourse.julialang.org/t/gumbel-softmax-in-julia/26190/1 "2019-07-09T15:12:08Z")

</div>

Can someone please point me to some implementation of gumbel softmax in julia or an equivalent of the following function?  
[https://pytorch.org/docs/master/tensors.html#torch.Tensor.scatter\_](https://pytorch.org/docs/master/tensors.html#torch.Tensor.scatter_)

---

<div class="post-metadata">

### Author: ![robsmith11](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/robsmith11/32/29641_2.png) [@robsmith11](https://discourse.julialang.org/u/robsmith11)
#### Post date: [July 9, 2019, 5:43pm UTC](https://discourse.julialang.org/t/gumbel-softmax-in-julia/26190/2 "2019-07-09T17:43:14Z")

</div>

Isn’t the Gumbel softmax trick just the argmax of the sum of the log-probabilities and samples from the Gumbel distribution?

You should be able to something like

```julia
julia> using Distributions

julia> p = 0.8
0.8

julia> lps = log.([1.0 - p, p])
2-element Array{Float64,1}:
 -1.6094379124341005
 -0.2231435513142097

julia> ixs = map(_ -> argmax(lps .+ rand(Gumbel(), 2)), 1:10^6);

julia> mean(ixs .- 1)
0.799612

```

---

<div class="post-metadata">

### Author: ![cscherrer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cscherrer/32/7631_2.png) [@cscherrer](https://discourse.julialang.org/u/cscherrer)
#### Post date: [July 9, 2019, 8:31pm UTC](https://discourse.julialang.org/t/gumbel-softmax-in-julia/26190/3 "2019-07-09T20:31:14Z")

</div>

> [@robsmith11](#):
>
> Isn’t the Gumbel softmax trick just the argmax of the sum of the log-probabilities and samples from the Gumbel distribution?

You need _softmax_ in place of _argmax_. There are some details here:

> **[The Gumbel-Softmax Trick for Inference of Discrete Variables | Columbia...](https://casmls.github.io/general/2017/02/01/GumbelSoftmax.html)**
>
> This week we scrutinized, in a discussion led by Shizhe Chen, two recent papers: “The Concrete Distribution: a Continuous Relaxation of Discrete Random Varia...

There are implementations of softmax in Flux, Knet, or NNLib, or @Joshua_Bowles has a straightforward implementation with a nice description here:

> **[M2.1 Softmax in Julia](https://medium.com/@bowlescompling/m2-1-softmax-in-julia-1498901f741c)**
>
> The goal of this post is not to motivate and explain softmax, nor to provide the best softmax function for Julia, nor to introduce the…

EDIT: Neither of these give direct access to the log-density; it would be really nice to have this directly available in Distributions.jl

---

<div class="post-metadata">

### Author: ![Elita\_Lobo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elita_lobo/32/9267_2.png) [@Elita\_Lobo](https://discourse.julialang.org/u/Elita_Lobo)
#### Post date: [July 11, 2019, 6:05pm UTC](https://discourse.julialang.org/t/gumbel-softmax-in-julia/26190/4 "2019-07-11T18:05:00Z")

</div>

Thanks! I got it working!

---

<div class="post-metadata">

### Author: ![cscherrer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cscherrer/32/7631_2.png) [@cscherrer](https://discourse.julialang.org/u/cscherrer)
#### Post date: [July 12, 2019, 4:13pm UTC](https://discourse.julialang.org/t/gumbel-softmax-in-julia/26190/5 "2019-07-12T16:13:29Z")

</div>

Great! Is this in a public repo? I’d love to check it out
