Skip to content

Commit

Permalink
changed lod density for printing to account for the coordinate transf…
Browse files Browse the repository at this point in the history
…ormation
  • Loading branch information
daneschi committed May 14, 2024
1 parent 912395b commit c507f02
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
2 changes: 0 additions & 2 deletions linfa/maf.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,4 @@ def inverse(self, u, y=None):

def log_prob(self, x, y=None):
u, sum_log_abs_det_jacobians = self.forward(x, y)
print(torch.exp(sum_log_abs_det_jacobians))
exit()
return torch.sum(self.base_dist.log_prob(u) + sum_log_abs_det_jacobians, dim=1)
11 changes: 6 additions & 5 deletions linfa/run_experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,22 +235,23 @@ def train(self, nf, optimizer, iteration, log, sampling=True, t=1):

# Save normalized domain samples
np.savetxt(self.output_dir + '/' + self.name + '_samples_' + str(iteration), xkk.data.clone().cpu().numpy(), newline="\n")

# Save samples in the original space
if self.transform:
xkk_samples = self.transform.forward(xkk).data.cpu().numpy()
np.savetxt(self.output_dir + '/' + self.name + '_params_' + str(iteration), xkk_samples, newline="\n")
# Conpute the sample density using normalizing flow AND account for the coordinate transformation
xkk_logprob = nf.log_prob(xkk).data.cpu().numpy() - self.transform.compute_log_jacob_func(xkk).data.cpu().numpy().flatten()
else:
xkk_samples = xkk.data.cpu().numpy()
np.savetxt(self.output_dir + '/' + self.name + '_params_' + str(iteration), xkk_samples, newline="\n")
xkk_logprob = nf.log_prob(xkk).data.cpu().numpy()

# Save marginal statistics
np.savetxt(self.output_dir + '/' + self.name + '_marginal_stats_' + str(iteration), np.concatenate((xkk_samples.mean(axis=0).reshape(-1,1),xkk_samples.std(axis=0).reshape(-1,1)),axis=1), newline="\n")

# Save log density at the same samples
# np.savetxt(self.output_dir + '/' + self.name + '_logdensity_' + str(iteration), self.model_logdensity(xkk).data.cpu().numpy(), newline="\n")
np.savetxt(self.output_dir + '/' + self.name + '_logdensity_' + str(iteration), nf.log_prob(xkk).data.cpu().numpy(), newline="\n")
print('Current MC integral: ',np.exp(nf.log_prob(xkk).data.cpu().numpy()).mean())
# Save log density at the samples in the PHYSICAL SPACE (!)
np.savetxt(self.output_dir + '/' + self.name + '_logdensity_' + str(iteration), xkk_logprob, newline="\n")

# Save model outputs at the samples - If a model is defined
if self.transform:
Expand Down

0 comments on commit c507f02

Please sign in to comment.