def run( self, T, out=None, obs=None, *, tstops=None, show_progress=True, callback=None, ): """ Runs the time evolution. By default uses :ref:`netket.logging.JsonLog`. To know about the output format check it's documentation. The logger object is also returned at the end of this function so that you can inspect the results without reading the json output. Args: T: The integration time period. out: A logger object, or an iterable of loggers, to be used to store simulation log and data. If this argument is a string, it will be used as output prefix for the standard JSON logger. obs: An iterable containing the observables that should be computed. tstops: A sequence of stopping times, each within the intervall :code:`[self.t0, self.t0 + T]`, at which the driver will stop and perform estimation of observables, logging, and excecute the callback function. By default, a stop is performed after each time step (at potentially varying step size if an adaptive integrator is used). show_progress: If true displays a progress bar (default=True) callback: Callable or list of callable callback functions to be executed at each stoping time. """ if obs is None: obs = {} if callback is None: callback = lambda *_args, **_kwargs: True # Log only non-root nodes if self._mynode == 0: if out is None: loggers = () # if out is a path, create an overwriting Json Log for output elif isinstance(out, str): loggers = (JsonLog(out, "w"), ) else: loggers = _to_iterable(out) else: loggers = tuple() show_progress = False callbacks = _to_iterable(callback) callback_stop = False with tqdm(total=self.t + T, disable=not show_progress) as pbar: old_step = self.step_value first_step = True for step in self.iter(T, tstops=tstops): log_data = self.estimate(obs) # if the cost-function is defined then report it in the progress bar if self._loss_stats is not None: pbar.set_postfix_str(self._loss_name + "=" + str(self._loss_stats)) log_data[self._loss_name] = self._loss_stats # Execute callbacks before loggers because they can append to log_data for callback in callbacks: if not callback(step, log_data, self): callback_stop = True for logger in loggers: logger(self.step_value, log_data, self.state) if len(callbacks) > 0: if mpi.mpi_any(callback_stop): break # Reset the timing of tqdm after the first step, to ignore compilation time if first_step: first_step = False pbar.unpause() # Update the progress bar pbar.update(np.asarray(self.step_value - old_step)) old_step = self.step_value # Final update so that it shows up filled. pbar.update(np.asarray(self.step_value - old_step)) # flush at the end of the evolution so that final values are saved to # file for logger in loggers: logger.flush(self.state) return loggers
def run( self, n_iter, out=None, obs=None, show_progress=True, save_params_every=50, # for default logger write_every=50, # for default logger step_size=1, # for default logger callback=lambda *x: True, ): """ Executes the Monte Carlo Variational optimization, updating the weights of the network stored in this driver for `n_iter` steps and dumping values of the observables `obs` in the output `logger`. If no logger is specified, creates a json file at `out`, overwriting files with the same prefix. By default uses :ref:`netket.logging.JsonLog`. To know about the output format check it's documentation. The logger object is also returned at the end of this function so that you can inspect the results without reading the json output. Args: n_iter: the total number of iterations out: A logger object, or an iterable of loggers, to be used to store simulation log and data. If this argument is a string, it will be used as output prefix for the standard JSON logger. obs: An iterable containing all observables that should be computed save_params_every: Every how many steps the parameters of the network should be serialized to disk (ignored if logger is provided) write_every: Every how many steps the json data should be flushed to disk (ignored if logger is provided) step_size: Every how many steps should observables be logged to disk (default=1) show_progress: If true displays a progress bar (default=True) callback: Callable or list of callable callback functions to stop training given a condition """ if not isinstance(n_iter, numbers.Number): raise ValueError( "n_iter, the first positional argument to `run`, must be a number!" ) if obs is None: obs = {} if out is None: out = tuple() print( "No output specified (out=[apath|nk.logging.JsonLogger(...)])." "Running the optimization but not saving the output.") # Log only non-root nodes if self._mynode == 0: # if out is a path, create an overwriting Json Log for output if isinstance(out, str): loggers = (JsonLog(out, "w", save_params_every, write_every), ) else: loggers = _to_iterable(out) else: loggers = tuple() show_progress = False callbacks = _to_iterable(callback) callback_stop = False with tqdm(total=n_iter, disable=not show_progress) as pbar: old_step = self.step_count first_step = True for step in self.iter(n_iter, step_size): log_data = self.estimate(obs) self._log_additional_data(log_data, step) # if the cost-function is defined then report it in the progress bar if self._loss_stats is not None: pbar.set_postfix_str(self._loss_name + "=" + str(self._loss_stats)) log_data[self._loss_name] = self._loss_stats # Execute callbacks before loggers because they can append to log_data for callback in callbacks: if not callback(step, log_data, self): callback_stop = True for logger in loggers: logger(self.step_count, log_data, self.state) if len(callbacks) > 0: if mpi.mpi_any(callback_stop): break # Reset the timing of tqdm after the first step, to ignore compilation time if first_step: first_step = False pbar.unpause() # Update the progress bar pbar.update(self.step_count - old_step) old_step = self.step_count # Final update so that it shows up filled. pbar.update(self.step_count - old_step) # flush at the end of the evolution so that final values are saved to # file for logger in loggers: logger.flush(self.state) return loggers