\ :py:mod:`clipppy.stochastic.sampler` ====================================== .. py:module:: clipppy.stochastic.sampler Module Contents --------------- .. py:class:: AbstractSampler The base class for "samplers": objects that dynamically (or not) generate values in a `Stochastic` context. .. py:method:: __call__(self) :abstractmethod: An `AbstractSampler` is called when `Stochastic` encounters one as a "specification", and it is this method that has to be overridden in subclasses to supply the desired value. .. py:class:: ConcreteSampler A base class for "true" samplers, i.e. that provide a value from |Pyro|. Namely, `Param` and `Sampler`. .. py:attribute:: init :annotation: :torch.Tensor .. py:attribute:: support :annotation: :torch.distributions.constraints.Constraint .. py:class:: Context(context: ContextManager, func_or_val: _ps_func_t, call: _ps_call_t = PseudoSampler.call) The base class for "samplers": objects that dynamically (or not) generate values in a `Stochastic` context. .. py:method:: __call__(self, *args, **kwargs) An `AbstractSampler` is called when `Stochastic` encounters one as a "specification", and it is this method that has to be overridden in subclasses to supply the desired value. .. py:class:: Deterministic A `PseudoSampler` that records the value via `pyro.deterministic `. .. py:attribute:: _func .. py:attribute:: event_dim :annotation: :int The ``event_dim`` parameter to `pyro.deterministic `. .. py:method:: __call__(self) An `AbstractSampler` is called when `Stochastic` encounters one as a "specification", and it is this method that has to be overridden in subclasses to supply the desired value. .. py:class:: Effect(effect: Callable[[clipppy.utils._Tin], clipppy.utils._Tout], func_or_val: _ps_func_t, call: _ps_call_t = PseudoSampler.call) The base class for "samplers": objects that dynamically (or not) generate values in a `Stochastic` context. .. py:method:: __call__(self) -> clipppy.utils._Tout An `AbstractSampler` is called when `Stochastic` encounters one as a "specification", and it is this method that has to be overridden in subclasses to supply the desired value. .. py:class:: Factor A `PseudoSampler` that records the value as a `pyro.factor `. .. py:attribute:: _func .. py:class:: NamedPseudoSampler A `PseudoSampler` with a `name `. Processes the value returned by `PseudoSampler.__call__` through a function `~NamedPseudoSampler._func` that is given the `~NamedSampler.name` as first argument and the value as second. .. py:attribute:: _func :annotation: :ClassVar[Callable[[str, _ps_return_t], _ps_return_t]] A function to process the value before returning. It is called as :python:`self._func(self.name, val, **kwargs)`. Subclasses can override this to e.g. `pyro.deterministic ` and `pyro.factor `. .. py:method:: __call__(self, **kwargs) An `AbstractSampler` is called when `Stochastic` encounters one as a "specification", and it is this method that has to be overridden in subclasses to supply the desired value. .. py:class:: NamedSampler A base class for samplers with a name. These include `Sampler`, `Param`, `Deterministic`, and `Factor` that correspond to the respective `pyro.primitives`. .. py:attribute:: _subclasses :annotation: :ClassVar[set[Type[NamedSampler]]] .. py:attribute:: name :annotation: :str The name of the sampler, used e.g. in `pyro.sample ` and `pyro.param `. .. py:method:: __init_subclass__(cls, **kwargs) :classmethod: .. py:method:: set_name(self: T, name: Optional[str]) -> T Set ``self.``\ `~NamedSampler.name` and return ``self``. :param name: Set ``self.``\ `~NamedSampler.name` to this, but only if it (the passed in argument) is not `None`. :type name: str or None :returns: ``self`` :rtype: NamedSampler .. py:class:: Param Represents a `pyro.param ` statement. .. py:attribute:: event_dim :annotation: :int The ``event_dim`` argument to `pyro.param `. .. py:method:: __call__(self) Call `pyro.param ` with suitable arguments. Passes in - ``self.``\ `~NamedSampler.name`, - ``self.``\ `~ConcreteSampler.init` as ``init_tensor``, - ``self.``\ `~ConcreteSampler.support` as ``constraint``, and - ``self.``\ `~Param.event_dim`. :returns: The result of the `~pyro.primitives.param` statement. :rtype: torch.Tensor .. py:class:: PseudoSampler The base class for "samplers": objects that dynamically (or not) generate values in a `Stochastic` context. .. py:attribute:: call :annotation: :_ps_call_t .. py:attribute:: func_or_val :annotation: :_ps_func_t .. py:method:: __call__(self) -> _ps_return_t An `AbstractSampler` is called when `Stochastic` encounters one as a "specification", and it is this method that has to be overridden in subclasses to supply the desired value. .. py:method:: __repr__(self) Return repr(self). .. py:class:: Sampler(d: _Sampler_dT, name: str = _Sampler.name, init: torch.Tensor = _Sampler.init, support: torch.distributions.constraints.Constraint = _Sampler.support, expand_by: Union[torch.Size, Iterable[int]] = _Sampler.expand_by, to_event: int = _Sampler.to_event, indep: Union[torch.Size, Iterable[int]] = _Sampler.indep, mask: torch.Tensor = _Sampler.mask, **kwargs) Represents a `pyro.sample ` statement. .. py:attribute:: d :annotation: :_Sampler_dT A\ `( callable that returns a)* `_ `distribution ` to be passed to `pyro.sample `. .. py:attribute:: infer :annotation: :Mapping[str, Any] .. py:method:: __call__(self) An `AbstractSampler` is called when `Stochastic` encounters one as a "specification", and it is this method that has to be overridden in subclasses to supply the desired value. .. py:method:: distribution(self) -> pyro.distributions.torch_distribution.TorchDistributionMixin :property: The distribution from which to sample. This is the result of repeatedly calling ``self.``\ `Sampler.d` (zero or more times) until a `distribution ` pops out. .. py:method:: infer_dict(self) -> dict[str, Any] :property: A `dict` to be used as a sample site's ``config`` attribute. It is constructed from ``self.``\ `~Sampler.infer` by filtering out `Sentinel.skip` values. .. py:class:: UnbindEffect(func_or_val: _ps_func_t, call: _ps_call_t = PseudoSampler.call, dim=-1) The base class for "samplers": objects that dynamically (or not) generate values in a `Stochastic` context.