Skip to content

Implement `__repr__` for various NetKet classes

Vicentini Filippo requested to merge github/fork/femtobit/repr into v3.0

Created by: femtobit

This PR implements __repr__ for graphs, hilbert spaces, and machines, which makes it more comfortable to work with NetKet objects interactively.

For example:

before:

>>> g = nk.graph.Hypercube(8, 2)
>>> g
<netket.graph.grid.Grid at 0x7f143c7eb070>

after:

>>> g = nk.graph.Hypercube(8, 2)
>>> g
Grid(length=[8, 8], pbc=True)

Similarly for Hilbert spaces

>>> hi = nk.hilbert.Spin(g, 1/2)
>>> hi
Spin(s=1/2; N=64)

or machines:

>>> tnn = torch.nn.Sequential(torch.nn.Linear(64, 128), torch.nn.Tanh(), torch.nn.Linear(128, 2))
>>> nk.machine.Torch(tnn, hi)
Torch(input_size=64, n_par=8578, complex_parameters=False)
  Sequential(
    (0): Linear(in_features=64, out_features=128, bias=True)
    (1): Tanh()
    (2): Linear(in_features=128, out_features=2, bias=True)
  )

This also vastly improves the utility of printing vmc.info:

>>> print(vmc.info())                                                      
Vmc(step_count=0, n_samples=1008, n_discard=100)
   Hamiltonian : <netket.operator._hamiltonian.Ising object at 0x7fdc983d2460>
   Machine     : RbmSpin(input_size=64, n_par=131, complex_parameters=True)
   Optimizer   : Sgd(learning_rate=0.01, l2reg=0, decay_factor=1.0)
   SR solver   : SR(solver=iterative:CG, has_complex_parameters=True)

(As you can see, some things such as operators are still missing, but those can easily be added in a later PR.)

Let me know what you think.

Merge request reports