I’m trying to do something similar (still not quite sure, still learning)
Trying to figure out if this is about the same problem:

  
      
      GitHub
  
  
    

ikostrikov/pytorch-flows 126

PyTorch implementations of algorithms for density estimation - ikostrikov/pytorch-flows