Solved by Dwight_Foster in post #2 
              
              
                Ok so for the first function you could do something like this 
epsilon = torch.normal(0,1, size=(batch, latent_dim))
z = z_mean + torch.exp(0.5 * z_logsigma) * epsilon

and for the loss you can use BCE with logits loss here.