3.4. Solver

FitSNAP uses a Solver class which is a parent of all the different types of solvers, e.g. SVD and ARD for linear regression, PYTORCH and JAX for neural networks, etc.

class fitsnap3lib.solvers.solver.Solver(name, pt, config, linear=True)

This class declares the method to solve the machine learning problem, e.g. linear regression, nonlinear regression, etc.

fit

Numpy array containing coefficients of fit.

error_analysis(a=None, b=None, w=None, fs_dict=None)

If linear fit: extracts and stores fitting data, such as descriptor values, truths, and predictions, into a Pandas dataframe.

If nonlinear fit: evaluate NN on all configurations to get truth values for error calculation.

The optional arguments are for calculating errors on a given set of inputs. For linear models these inputs are A matrix, truth array, weights, and a fs dictionary which contains group info. Care must be taken to ensure that these data structures are already processed and lined up properly.

Parameters
  • a – Optional A matrix numpy array.

  • b – Optional truth matrix numpy array.

  • w – Optional weight matrix numpy array.

  • fs_dict – Optional fs dictionary from a fs.pt.fitsnap_dict

perform_fit()

Base class function for performing a fit.

prepare_data(a, b, w, fs_dict)

Prepare a, b, w data for fitting by applying weight arrays w to the a and b arrays.

Parameters
  • a (np.array) – design matrix

  • b (np.array) – truth array

  • w (np.array) – weight array

  • fs_dict (dict) – dictionary with Testing key of bools for which structures to test on.

Returns

design matrix and truth array multiplied by weights.

Return type

aw, bw (np.array)

Specific solvers are inherited from the base Solver class.

3.4.1. SVD

This class is for performing SVD fits on linear systems.

class fitsnap3lib.solvers.svd.SVD(name, pt, config)
perform_fit(a=None, b=None, w=None, fs_dict=None, trainall=False)

Perform fit on a linear system. If no args are supplied, will use fitting data in pt.shared_arrays.

Parameters
  • a (np.array) – Optional “A” matrix.

  • b (np.array) – Optional Truth array.

  • w (np.array) – Optional Weight array.

  • fs_dict (dict) – Optional dictionary containing a Testing key of which A matrix rows should not be trained.

  • trainall (bool) – Optional boolean declaring whether to train on all samples in the A matrix.

The fit is stored as a member fs.solver.fit.

3.4.2. RIDGE

This class is for performing ridge regression fits on linear systems.

class fitsnap3lib.solvers.ridge.RIDGE(name, pt, config)
perform_fit(a=None, b=None, w=None, fs_dict=None, trainall=False)

Perform fit on a linear system. If no args are supplied, will use fitting data in pt.shared_arrays.

Parameters
  • a (np.array) – Optional “A” matrix.

  • b (np.array) – Optional Truth array.

  • w (np.array) – Optional Weight array.

  • fs_dict (dict) – Optional dictionary containing a Testing key of which A matrix rows should not be trained.

  • trainall (bool) – Optional boolean declaring whether to train on all samples in the A matrix.

The fit is stored as a member fs.solver.fit.

3.4.3. PYTORCH

This class inherits from the Solver class, since it is a particular solver option.

class fitsnap3lib.solvers.pytorch.PYTORCH(name, pt, config)

A class to wrap Modules to ensure lammps mliap compatability.

Parameters

name – Name of the solver class.

optimizer

PyTorch Adam optimization object.

Type

torch.optim.Adam

model

Network model that maps descriptors to a per atom quantity (e.g. energy).

Type

torch.nn.Module

loss_function

Mean squared error loss function.

Type

torch.loss.MSELoss

learning_Rate

Learning rate for gradient descent.

Type

float

scheduler

Torch learning rate scheduler.

Type

torch.optim.lr_scheduler.ReduceLROnPlateau

device

Torch accelerator device.

training_data

Torch dataset for training.

Type

torch.utils.data.Dataset

training_loader

Data loader for loading datasets.

Type

torch.utils.data.DataLoader

create_datasets(configs=None, pt=None)

Creates the dataset to be used for training and the data loader for the batch system.

Parameters
  • configs – Optional list of Configuration objects. If not supplied, we generate a configs list using the shared arrays.

  • pt – Optional ParallelTools instance containing data we want to fit to.

evaluate_configs(config_idx=0, standardize_bool=True, dtype=torch.float64, eval_device='cpu')

Evaluates energies and forces on configs for testing purposes.

Parameters
  • config_idx (int) – Index of config to evaluate. None if evaluating all configs.

  • standardize_bool (bool) – True to standardize weights, False otherwise. Useful if comparing inputs with a previously standardized model.

  • dtype (torch.dtype) – Optional override of the global dtype.

  • eval_device (torch.device) – Optional device to evaluate on, defaults to CPU to prevent device mismatch when training on GPU.

Returns

A tuple (energy, force) for the config given by config_idx. The tuple will contain lists of energy/force for each config if config_idx is None.

perform_fit(configs: Optional[list] = None, pt=None, outfile: Optional[str] = None, verbose: bool = True)

Performs PyTorch fitting using previously calculated descriptors.

Parameters
  • configs – Optional list of Configuration objects to perform fitting on.

  • pt – ParallelTools instance containing shared arrays and data we want to fit to.

  • outfile – Optional output file to write progress to.

  • verbose – Optional flag to print progress to screen; overrides the verbose CLI.