Sampling isn't differentiable in and . The reparameterisation trick writes with , so gradients flow through and .
VAEs predict ("logvar") for stability. Implement:
sample_z(mu, logvar, eps): (the noise eps is passed in)sample_grads(mu, logvar, eps, dz): given upstream gradient dz, return (dmu, dlogvar)