Trainer#

class pinnx.Trainer(problem, external_trainable_variables=None, batch_size=None)#

A Trainer trains a neural network on a Problem.

Parameters:
  • problem (Problem) – pinnx.problem.Problem instance.

  • external_trainable_variables (Union[ParamState, Sequence[ParamState]]) – A trainable brainstate.ParamState object or a list of trainable brainstate.ParamState objects. The unknown parameters in the physics systems that need to be recovered.

compile(optimizer, metrics=None, measture_train_step_compile_time=False)[source]#

Configures the trainer for training.

Parameters:
  • optimizer (Optimizer) – String name of an optimizer, or an optimizer class instance.

  • metrics (Union[str, Sequence[str]]) – List of metrics to be evaluated by the trainer during training.

predict(xs, operator=None, callbacks=None)[source]#

Generates predictions for the input samples. If operator is None, returns the network output, otherwise returns the output of the operator.

Parameters:
  • xs – The network inputs. A Numpy array or a tuple of Numpy arrays.

  • operator (Optional[Callable]) – A function takes arguments (neural_net, inputs) and outputs a tensor. inputs and outputs are the network input and output tensors, respectively. operator is typically chosen as the PDE (used to define pinnx.problem.PDE) to predict the PDE residual.

  • callbacks (Union[Callback, Sequence[Callback]]) – List of pinnx.callbacks.Callback instances. List of callbacks to apply during prediction.

restore(save_path, verbose=0)[source]#

Restore all variables from a disk file.

Parameters:
  • save_path (string) – Path where trainer was previously saved.

  • verbose (int) – Verbosity mode, 0 or 1.

save(save_path, verbose=0)[source]#

Saves all variables to a disk file.

Parameters:
  • save_path (string) – Prefix of filenames to save the trainer file.

  • verbose (int) – Verbosity mode, 0 or 1.

Returns:

Path where trainer is saved.

Return type:

string

saveplot(issave=True, isplot=True, loss_fname='loss.dat', train_fname='train.dat', test_fname='test.dat', output_dir=None)[source]#

Saves and plots the loss and metrics.

Parameters:
  • issave (bool) – If True, save the loss and metrics to files.

  • isplot (bool) – If True, plot the loss and metrics.

  • loss_fname (str) – Filename to save the loss.

  • train_fname (str) – Filename to save the training metrics.

  • test_fname (str) – Filename to save the test metrics.

  • output_dir (str) – Directory to save the files.

train(iterations, batch_size=None, display_every=1000, disregard_previous_best=False, callbacks=None, model_restore_path=None, model_save_path=None, measture_train_step_time=False)[source]#

Trains the trainer.

Parameters:
  • iterations (int) – Number of iterations to train the trainer, i.e., number of times the network weights are updated.

  • batch_size (int) –

    Integer, tuple, or None.

    • If you solve PDEs via pinnx.problem.PDE or pinnx.problem.TimePDE, do not use batch_size, and instead use pinnx.callbacks.PDEPointResampler, see an example.

    • For DeepONet in the format of Cartesian product, if batch_size is an Integer, then it is the batch size for the branch input; if you want to also use mini-batch for the trunk net input, set batch_size as a tuple, where the fist number is the batch size for the branch net input and the second number is the batch size for the trunk net input.

  • display_every (int) – Print the loss and metrics every this steps.

  • disregard_previous_best (bool) – If True, disregard the previous saved best trainer.

  • callbacks (Union[Callback, Sequence[Callback]]) – List of pinnx.callbacks.Callback instances. List of callbacks to apply during training.

  • model_restore_path (str) – Path where parameters were previously saved.

  • model_save_path (str) – Prefix of filenames created for the checkpoint.