@@ -485,14 +485,18 @@ def log_prob(self, input: Tensor, condition: Tensor, **kwargs) -> Tensor:
485485 self ._check_condition_shape (condition )
486486 self ._check_input_shape (input )
487487
488+ # Handle input with or without sample dimension
488489 has_sample_dim = input .dim () > len (self .input_shape ) + 1
489490 if not has_sample_dim :
490491 input = input .unsqueeze (0 )
491492
493+ # Apply z-score transform to input if enabled
492494 transformed_input = self ._transform_input (input )
493495
496+ # Get MoG from network
494497 mog = self .get_uncorrected_mog (condition )
495498
499+ # Change of variables: log p(x) = log p(z) + log|det(dz/dx)|
496500 log_probs = mog .log_prob (transformed_input )
497501 log_probs = log_probs + self ._log_det_jacobian_forward (
498502 input , transformed_input
@@ -513,6 +517,7 @@ def loss(self, input: Tensor, condition: Tensor, **kwargs) -> Tensor:
513517 Returns:
514518 Loss per batch element, shape (batch_dim,).
515519 """
520+ # Add sample dimension, compute log_prob, remove sample dimension
516521 log_prob = self .log_prob (input .unsqueeze (0 ), condition )
517522 return - log_prob .squeeze (0 )
518523
@@ -530,10 +535,13 @@ def sample(self, sample_shape: torch.Size, condition: Tensor, **kwargs) -> Tenso
530535 """
531536 self ._check_condition_shape (condition )
532537
538+ # Get MoG from network
533539 mog = self .get_uncorrected_mog (condition )
534540
541+ # MoG.sample returns (*sample_shape, batch_dim, dim) - matches sbi convention
535542 samples = mog .sample (sample_shape )
536543
544+ # Apply inverse transform to get samples in original space
537545 samples = self ._inverse_transform_input (samples )
538546
539547 return samples
0 commit comments