netket.experimental.vqs.variables_from_file

netket.experimental.vqs.variables_from_file(filename, variables)

Loads the variables of a variational state from a .mpack file.

Parameters
  • filename (str) – the file containing the variables. Assumes a .mpack extension and adds it if missing and no file exists.

  • variables (Any) – An object variables with the same structure and shape of the object to be deserialized.

Returns

a PyTree like variables

Examples

Serializing the data:

>>> import netket as nk
>>> import flax
>>> # construct an RBM model on 10 spins
>>> vstate = nk.variational.MCState(
...      nk.sampler.MetropolisLocal(nk.hilbert.Spin(0.5)**10),
...      nk.models.RBM())
>>> with open("test.mpack", 'wb') as file:
...     bytes_written = file.write(flax.serialization.to_bytes(vstate.variables))
>>> print(bytes_written)
1052
>>>
>>> # Deserialize the data
>>>
>>> del vstate
>>> # construct an RBM model on 10 spins
>>> vstate2 = nk.variational.MCState(
...      nk.sampler.MetropolisLocal(nk.hilbert.Spin(0.5)**10),
...      nk.models.RBM())
>>> # Load the data by passing the model
>>> vars = nk.variational.experimental.variables_from_file("test.mpack",
...                                                        vstate2.variables)
>>> # update the variables of vstate with the loaded data.
>>> vstate2.variables = vars