netket.sampler.Sampler¶
-
class
netket.sampler.
Sampler
(hilbert, n_chains=16, machine_pow=2, dtype=<class 'numpy.float64'>)[source]¶ Bases:
abc.ABC
Abstract base class for all samplers.
It contains the fields that all of them should posses, defining the common API. Note that fields marked with pytree_node=False are treated as static arguments when jitting.
-
__init__
(hilbert, n_chains=16, machine_pow=2, dtype=<class 'numpy.float64'>)¶ Initialize self. See help(type(self)) for accurate signature.
- Attributes
- Methods
-
init_state
(machine, parameters, seed=None)[source]¶ Creates the structure holding the state of the sampler.
If you want reproducible samples, you should specify seed, otherwise the state will be initialised randomly.
If running across several MPI processes, all sampler_states are guaranteed to be in a different (but deterministic) state. This is achieved by first reducing (summing) the seed provided to every MPI rank, then generating n_rank seeds starting from the reduced one, and every rank is initialized with one of those seeds.
The resulting state is guaranteed to be a frozen python dataclass (in particular, a flax’s dataclass), and it can be serialized using Flax serialization methods.
- Parameters
- Return type
SamplerState
- Returns
The structure holding the state of the sampler. In general you should not expect it to be in a valid state, and should reset it before use.
-
log_pdf
(model)[source]¶ Returns a closure with the log_pdf function encoded by this sampler.
Note: the result is returned as an HashablePartial so that the closure does not trigger recompilation.
-
replace
(**updates)¶ “Returns a new object replacing the specified fields with new values.
-
reset
(machine, parameters, state=None)[source]¶ Resets the state of the sampler. To be used every time the parameters are changed.
- Parameters
machine (
Union
[Callable
,Module
]) – a Flax module or callable with the forward pass of the log-pdf.parameters (
Any
) – The PyTree of parameters of the model.state (
Optional
[SamplerState
]) – The current state of the sampler. If it’s not provided, it will be constructed by callingsampler.init_state(machine, parameters)
with a random seed.
- Return type
SamplerState
- Returns
A valid sampler state.
-
sample
(machine, parameters, *, state=None, chain_length=1)[source]¶ Samples chain_length elements along the chains.
- Parameters
sampler – The Monte Carlo sampler.
machine (
Union
[Callable
,Module
]) – The model or callable to sample from (if it’s a function it should have the signaturef(parameters, σ) -> jnp.ndarray
).parameters (
Any
) – The PyTree of parameters of the model.state (
Optional
[SamplerState
]) – current state of the sampler. If None, then initialises it.chain_length (
int
) – (default=1), the length of the chains.
- Returns
The new state of the sampler σ: The next batch of samples.
- Return type
state
-
sample_next
(machine, parameters, state=None)[source]¶ Samples the next state in the markov chain.
- Parameters
machine (
Union
[Callable
,Module
]) – a Flax module or callable apply function with the forward pass of the log-pdf.parameters (
Any
) – The PyTree of parameters of the model.state (
Optional
[SamplerState
]) – The current state of the sampler. If it’s not provided, it will be constructed by callingsampler.reset(machine, parameters)
with a random seed.
- Returns
The new state of the sampler σ: The next batch of samples.
- Return type
state
-
-