diff --git a/docs.it4i/software/machine-learning/introduction.md b/docs.it4i/software/machine-learning/introduction.md index 98f9bf18afe491fed36c54212648bc01a31320e4..3bb01ad057cdc4e7fc296e6a48fc083762b60b99 100644 --- a/docs.it4i/software/machine-learning/introduction.md +++ b/docs.it4i/software/machine-learning/introduction.md @@ -2,6 +2,21 @@ This section overviews machine learning frameworks and libraries available on the clusters. +## Keras + +Keras is an API designed for human beings, not machines. Keras follows best practices for reducing cognitive load: it offers consistent & simple APIs, it minimizes the number of user actions required for common use cases, and it provides clear & actionable error messages. It also has extensive documentation and developer guides. For more information, see the [official website][c]. + +For the list of available versions, type: + +```console +$ ml av Keras +``` + +## NetKet + +NetKet is an open-source project for the development of machine intelligence for many-body quantum systems. +For more information, see the [official website][d] or [GitHub][e]. + ## TensorFlow TensorFlow is an end-to-end open source platform for machine learning. It has a comprehensive, flexible ecosystem of tools, libraries, and community resources that lets researchers push the state-of-the-art in ML and developers easily build and deploy ML powered applications. For more information, see the [official website][a]. @@ -18,21 +33,13 @@ For the list of available versions, type: $ ml av Theano ``` -## Keras - -Keras is an API designed for human beings, not machines. Keras follows best practices for reducing cognitive load: it offers consistent & simple APIs, it minimizes the number of user actions required for common use cases, and it provides clear & actionable error messages. It also has extensive documentation and developer guides. For more information, see the [official website][c]. - -For the list of available versions, type: - -```console -$ ml av Keras -``` - [1]: tensorflow.md [a]: https://www.tensorflow.org/ [b]: https://github.com/Theano/ [c]: https://keras.io/ +[d]: http://www.netket.org +[e]: https://github.com/netket <!--- 2021-04-08 diff --git a/docs.it4i/software/machine-learning/netket.md b/docs.it4i/software/machine-learning/netket.md new file mode 100644 index 0000000000000000000000000000000000000000..5d4a0729898dfc235c11a368c448b225873959f4 --- /dev/null +++ b/docs.it4i/software/machine-learning/netket.md @@ -0,0 +1,129 @@ +# NetKet + +Open-source project for the development of machine intelligence for many-body quantum systems. + +## Introduction + +NetKet is a numerical framework written in Python to simulate many-body quantum systems using variational methods. In general, NetKet allows the user to parametrize quantum states using arbitrary functions, be it simple mean-field ansatz, Jastrow, MPS ansatz or convolutional neural networks. Those states can be sampled efficiently in order to estimate observables or other quantities. Stochastic optimization of the energy or a time-evolution are implemented on top of those samplers. + +NetKet tries to follow the functional programming paradigm, and is built around jax. While it is possible to run the examples without knowledge of jax, it is recommended that the users get familiar with it if they wish to extend NetKet. + +For more information, see the [NetKet documentation][1]. + +## Running NetKet + +Load the `Python/3.8.6-GCC-10.2.0-NetKet` and `intel/2020b` modules. + +### Example for Multi-GPU Node + +!!! important + Set the visible device in the environment variable before loading jax and NetKet, as NetKet loads jax. + +```code +# J1-J2 model +# Version with complex Hamiltonian +# +################################################################################ + +import os +import sys + +# detect MPI rank +from mpi4py import MPI +rank = MPI.COMM_WORLD.Get_rank() + +# set only one visible device +os.environ["CUDA_VISIBLE_DEVICES"] = f"{rank}" +# force to use gpu +os.environ["JAX_PLATFORM_NAME"] = "gpu" + +import jax +import netket as nk +import numpy as np +import json +import mpi4jax + +print("NetKet version: {}".format(nk.__version__)) +print("Jax devices: {}".format(jax.devices())) +print("Jax version: {}".format(jax.__version__)) +print("MPI utils available: {}".format(nk.utils.mpi.available)) + +# Parameters +L = 12 # length +J1 = 1.0 # nearest-neighbours exchange interaction +J2 = .50 # next-nearest-neighbours exchange interaction + +MSR = 1 # Marshall sign rule (+1/-1) + +# ## Hamiltonian + +# Hilbert space +g = nk.graph.Chain(L, pbc=True) +hilbert = nk.hilbert.Spin(s=0.5, total_sz=0.0, N=g.n_nodes) + +print("Number of graph nodes: {:d}".format(g.n_nodes)) +print("Hilbert size: {:d}".format(hilbert.size)) + +# Pauli matrices +def sigx(i): + return nk.operator.spin.sigmax(hilbert, i, dtype=np.complex128) + +def sigy(i): + return nk.operator.spin.sigmay(hilbert, i, dtype=np.complex128) + +def sigz(i): + return nk.operator.spin.sigmaz(hilbert, i, dtype=np.complex128) + +def heisenberg(i, j, sgn=1): + """Heisenberg two spin interaction including Marshall sign rule.""" + return sgn * (sigx(i)*sigx(j) + sigy(i)*sigy(j)) + sigz(i)*sigz(j) + +# setup local Hamiltonian +Ha = nk.operator.LocalOperator(hilbert, dtype=np.complex128) # Hamiltonian + +# nearest neighbours +for i in range(L - 1): + Ha += J1 * heisenberg(i, i+1, MSR) + +Ha += J1 * heisenberg(L-1, 0, MSR) + +# next nearest neighbours +for i in range(L - 2): + Ha += J2 * heisenberg(i, i+2) + +Ha += J2 * ( heisenberg(L-1, 1) + heisenberg(L-2, 0) ) + +# check Hamiltonian +print("Hamiltonian is hemitian: {}".format(Ha.is_hermitian)) +print("Number of local operators: {:d}".format(len(Ha.operators))) +print("Hamiltonian size: {}".format(Ha.to_dense().shape)) + +# ## Exact diagonalization + +ED = nk.exact.lanczos_ed(Ha, compute_eigenvectors=False) +E0 = ED[0] +print("Exact ground state energy: {:.5f}".format(E0)) + +# ## Restricted Boltzmann Machine + +# setup model +model = nk.models.RBM(alpha=1,dtype=np.complex128) + +sa = nk.sampler.MetropolisExchange(hilbert, graph=g, d_max=2, n_chains_per_rank=1) +vs = nk.vqs.MCState(sa, model, n_samples=3000) +opt = nk.optimizer.Sgd(learning_rate=0.01) +sr = nk.optimizer.SR(diag_shift=0.01) + +gs = nk.VMC(hamiltonian=Ha, optimizer=opt, variational_state=vs, preconditioner=sr) + +# run simulations +output_files = "J1J2cplx_L{:d}_J{:.2f}_rbm".format(L, J2) +gs.run(out=output_files, n_iter=3000) + +# print energy +Data = json.load(open("{:s}.log".format(output_files))) +E0rbm = np.mean(Data["Energy"]["Mean"]["real"][-500:-1]) +print("RBM ground state energy: {:.5f}".format(E0rbm)) +``` + +[1]: https://www.netket.org/docs/getting_started.html#installation-and-requirements diff --git a/mkdocs.yml b/mkdocs.yml index 417d191bd069f5866b7bb9801e4efa0e79dc9858..1e3715ffd63a023be5bfe83875e9d0d4b14d8d6a 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -171,6 +171,7 @@ nav: - Intel Trace Analyzer and Collector: software/intel/intel-suite/intel-trace-analyzer-and-collector.md - Machine Learning: - Introduction: software/machine-learning/introduction.md + - NetKet: software/machine-learning/netket.md - TensorFlow: software/machine-learning/tensorflow.md - MPI: - Introduction: software/mpi/mpi.md