Your sampling is done as: z = x_var.sqrt() * torch.randn(x_mean.size()) + x_mean. As you can see, the torch.randn() part is independant of the way you use x_var and x_mean. So even though you cannot backprop through the randn op, you can backprop thourgh the multiply and sqrt op for x_var. This is known as the reparametrisation trick if I’m not mistaken in the litterature.