Repository: sbi-dev/sbi

Issue:
self._neural_net not set in snle build posterior
https://github.com/mackelab/sbi/blob/02fde758334bd9292b4950b494496f66b6d2e9da/sbi/inference/snle/snle_base.py#L320

in case that a density estimator is passed to the function, the potential function still receives self._neural_net without self._neural_net being set to density_estimator beforehand


Discussion:
good point, thanks for reporting!

Resolving patch:
diff --git a/sbi/inference/snle/snle_base.py b/sbi/inference/snle/snle_base.py
--- a/sbi/inference/snle/snle_base.py
+++ b/sbi/inference/snle/snle_base.py
@@ -309,15 +309,16 @@ def build_posterior(
             prior = self._prior
 
         if density_estimator is None:
-            density_estimator = self._neural_net
+            likelihood_estimator = self._neural_net
             # If internal net is used device is defined.
             device = self._device
         else:
+            likelihood_estimator = density_estimator
             # Otherwise, infer it from the device of the net parameters.
             device = next(density_estimator.parameters()).device.type
 
         potential_fn, theta_transform = likelihood_estimator_based_potential(
-            likelihood_estimator=self._neural_net, prior=prior, x_o=None
+            likelihood_estimator=likelihood_estimator, prior=prior, x_o=None
         )
 
         if sample_with == "mcmc":
diff --git a/sbi/inference/snpe/snpe_base.py b/sbi/inference/snpe/snpe_base.py
--- a/sbi/inference/snpe/snpe_base.py
+++ b/sbi/inference/snpe/snpe_base.py
@@ -398,15 +398,16 @@ def build_posterior(
             prior = self._prior
 
         if density_estimator is None:
-            density_estimator = self._neural_net
+            posterior_estimator = self._neural_net
             # If internal net is used device is defined.
             device = self._device
         else:
+            posterior_estimator = density_estimator
             # Otherwise, infer it from the device of the net parameters.
             device = next(density_estimator.parameters()).device.type
 
         potential_fn, theta_transform = posterior_estimator_based_potential(
-            posterior_estimator=self._neural_net, prior=prior, x_o=None
+            posterior_estimator=posterior_estimator, prior=prior, x_o=None
         )
 
         if sample_with == "rejection":
@@ -419,7 +420,7 @@ def build_posterior(
                 )
             else:
                 self._posterior = DirectPosterior(
-                    posterior_estimator=self._neural_net,
+                    posterior_estimator=posterior_estimator,
                     prior=prior,
                     x_shape=self._x_shape,
                     device=device,
diff --git a/sbi/inference/snre/snre_base.py b/sbi/inference/snre/snre_base.py
--- a/sbi/inference/snre/snre_base.py
+++ b/sbi/inference/snre/snre_base.py
@@ -342,15 +342,16 @@ def build_posterior(
             prior = self._prior
 
         if density_estimator is None:
-            density_estimator = self._neural_net
+            ratio_estimator = self._neural_net
             # If internal net is used device is defined.
             device = self._device
         else:
+            ratio_estimator = density_estimator
             # Otherwise, infer it from the device of the net parameters.
             device = next(density_estimator.parameters()).device.type
 
         potential_fn, theta_transform = ratio_estimator_based_potential(
-            ratio_estimator=self._neural_net, prior=prior, x_o=None
+            ratio_estimator=ratio_estimator, prior=prior, x_o=None
         )
 
         if sample_with == "mcmc":


Test patch: