netket.logging.TensorBoardLog#
- class netket.logging.TensorBoardLog[source]#
Bases:
AbstractCallbackCreates a tensorboard logger using tensorboardX’s summarywriter.
Refer to its documentation for further details
https://tensorboardx.readthedocs.io/en/latest/tensorboard.html
TensorBoardX must be installed.
This class is a full
AbstractCallbackand can be passed either asout=loggeror inside thecallbacks=[..., logger]list. When used as a callback the logger automatically captures the variational state snapshot taken just before the parameter update.- Parameters:
logdir (
string) – Save directory location. Default is runs/CURRENT_DATETIME_HOSTNAME, which changes after each run. Use hierarchical folder structure to compare between runs easily. e.g. pass in ‘runs/exp1’, ‘runs/exp2’, etc. for each new experiment to compare across them.comment (
string) – Comment logdir suffix appended to the defaultlogdir. Iflogdiris assigned, this argument has no effect.purge_step (
int) – When logging crashes at step \(T+X\) and restarts at step \(T\), any events whose global_step larger or equal to \(T\) will be purged and hidden from TensorBoard. Note that crashed and resumed experiments should have the samelogdir.max_queue (
int) – Size of the queue for pending events and summaries before one of the ‘add’ calls forces a flush to disk. Default is ten items.flush_secs (
int) – How often, in seconds, to flush the pending events and summaries to disk. Default is every two minutes.filename_suffix (
string) – Suffix added to all event filenames in the logdir directory. More details on filename construction in tensorboard.summary.writer.event_file_writer.EventFileWriter.write_to_disk (
boolean) – If pass False, TensorBoardLog will not write to disk.metadata (
dict|None) – Optional flat dict of key/value pairs written as a JSON text summary at the start of the run (visible in the TensorBoard Text tab).
Tip
Use
metadatato attach a flat dict of hyper-parameters (learning rate, system size, model type, …) to the run. They are written once as a JSON text entry and appear in the TensorBoard Text tab, keeping all run information in one place without external bookkeeping.Examples
Logging optimisation to tensorboard.
>>> import pytest; pytest.skip("skip automated test of this docstring") >>> >>> import netket as nk >>> # create a summary writer with automatically generated folder name. >>> writer = nk.logging.TensorBoardLog() >>> # folder location: runs/May04_22-14-54_s-MacBook-Pro.local/ >>> # create a summary writer using the specified folder name. >>> writer = nk.logging.TensorBoardLog("my_experiment") >>> # folder location: my_experiment >>> # create a summary writer with comment appended. >>> writer = nk.logging.TensorBoardLog(comment="LR_0.1_BATCH_16") >>> # folder location: runs/May04_22-14-54_s-MacBook-Pro.localLR_0.1_BATCH_16/
Attaching metadata to record hyper-parameters.
>>> import pytest; pytest.skip("skip automated test of this docstring") >>> >>> import netket as nk >>> writer = nk.logging.TensorBoardLog( ... "my_experiment", ... metadata={"learning_rate": 0.01, "alpha": 1, "L": 20}, ... ) >>> gs.run(n_iter=300, out=writer) >>> # hyper-parameters appear in the TensorBoard Text tab as JSON
Using the logger as a callback.
>>> import pytest; pytest.skip("skip automated test of this docstring") >>> >>> import netket as nk >>> writer = nk.logging.TensorBoardLog("my_experiment") >>> gs.run(n_iter=300, callbacks=[writer])
- Inheritance

- Attributes
- callback_order#
- Methods
- __call__(step, item, variational_state=None)[source]#
Call self as a function.
- Parameters:
step (int)
variational_state (VariationalState | None)
- before_parameter_update(step, log_data, driver)[source]#
Called after all update logic has been computed and the step has been accepted, but before the driver applies the parameter update.
At this point:
The loss and its gradient have been computed by
compute_loss_and_update().The step has been accepted (not rejected by
on_compute_update_end()).driver.step_countstill refers to the current step — it has not yet been incremented.The variational state parameters have not yet changed.
This is the right place to estimate additional observables, add data to
log_data, or take a snapshot of the state for logging. Callbacks with a lowercallback_orderrun first, so observables callbacks (order 0) are guaranteed to populatelog_databefore logger callbacks (order 10) read it.
- flush(variational_state=None)[source]#
Writes to file the content of this logger.
- Parameters:
machine – optionally also writes the parameters of the machine.
- on_compute_update_end(step, log_data, driver)[source]#
Callback called at the end of the compute update phase, after computing the loss and its gradient.
This is called before the parameters are updated, so it can be used to implement custom logic for rejecting a step based on the computed loss or gradient.
- Return type:
- Returns:
A boolean indicating whether to reject the step (i.e. repeat it with the same parameters). If it returns None, it is treated as False.