Now comes one part of the magic behind VAEs: sampling to create the encoding that we will feed into the decoder. For reference, we are building something a little like this:
If you recall from earlier in the chapter, we need to add some noise during the sampling process, and we'll call this noise epsilon. This feeds into our sampled encoding; in Gorgonia, we can implement this with GaussianRandomNode with a mean of 0 and standard deviation of 1 as input parameters:
epsilon := gorgonia.GaussianRandomNode(g, dt, 0, 1, 100, 8)
We then feed this into our formula to create our sampled encoding:
if sz, err = gorgonia.Add(l3, gorgonia.Must(gorgonia.HadamardProd(gorgonia.Must(gorgonia.Exp(l4)), m.epsilon))); err != nil {
return errors.Wrap(err, "Layer Sampling failed")
}
log.Printf("sz shape %v", sz.Shape())
The preceding code might be difficult to read. In simpler terms, what we are doing is the following:
sampled = mean + exp(sd) * epsilon
This gives us a sampled encoding using both the mean and standard deviation vectors plus a noise component. This ensures that the result is not quite the same every time.