
#Residual calculator code
The above code takes advantage of identifier to distinguish configurations withĬracks and without cracks, and then weigh more for configurations with cracks.įor configurations with cracks, you may even want to weigh more for the atoms near # create a calculator loss = Loss ( calculator, nprocs = 1, residual_fn = residual_fn, residual_data = ) forces_weight # larger weight for configuration with cracks if 'with_cracks' in identifer : config_weight *= 10 normalize = data if normalize : energy_weight /= natoms forces_weight /= natoms # obtain residual and properly normalize it residual = config_weight * ( prediction - reference ) residual *= energy_weight residual *= forces_weight return residual calculator =. One can provide a residual_data instead of using the default one to tune the loss,įor example, if one wants to ignore the normalization by the number of atoms.įrom kliff.loss import Loss # define my own residual function def residual_fn ( identifier, natoms, weight, prediction, reference, data ): # extract the weight information config_weight = weight. Set contributes equally to the loss function otherwise, configurations with more atomsĬan dominate the loss, which (most of the times) is not what we prefer.

Normalization by the number of atoms makes each individual configuration in the training Instance and enables the normalization of the residual based on the number of atoms. This residual function retrieves the weights for energy and forces f``weight`` forces_weight # obtain residual and properly normalize it residual = config_weight * ( prediction - reference ) residual *= energy_weight residual *= forces_weight if data : residual /= natoms return residual That constructs the residual using energy and forces is defined as (in a nutshell):ĭef energy_forces_residual ( identifier, natoms, weight, prediction, reference, data ): # extract up the weight information config_weight = weight. For example, the energy_forces_residual() KLIFF provides a number of residual functions readily to be plugged into LossĪnd let the wheel spin. Residual_fn is also optional, and it defaults to energy_forces_residual()ĭiscussed below. Residual_data is a dictionary, with which the user can provide extra ĭata is residual_data provided at the initialization of Loss. Reference is a vector of the corresponding reference data. Prediction is a vector of the prediction computed from the Weights from the configuration (see Weight). Weight is a Weight instance that generates the Natoms is an int denoting the number of atoms in the configuration.

There, identifier is default to the path to the file that storing theĬonfiguration, e.g.

Identifier is a (unique) str associated with the configuration, which compute u based on p (prediction) and q (reference) # and it should be a vector return u Def residual_fn ( identifier, natoms, weight, prediction, reference, data ): """A function to compute the residual for a configuration.""" #.
