kliff.legacy.calculators.calculator_torch

class kliff.legacy.calculators.calculator_torch.CalculatorTorch(model, gpu=None)[source]

A calculator for torch based models.

Parameters:
  • model (ModelTorch) – torch models, e.g. NeuralNetwork.

  • gpu (Union[bool, int, None]) – whether to use gpu for training. If int (e.g. 0), will trained on this gpu device. If True will always train on gpu 0.

implemented_property = ['energy', 'forces', 'stress']
create(configs, use_energy=True, use_forces=True, use_stress=False, fingerprints_filename='fingerprints.pkl', fingerprints_mean_stdev_filename=None, reuse=False, use_welford_method=False, nprocs=1)[source]

Process configs to generate fingerprints.

Parameters:
  • configs (List[Configuration]) – atomic configurations

  • use_energy (bool) – Whether to require the calculator to compute energy.

  • use_forces (bool) – Whether to require the calculator to compute forces.

  • use_stress (bool) – Whether to require the calculator to compute stress.

  • fingerprints_filename (Union[Path, str]) – Path to save the generated fingerprints. If reuse=True, Will not generate the fingerprints, but directly use the one provided via this file.

  • fingerprints_mean_stdev_filename (Union[str, Path, None]) – Path to save the mean and standard deviation of the fingerprints. If reuse=True, Will not generate new fingerprints mean and stdev, but directly use the one provided via this file. If normalize is not required by a descriptor, this is ignored.

  • reuse (bool) – Whether to reuse provided fingerprints.

  • use_welford_method (bool) – Whether to compute mean and standard deviation using the Welford method, which is memory efficient. See https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance

  • nprocs (int) – Number of processes used to generate the fingerprints. If 1, run in serial mode, otherwise nprocs processes will be forked via multiprocessing to do the work.

get_fingerprints()[source]

Return a list of fingerprints of the configurations.

Return type:

List[dict]

get_compute_arguments(batch_size=1)[source]

Return the dataloader with batch size set to batch_size.

set_fingerprints(fingerprints)[source]

Update the fingerprints of the calculator. The fingerprints input argument should be in the same format as the output of meth:~kliff.legacy.descriptors.descriptor.load_fingerprints, which is a list of dictionaries.

Parameters:

fingerprints (List[dict]) – A list of fingerprints.

fit()[source]
compute(batch)[source]
property model

Get the underlying torch model

save_model(epoch, force_save=False)[source]

Save the model to disk.

When to save a model is dependent on epoch and a model’s metadata for save.

Parameters:
  • epoch (int) – current optimization epoch.

  • force_save (bool) – save the model, ignoring epoch and save metadata.

get_energy(batch)[source]
get_forces(batch)[source]
get_stress(batch)[source]
get_size_opt_params()[source]

Return the size of the parameters.

Returns:

Each element in the list gives the shape of each type of parameter

tensors, containing, e.g., weights and biases, for each layer.

nelements: Number of elements of each parameter tensor. nparams: Total number of parameters

Return type:

sizes

get_num_opt_params()[source]

Return the total number of parameters.

Return type:

int

get_opt_params(flat=True)[source]

Retrieve the parameters, i.e., weights and biases.

Parameters:

flat (bool) – A flag to return a flat, 1D array.

Return type:

Union[List, array]

Returns:

Parameters, i.e., weights and biases. If flat=True, a 1D np.ndarray will be returned. Otherwise, nested lists will be returned, where each list contain the weights and biases for each layer.

update_model_params(parameters)[source]

Update the model parameters from a 1D array.

Parameters:

parameters (array) – New parameter values to set. It needs to be a 1D array.

class kliff.legacy.calculators.calculator_torch.CalculatorTorchSeparateSpecies(models, gpu=None)[source]

A calculator supporting models of difference species.

Parameters:
  • models (Dict[str, NeuralNetwork]) – {species: model} with species specifying the chemical symbol for the model.

  • gpu (Union[bool, int, None]) – whether to use gpu for training. If int (e.g. 0), will trained on this gpu device. If True will always train on gpu 0.

compute(batch)[source]
property model

Get the underlying torch model

save_model(epoch, force_save=False)[source]

Save the models to disk.

When to save a model is dependent on epoch and a model’s metadata for save.

Parameters:
  • epoch (int) – current optimization epoch.

  • force_save (bool) – save the model, ignoring epoch and save metadata.

exception kliff.legacy.calculators.calculator_torch.CalculatorTorchError(msg)[source]