netket.experimental.sampler.MetropolisSamplerPmap#
- class netket.experimental.sampler.MetropolisSamplerPmap#
Bases:
MetropolisSampler
Metropolis-Hastings sampler for an Hilbert space according to a specific transition rule where chains are split among the available devices (jax.devices()).
This sampler is experimental. Itβs API might change without warnings in future NetKet releases.
To parallelize on CPU, you should set the following environment variable before loading jax/NetKet, XLA_FLAGS=ββxla_force_host_platform_device_count=XXβ, where XX is the number of desired cpu devices.
The transition rule is used to generate a proposed state \(s^\prime\), starting from the current state \(s\). The move is accepted with probability
\[A(s \rightarrow s^\prime) = \mathrm{min} \left( 1,\frac{P(s^\prime)}{P(s)} e^{L(s,s^\prime)} \right) ,\]where the probability being sampled from is \(P(s)=|M(s)|^p\). Here \(M(s)\) is a user-provided function (the machine), \(p\) is also user-provided with default value \(p=2\), and \(L(s,s^\prime)\) is a suitable correcting factor computed by the transition kernel.
The dtype of the sampled states can be chosen.
- Inheritance
- __init__(*args, __precompute_cached_properties=False, __skip_preprocess=False, **kwargs)#
Constructs a Metropolis Sampler.
- Parameters:
hilbert β The hilbert space to sample
rule β A MetropolisRule to generate random transitions from a given state as well as uniform random states.
n_sweeps β The number of exchanges that compose a single sweep. If None, sweep_size is equal to the number of degrees of freedom being sampled (the size of the input vector s to the machine).
reset_chains β If False the state configuration is not reset when reset() is called.
n_chains β The total number of Markov Chain to be run in parallel on a the available devices. This will be rounded to the nearest multiple of len(jax.devices())
n_chains_per_device β The number of chains to be run in parallel on one device. Cannot be specified if n_chains is also specified.
machine_pow β The power to which the machine should be exponentiated to generate the pdf (default = 2).
dtype β The dtype of the states sampled (default = np.float32).
- Attributes
- is_exact#
Returns True if the sampler is exact.
The sampler is exact if all the samples are exactly distributed according to the chosen power of the variational state, and there is no correlation among them.
- n_batches#
The batch size of the configuration $sigma$ used by this sampler.
In general, it is equivalent to
n_chains_per_rank
.
- n_chains_per_device#
- n_chains_per_rank#
The total number of independent chains per MPI rank.
If you are not using MPI, this is equal to
n_chains
.
-
rule:
MetropolisRule
= None#
-
hilbert:
AbstractHilbert
#
- Methods
- init_state(machine, parameters, seed=None)#
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_state`s 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 dataclass), and it can be serialized using Flax serialization methods.
- Parameters:
machine (
Union
[Callable
,Module
]) β A Flax module or callable with the forward pass of the log-pdf. If it is a callable, it should have the signaturef(parameters, Ο) -> jnp.ndarray
.parameters (
Any
) β The PyTree of parameters of the model.seed (
Union
[int
,Any
,None
]) β An optional seed or jax PRNGKey. If not specified, a random seed will be used.
- Return type:
- 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)#
Returns a closure with the log-pdf function encoded by this sampler.
- Parameters:
model (
Union
[Callable
,Module
]) β A Flax module or callable with the forward pass of the log-pdf. If it is a callable, it should have the signaturef(parameters, Ο) -> jnp.ndarray
.- Return type:
- Returns:
The log-probability density function.
Note
The result is returned as a 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)#
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. If it is a callable, it should have the signaturef(parameters, Ο) -> jnp.ndarray
.parameters (
Any
) β The PyTree of parameters of the model.state (
Optional
[SamplerState
]) β The current state of the sampler. If not specified, it will be constructed by callingsampler.init_state(machine, parameters)
with a random seed.
- Return type:
- Returns:
A valid sampler state.
- sample(machine, parameters, *, state=None, chain_length=1)#
Samples chain_length batches of samples along the chains.
- Parameters:
machine (
Union
[Callable
,Module
]) β A Flax module or callable with the forward pass of the log-pdf. If it is a callable, it should have the signaturef(parameters, Ο) -> jnp.ndarray
.parameters (
Any
) β The PyTree of parameters of the model.state (
Optional
[SamplerState
]) β The current state of the sampler. If not specified, then initialize and reset it.chain_length (
int
) β The length of the chains (default = 1).
- Returns:
The generated batches of samples. state: The new state of the sampler.
- Return type:
Ο
- sample_next(machine, parameters, state=None)#
Samples the next state in the Markov chain.
- Parameters:
machine (
Union
[Callable
,Module
]) β A Flax module or callable with the forward pass of the log-pdf. If it is a callable, it should have the signaturef(parameters, Ο) -> jnp.ndarray
.parameters (
Any
) β The PyTree of parameters of the model.state (
Optional
[SamplerState
]) β The current state of the sampler. If not specified, then initialize and reset it.
- Returns:
The new state of the sampler. Ο: The next batch of samples.
- Return type:
state
Note
The return order is inverted wrt sample because when called inside of a scan function the first returned argument should be the state.
- samples(machine, parameters, *, state=None, chain_length=1)#
Returns a generator sampling chain_length batches of samples along the chains.
- Parameters:
machine (
Union
[Callable
,Module
]) β A Flax module or callable with the forward pass of the log-pdf. If it is a callable, it should have the signaturef(parameters, Ο) -> jnp.ndarray
.parameters (
Any
) β The PyTree of parameters of the model.state (
Optional
[SamplerState
]) β The current state of the sampler. If not specified, then initialize and reset it.chain_length (
int
) β The length of the chains (default = 1).
- Return type: