def __init__( self, prior, classifier: Union[str, Callable] = "resnet", device: str = "cpu", logging_level: Union[int, str] = "warning", summary_writer: Optional[SummaryWriter] = None, show_progress_bars: bool = True, **unused_args ): r"""Sequential Neural Ratio Estimation. We implement two inference methods in the respective subclasses. - SNRE_A / AALR is limited to `num_atoms=2`, but allows for density evaluation when training for one round. - SNRE_B / SRE can use more than two atoms, potentially boosting performance, but allows for posterior evaluation **only up to a normalizing constant**, even when training only one round. Args: classifier: Classifier trained to approximate likelihood ratios. If it is a string, use a pre-configured network of the provided type (one of linear, mlp, resnet). Alternatively, a function that builds a custom neural network can be provided. The function will be called with the first batch of simulations (theta, x), which can thus be used for shape inference and potentially for z-scoring. It needs to return a PyTorch `nn.Module` implementing the classifier. unused_args: Absorbs additional arguments. No entries will be used. If it is not empty, we warn. In future versions, when the new interface of 0.14.0 is more mature, we will remove this argument. See docstring of `NeuralInference` class for all other arguments. """ super().__init__( prior=prior, device=device, logging_level=logging_level, summary_writer=summary_writer, show_progress_bars=show_progress_bars, **unused_args ) # As detailed in the docstring, `density_estimator` is either a string or # a callable. The function creating the neural network is attached to # `_build_neural_net`. It will be called in the first round and receive # thetas and xs as inputs, so that they can be used for shape inference and # potentially for z-scoring. check_estimator_arg(classifier) if isinstance(classifier, str): self._build_neural_net = utils.classifier_nn(model=classifier) else: self._build_neural_net = classifier # Ratio-based-specific summary_writer fields. self._summary.update({"mcmc_times": []}) # type: ignore
def __init__( self, prior: Optional[Any] = None, density_estimator: Union[str, Callable] = "maf", device: str = "cpu", logging_level: Union[int, str] = "WARNING", summary_writer: Optional[SummaryWriter] = None, show_progress_bars: bool = True, **unused_args, ): """Base class for Sequential Neural Posterior Estimation methods. Args: density_estimator: If it is a string, use a pre-configured network of the provided type (one of nsf, maf, mdn, made). Alternatively, a function that builds a custom neural network can be provided. The function will be called with the first batch of simulations (theta, x), which can thus be used for shape inference and potentially for z-scoring. It needs to return a PyTorch `nn.Module` implementing the density estimator. The density estimator needs to provide the methods `.log_prob` and `.sample()`. unused_args: Absorbs additional arguments. No entries will be used. If it is not empty, we warn. In future versions, when the new interface of 0.14.0 is more mature, we will remove this argument. See docstring of `NeuralInference` class for all other arguments. """ super().__init__( prior=prior, device=device, logging_level=logging_level, summary_writer=summary_writer, show_progress_bars=show_progress_bars, **unused_args, ) # As detailed in the docstring, `density_estimator` is either a string or # a callable. The function creating the neural network is attached to # `_build_neural_net`. It will be called in the first round and receive # thetas and xs as inputs, so that they can be used for shape inference and # potentially for z-scoring. check_estimator_arg(density_estimator) if isinstance(density_estimator, str): self._build_neural_net = utils.posterior_nn( model=density_estimator) else: self._build_neural_net = density_estimator self._proposal_roundwise = [] self.use_non_atomic_loss = False # Extra SNPE-specific fields summary_writer. self._summary.update({"rejection_sampling_acceptance_rates": []}) # type:ignore
def __init__( self, prior: Optional[Distribution] = None, density_estimator: Union[str, Callable] = "maf", device: str = "cpu", logging_level: Union[int, str] = "WARNING", summary_writer: Optional[SummaryWriter] = None, show_progress_bars: bool = True, ): r"""Base class for Sequential Neural Likelihood Estimation methods. Args: prior: A probability distribution that expresses prior knowledge about the parameters, e.g. which ranges are meaningful for them. Any object with `.log_prob()`and `.sample()` (for example, a PyTorch distribution) can be used. density_estimator: If it is a string, use a pre-configured network of the provided type (one of nsf, maf, mdn, made). Alternatively, a function that builds a custom neural network can be provided. The function will be called with the first batch of simulations (theta, x), which can thus be used for shape inference and potentially for z-scoring. It needs to return a PyTorch `nn.Module` implementing the density estimator. The density estimator needs to provide the methods `.log_prob` and `.sample()`. See docstring of `NeuralInference` class for all other arguments. """ super().__init__( prior=prior, device=device, logging_level=logging_level, summary_writer=summary_writer, show_progress_bars=show_progress_bars, ) # As detailed in the docstring, `density_estimator` is either a string or # a callable. The function creating the neural network is attached to # `_build_neural_net`. It will be called in the first round and receive # thetas and xs as inputs, so that they can be used for shape inference and # potentially for z-scoring. check_estimator_arg(density_estimator) if isinstance(density_estimator, str): self._build_neural_net = utils.likelihood_nn( model=density_estimator) else: self._build_neural_net = density_estimator # SNLE-specific summary_writer fields. self._summary.update({"mcmc_times": []}) # type: ignore
def __init__( self, simulator: Callable, prior, num_workers: int = 1, simulation_batch_size: int = 1, density_estimator: Union[str, Callable] = "maf", sample_with_mcmc: bool = False, mcmc_method: str = "slice_np", mcmc_parameters: Optional[Dict[str, Any]] = None, device: str = "cpu", logging_level: Union[int, str] = "WARNING", summary_writer: Optional[SummaryWriter] = None, show_progress_bars: bool = True, show_round_summary: bool = False, ): """Base class for Sequential Neural Posterior Estimation methods. Args: density_estimator: If it is a string, use a pre-configured network of the provided type (one of nsf, maf, mdn, made). Alternatively, a function that builds a custom neural network can be provided. The function will be called with the first batch of simulations (theta, x), which can thus be used for shape inference and potentially for z-scoring. It needs to return a PyTorch `nn.Module` implementing the density estimator. The density estimator needs to provide the methods `.log_prob` and `.sample()`. sample_with_mcmc: Whether to sample with MCMC. MCMC can be used to deal with high leakage. mcmc_method: Method used for MCMC sampling, one of `slice_np`, `slice`, `hmc`, `nuts`. Currently defaults to `slice_np` for a custom numpy implementation of slice sampling; select `hmc`, `nuts` or `slice` for Pyro-based sampling. mcmc_parameters: Dictionary overriding the default parameters for MCMC. The following parameters are supported: `thin` to set the thinning factor for the chain, `warmup_steps` to set the initial number of samples to discard, `num_chains` for the number of chains, `init_strategy` for the initialisation strategy for chains; `prior` will draw init locations from prior, whereas `sir` will use Sequential-Importance-Resampling using `init_strategy_num_candidates` to find init locations. See docstring of `NeuralInference` class for all other arguments. """ super().__init__( simulator=simulator, prior=prior, num_workers=num_workers, simulation_batch_size=simulation_batch_size, device=device, logging_level=logging_level, summary_writer=summary_writer, show_progress_bars=show_progress_bars, show_round_summary=show_round_summary, ) # As detailed in the docstring, `density_estimator` is either a string or # a callable. The function creating the neural network is attached to # `_build_neural_net`. It will be called in the first round and receive # thetas and xs as inputs, so that they can be used for shape inference and # potentially for z-scoring. check_estimator_arg(density_estimator) if isinstance(density_estimator, str): self._build_neural_net = utils.posterior_nn(model=density_estimator) else: self._build_neural_net = density_estimator self._posterior = None self._sample_with_mcmc = sample_with_mcmc self._mcmc_method = mcmc_method self._mcmc_parameters = mcmc_parameters self._model_bank = [] self.use_non_atomic_loss = False # Extra SNPE-specific fields summary_writer. self._summary.update({"rejection_sampling_acceptance_rates": []}) # type:ignore
def __init__( self, simulator: Callable, prior, num_workers: int = 1, simulation_batch_size: int = 1, classifier: Union[str, Callable] = "resnet", mcmc_method: str = "slice_np", mcmc_parameters: Optional[Dict[str, Any]] = None, device: str = "cpu", logging_level: Union[int, str] = "warning", summary_writer: Optional[SummaryWriter] = None, show_progress_bars: bool = True, show_round_summary: bool = False, ): r"""Sequential Neural Ratio Estimation. We implement two inference methods in the respective subclasses. - SNRE_A / AALR is limited to `num_atoms=2`, but allows for density evaluation when training for one round. - SNRE_B / SRE can use more than two atoms, potentially boosting performance, but allows for posterior evaluation **only up to a normalizing constant**, even when training only one round. Args: classifier: Classifier trained to approximate likelihood ratios. If it is a string, use a pre-configured network of the provided type (one of linear, mlp, resnet). Alternatively, a function that builds a custom neural network can be provided. The function will be called with the first batch of simulations (theta, x), which can thus be used for shape inference and potentially for z-scoring. It needs to return a PyTorch `nn.Module` implementing the classifier. mcmc_method: Method used for MCMC sampling, one of `slice_np`, `slice`, `hmc`, `nuts`. Currently defaults to `slice_np` for a custom numpy implementation of slice sampling; select `hmc`, `nuts` or `slice` for Pyro-based sampling. mcmc_parameters: Dictionary overriding the default parameters for MCMC. The following parameters are supported: `thin` to set the thinning factor for the chain, `warmup_steps` to set the initial number of samples to discard, `num_chains` for the number of chains, `init_strategy` for the initialisation strategy for chains; `prior` will draw init locations from prior, whereas `sir` will use Sequential-Importance- Resampling using `init_strategy_num_candidates` to find init locations. See docstring of `NeuralInference` class for all other arguments. """ super().__init__( simulator=simulator, prior=prior, num_workers=num_workers, simulation_batch_size=simulation_batch_size, device=device, logging_level=logging_level, summary_writer=summary_writer, show_progress_bars=show_progress_bars, show_round_summary=show_round_summary, ) # As detailed in the docstring, `density_estimator` is either a string or # a callable. The function creating the neural network is attached to # `_build_neural_net`. It will be called in the first round and receive # thetas and xs as inputs, so that they can be used for shape inference and # potentially for z-scoring. check_estimator_arg(classifier) if isinstance(classifier, str): self._build_neural_net = utils.classifier_nn(model=classifier) else: self._build_neural_net = classifier self._posterior = None self._sample_with_mcmc = True self._mcmc_method = mcmc_method self._mcmc_parameters = mcmc_parameters # Ratio-based-specific summary_writer fields. self._summary.update({"mcmc_times": []}) # type: ignore