elfes.modules.losses
Loss functions for Cartesian, orbital, and block-sparse quantities.
block_sparse_mae_loss
block_sparse_mae_loss(
input: BlockSparseOrbData,
target: BlockSparseOrbData,
*,
blocks: _BlockSelection = "all",
reduction: _Reduction = "mean",
) -> Tensor
Return the mean absolute error of each block-sparse orbital matrix.
The set \(\mathcal B_s\) contains all stored blocks by default. With
blocks="onsite", it contains blocks whose row and column atoms are equal
and whose cell shift is zero; blocks="offsite" selects the complement.
For complex values, the absolute value is the complex modulus. Here, \(N_c\)
is the number of additional value components. Each stored value has equal
weight within one sample, while reduction="mean" gives every sample equal
weight. For Hermitian half-storage, this loss measures the stored values;
it does not reconstruct or double-count the omitted conjugate blocks. Each
sample must contain at least one selected matrix element.
Note
This elementwise L1 error is generally not invariant under orbital-basis rotations.
Parameters:
-
(inputBlockSparseOrbData) –values_real:[sum(num_values), *extra_shape].values_imag: Optional[sum(num_values), *extra_shape].num_values:[B]. -
(targetBlockSparseOrbData) –Target data with the same stored block layout and field shapes as
input. -
(blocks_BlockSelection, default:'all') –Stored blocks included in the loss.
-
(reduction_Reduction, default:'mean') –"none"returns one value per sample;"mean"or"sum"reduces those sample losses.
block_sparse_mse_loss
block_sparse_mse_loss(
input: BlockSparseOrbData,
target: BlockSparseOrbData,
*,
blocks: _BlockSelection = "all",
reduction: _Reduction = "mean",
) -> Tensor
Return the mean squared error of each block-sparse orbital matrix.
The set \(\mathcal B_s\) contains all stored blocks by default. With
blocks="onsite", it contains blocks whose row and column atoms are equal
and whose cell shift is zero; blocks="offsite" selects the complement.
For complex values, the squared absolute value is the sum of the squared
real and imaginary errors. Here, \(N_c\) is the number of additional value
components. Each stored value has equal weight within one sample, while
reduction="mean" gives every sample equal weight. For Hermitian
half-storage, this loss measures the stored values; it does not reconstruct
or double-count the omitted conjugate blocks. No factor of \(1/2\) is
applied. Each sample must contain at least one selected matrix element.
Parameters:
-
(inputBlockSparseOrbData) –values_real:[sum(num_values), *extra_shape].values_imag: Optional[sum(num_values), *extra_shape].num_values:[B]. -
(targetBlockSparseOrbData) –Target data with the same stored block layout and field shapes as
input. -
(blocks_BlockSelection, default:'all') –Stored blocks included in the loss.
-
(reduction_Reduction, default:'mean') –"none"returns one value per sample;"mean"or"sum"reduces those sample losses.
block_sparse_orthogonalized_mse_loss
block_sparse_orthogonalized_mse_loss(
input: BlockSparseOrbData,
target: BlockSparseOrbData,
overlap: BlockSparseOrbData,
orb_counts: Tensor,
batch_ptr: Tensor,
kpoints: Tensor,
*,
reduction: _Reduction = "mean",
) -> Tensor
Return the MSE between Cholesky-orthogonalized Bloch matrices.
Here, \(\Delta X(\mathbf k)\) is the Bloch transform of input - target, and
\(S(\mathbf k)\) is the Bloch transform of overlap. Thus the transformed
difference equals the difference between input and target after both
are mapped to the same orthonormal basis. The overlap must be
positive-definite at every k-point; failed Cholesky factorization is
reported by PyTorch. Samples are grouped by their exact orbital dimension,
and the loss does not pad matrices.
Here, \(N_{\mathrm{orb}}\) is the sample's total number of orbitals,
\(N_k=|\mathcal K|\), and \(N_c\) is the number of additional value
components. Every dense matrix entry, k-point, and additional component
has equal weight within one sample; reduction="mean" gives every sample
equal weight. No factor of \(1/2\) is applied.
Parameters:
-
(inputBlockSparseOrbData) –values_real:[sum(num_values), *extra_shape].values_imag: Optional[sum(num_values), *extra_shape].num_values:[B]. -
(targetBlockSparseOrbData) –Target data with the same stored block layout and field shapes as
input. -
(overlapBlockSparseOrbData) –Hermitian positive-definite overlap matrix with the same samples and no additional value axes.
-
(orb_countsTensor) –Number of orbitals on every atom, with shape
[N_atom]. -
(batch_ptrTensor) –Sample boundaries on the atom axis, with shape
[B+1]. -
(kpointsTensor) –One shared fractional k-point with shape
[3]or multiple shared k-points with shape[N_kpoint, 3]. -
(reduction_Reduction, default:'mean') –"none"returns one value per sample;"mean"or"sum"reduces those sample losses.
atom_mae_loss
atom_mae_loss(
input: Tensor,
target: Tensor,
num_atoms: Tensor,
*,
mean_over: _MeanOver = "atoms",
reduction: _Reduction = "mean",
) -> Tensor
Return the componentwise absolute error of packed atom-level values.
The elementary errors are
With reduction="mean" and mean_over="atoms", every atom and value
component in the batch has equal weight:
With mean_over="samples", every sample instead has equal weight,
independently of its number of atoms:
Here \(C\) is the product of value_shape; \(C=1\) for one scalar per atom.
mean_over affects only the "mean" reduction. "none" returns every
elementary error and "sum" returns their unnormalized sum. This
componentwise loss is generally not invariant under rotations when the
value axes contain Cartesian vectors or tensors.
Parameters:
-
(inputTensor) –Predicted packed atom-level values with shape
[N_atom, *value_shape]. -
(targetTensor) –Target values with shape
[N_atom, *value_shape]. -
(num_atomsTensor) –Number of consecutive atoms in each sample, with shape
[B]and sumN_atom. -
(mean_over_MeanOver, default:'atoms') –"atoms"gives every atom equal weight;"samples"gives every sample equal weight. -
(reduction_Reduction, default:'mean') –"none"returns elementary errors with shape[N_atom, *value_shape];"mean"averages them according tomean_over;"sum"sums them.
Returns:
-
Tensor–Loss tensor with the shape determined by
reduction.
atom_mse_loss
atom_mse_loss(
input: Tensor,
target: Tensor,
num_atoms: Tensor,
*,
mean_over: _MeanOver = "atoms",
reduction: _Reduction = "mean",
) -> Tensor
Return the componentwise squared error of packed atom-level values.
The elementary errors are
With reduction="mean" and mean_over="atoms", every atom and value
component in the batch has equal weight:
With mean_over="samples", every sample instead has equal weight:
Here \(C\) is the product of value_shape; \(C=1\) for one scalar per atom.
mean_over affects only the "mean" reduction. "none" returns every
elementary error and "sum" returns their unnormalized sum. No factor of
\(1/2\) is applied.
Parameters:
-
(inputTensor) –Predicted packed atom-level values with shape
[N_atom, *value_shape]. -
(targetTensor) –Target values with shape
[N_atom, *value_shape]. -
(num_atomsTensor) –Number of consecutive atoms in each sample, with shape
[B]and sumN_atom. -
(mean_over_MeanOver, default:'atoms') –"atoms"gives every atom equal weight;"samples"gives every sample equal weight. -
(reduction_Reduction, default:'mean') –"none"returns elementary errors with shape[N_atom, *value_shape];"mean"averages them according tomean_over;"sum"sums them.
Returns:
-
Tensor–Loss tensor with the shape determined by
reduction.
atom_vector_norm_loss
atom_vector_norm_loss(
input: Tensor,
target: Tensor,
num_atoms: Tensor,
*,
mean_over: _MeanOver = "atoms",
reduction: _Reduction = "mean",
) -> Tensor
Return Euclidean errors of packed atom-level Cartesian vectors.
The final axis is interpreted as one Cartesian vector and is consumed by the Euclidean norm. The elementary errors are
With reduction="mean" and mean_over="atoms", every atom and extra
vector in the batch has equal weight:
With mean_over="samples", every sample instead has equal weight:
Here \(C\) is the product of extra_shape; \(C=1\) for one vector per atom.
mean_over affects only the "mean" reduction. "none" returns every
vector error and "sum" returns their unnormalized sum. Each elementary
error is invariant under simultaneous orthogonal transformations of
input and target.
Parameters:
-
(inputTensor) –Predicted packed atom-level Cartesian vectors with shape
[N_atom, *extra_shape, 3]. -
(targetTensor) –Target vectors with shape
[N_atom, *extra_shape, 3]. -
(num_atomsTensor) –Number of consecutive atoms in each sample, with shape
[B]and sumN_atom. -
(mean_over_MeanOver, default:'atoms') –"atoms"gives every atom equal weight;"samples"gives every sample equal weight. -
(reduction_Reduction, default:'mean') –"none"returns vector errors with shape[N_atom, *extra_shape];"mean"averages them according tomean_over;"sum"sums them.
Returns:
-
Tensor–Loss tensor with the shape determined by
reduction.
mae_loss
Return the componentwise absolute error of sample-level values.
With reduction="none", the elementary errors are
reduction="mean" returns
where \(C\) is the product of value_shape; \(C=1\) for one scalar per
sample. reduction="sum" returns the corresponding unnormalized sum.
Because the absolute errors are taken component by component, this loss is
generally not invariant under rotations when the value axes contain
Cartesian vectors or tensors.
Parameters:
-
(inputTensor) –Predicted sample-level values with shape
[B, *value_shape]. -
(targetTensor) –Target values with shape
[B, *value_shape]. -
(reduction_Reduction, default:'mean') –"none"returns elementary errors with shape[B, *value_shape];"mean"averages all elementary errors;"sum"sums them.
Returns:
-
Tensor–Loss tensor with the shape determined by
reduction.
mse_loss
Return the componentwise squared error of sample-level values.
With reduction="none", the elementary errors are
reduction="mean" returns
where \(C\) is the product of value_shape; \(C=1\) for one scalar per
sample. reduction="sum" returns the corresponding unnormalized sum. No
factor of \(1/2\) is applied.
Parameters:
-
(inputTensor) –Predicted sample-level values with shape
[B, *value_shape]. -
(targetTensor) –Target values with shape
[B, *value_shape]. -
(reduction_Reduction, default:'mean') –"none"returns elementary errors with shape[B, *value_shape];"mean"averages all elementary errors;"sum"sums them.
Returns:
-
Tensor–Loss tensor with the shape determined by
reduction.
per_atom_mae_loss
per_atom_mae_loss(
input: Tensor, target: Tensor, num_atoms: Tensor, *, reduction: _Reduction = "mean"
) -> Tensor
Return the per-atom absolute error of extensive sample-level values.
This loss is for an extensive quantity such as total energy: the residual
is divided by the number of atoms in its sample before the absolute error
is formed. With reduction="none",
reduction="mean" averages these errors over all samples and value
components. For one scalar per sample,
This differs from an atom-level loss: input and target have one row per
sample, not one row per atom.
Parameters:
-
(inputTensor) –Predicted extensive sample-level values with shape
[B, *value_shape]. -
(targetTensor) –Target values with shape
[B, *value_shape]. -
(num_atomsTensor) –Number of atoms in each sample, with shape
[B]. -
(reduction_Reduction, default:'mean') –"none"returns per-atom elementary errors with shape[B, *value_shape];"mean"averages them;"sum"sums them.
Returns:
-
Tensor–Loss tensor with the shape determined by
reduction.
per_atom_mse_loss
per_atom_mse_loss(
input: Tensor, target: Tensor, num_atoms: Tensor, *, reduction: _Reduction = "mean"
) -> Tensor
Return the per-atom squared error of extensive sample-level values.
This loss is for an extensive quantity such as total energy: the residual
is divided by the number of atoms in its sample before it is squared. With
reduction="none",
reduction="mean" averages these errors over all samples and value
components. For one scalar per sample,
The division therefore contributes \((N_{\mathrm{atom}}^{(b)})^{-2}\), not \((N_{\mathrm{atom}}^{(b)})^{-1}\), to the squared loss. No factor of \(1/2\) is applied.
Parameters:
-
(inputTensor) –Predicted extensive sample-level values with shape
[B, *value_shape]. -
(targetTensor) –Target values with shape
[B, *value_shape]. -
(num_atomsTensor) –Number of atoms in each sample, with shape
[B]. -
(reduction_Reduction, default:'mean') –"none"returns per-atom elementary errors with shape[B, *value_shape];"mean"averages them;"sum"sums them.
Returns:
-
Tensor–Loss tensor with the shape determined by
reduction.
vector_norm_loss
Return Euclidean errors of sample-level Cartesian vectors.
The final axis is interpreted as one Cartesian vector and is consumed by
the Euclidean norm. With reduction="none", the elementary errors are
reduction="mean" returns
where \(C\) is the product of extra_shape; \(C=1\) for one vector per
sample. reduction="sum" returns the corresponding unnormalized sum.
Unlike componentwise MAE, the elementary vector errors are invariant under
simultaneous orthogonal transformations of input and target.
Parameters:
-
(inputTensor) –Predicted sample-level Cartesian vectors with shape
[B, *extra_shape, 3]. -
(targetTensor) –Target vectors with shape
[B, *extra_shape, 3]. -
(reduction_Reduction, default:'mean') –"none"returns vector errors with shape[B, *extra_shape];"mean"averages all vector errors;"sum"sums them.
Returns:
-
Tensor–Loss tensor with the shape determined by
reduction.
orb_vector_mae_loss
Return the sample-balanced componentwise MAE of orbital vectors.
For sample \(b\),
reduction="mean" averages the B sample losses, giving every sample the
same weight independently of its number of orbital values. "sum" sums
the sample losses. This componentwise L1 error is generally not invariant
under rotations of non-scalar orbital representations.
Parameters:
-
(inputOrbData) –values_real:[sum(num_values), *extra_shape].num_values:[B]. -
(targetOrbData) –Target orbital vectors with the same field shapes as
input. -
(reduction_Reduction, default:'mean') –"none"returns sample losses with shape[B];"mean"averages them;"sum"sums them.
Returns:
-
Tensor–Loss tensor with the shape determined by
reduction.
orb_vector_mse_loss
Return the sample-balanced componentwise MSE of orbital vectors.
For sample \(b\),
reduction="mean" averages the B sample losses, giving every sample the
same weight independently of its number of orbital values. "sum" sums
the sample losses. No factor of \(1/2\) is applied.
Parameters:
-
(inputOrbData) –values_real:[sum(num_values), *extra_shape].num_values:[B]. -
(targetOrbData) –Target orbital vectors with the same field shapes as
input. -
(reduction_Reduction, default:'mean') –"none"returns sample losses with shape[B];"mean"averages them;"sum"sums them.
Returns:
-
Tensor–Loss tensor with the shape determined by
reduction.
orb_vector_quadratic_loss
orb_vector_quadratic_loss(
input: OrbData,
target: OrbData,
symm_orb_matrix: OrbData,
*,
reduction: _Reduction = "mean",
) -> Tensor
Return a symmetric quadratic orbital-vector error.
For sample \(b\),
\(\mathsf S^{(b)}\) is real and symmetric. symm_orb_matrix stores its upper
triangle: sample \(b\) contributes
\(N_{\mathrm{orb}}^{(b)}(N_{\mathrm{orb}}^{(b)}+1)/2\) values in
torch.triu_indices row-major order, and the stored off-diagonal entries
are reflected across the diagonal. The same matrix acts independently on
every trailing component, whose quadratic errors are summed. The matrix is
treated as fixed data. Positive semidefiniteness gives a nonnegative loss;
this function does not test it.
The orbital dimension is not averaged out: it is part of the quadratic
form. reduction acts only across the B samples, and no factor of \(1/2\)
is applied.
Parameters:
-
(inputOrbData) –values_real:[sum(num_values), *extra_shape].num_values:[B]. -
(targetOrbData) –Target orbital vectors with the same field shapes as
input. -
(symm_orb_matrixOrbData) –values_real:[N_matrix].num_values:[B]. -
(reduction_Reduction, default:'mean') –"none"returns sample losses with shape[B];"mean"averages them;"sum"sums them.
Returns:
-
Tensor–Loss tensor with the shape determined by
reduction.
orb_vector_triu_mse_loss
orb_vector_triu_mse_loss(
input: OrbData,
target: OrbData,
triu_orb_matrix: OrbData,
*,
reduction: _Reduction = "mean",
) -> Tensor
Return an orbital-vector squared error transformed by an upper factor.
For sample \(b\),
\(\mathsf U^{(b)}\) is a real upper-triangular factor. triu_orb_matrix
stores it in packed-upper layout: sample \(b\) contributes
\(N_{\mathrm{orb}}^{(b)}(N_{\mathrm{orb}}^{(b)}+1)/2\) values in
torch.triu_indices row-major order. When
\(\mathsf S=\mathsf U^\mathsf T\mathsf U\), this function is mathematically
equivalent to passing \(\mathsf S\) to orb_vector_quadratic_loss. The same
factor acts independently on every trailing component, whose quadratic
errors are summed. The factor is treated as fixed data.
The orbital dimension is not averaged out: it is part of the quadratic
form. reduction acts only across the B samples, and no factor of \(1/2\)
is applied.
Parameters:
-
(inputOrbData) –values_real:[sum(num_values), *extra_shape].num_values:[B]. -
(targetOrbData) –Target orbital vectors with the same field shapes as
input. -
(triu_orb_matrixOrbData) –values_real:[N_factor].num_values:[B]. -
(reduction_Reduction, default:'mean') –"none"returns sample losses with shape[B];"mean"averages them;"sum"sums them.
Returns:
-
Tensor–Loss tensor with the shape determined by
reduction.
quadrature_volumetric_mae_loss
quadrature_volumetric_mae_loss(
input: QuadratureVolumetricData,
target: QuadratureVolumetricData,
*,
reduction: _Reduction = "mean",
) -> Tensor
Return the quadrature-integrated L1 error of each sample.
For sample \(b\),
The quadrature weights are taken from input and should be nonnegative.
Additional value components are summed as part of the L1 norm.
reduction="mean" averages the B integrated sample losses; it does not
divide by the sum of weights or component count.
Parameters:
-
(inputQuadratureVolumetricData) –values_real:[sum(num_values), *extra_shape].num_values:[B].weights:[sum(num_values)]. -
(targetQuadratureVolumetricData) –Target data with the same grid layout and field shapes as
input. -
(reduction_Reduction, default:'mean') –"none"returns integrated sample losses with shape[B];"mean"averages them;"sum"sums them.
Returns:
-
Tensor–Loss tensor with the shape determined by
reduction.
quadrature_volumetric_mse_loss
quadrature_volumetric_mse_loss(
input: QuadratureVolumetricData,
target: QuadratureVolumetricData,
*,
reduction: _Reduction = "mean",
) -> Tensor
Return the quadrature-integrated squared L2 error of each sample.
For sample \(b\),
The quadrature weights are taken from input and should be nonnegative.
Additional value components are summed as part of the squared L2 norm.
reduction="mean" averages the B integrated sample losses; it does not
divide by the sum of weights or component count. No factor of \(1/2\) is
applied.
Parameters:
-
(inputQuadratureVolumetricData) –values_real:[sum(num_values), *extra_shape].num_values:[B].weights:[sum(num_values)]. -
(targetQuadratureVolumetricData) –Target data with the same grid layout and field shapes as
input. -
(reduction_Reduction, default:'mean') –"none"returns integrated sample losses with shape[B];"mean"averages them;"sum"sums them.
Returns:
-
Tensor–Loss tensor with the shape determined by
reduction.
uniform_volumetric_mae_loss
uniform_volumetric_mae_loss(
input: UniformVolumetricData,
target: UniformVolumetricData,
*,
reduction: _Reduction = "mean",
) -> Tensor
Return the integrated L1 error on each uniform grid.
For sample \(b\),
The rows of \(\mathsf H^{(b)}\) are the grid step vectors. Additional value
components are summed as part of the L1 norm. reduction="mean" averages
the B integrated sample losses; it does not divide by grid volume or
component count.
Parameters:
-
(inputUniformVolumetricData) –values_real:[sum(num_values), *extra_shape].num_values:[B].step_vectors:[B, 3, 3]. -
(targetUniformVolumetricData) –Target data with the same grid layout and field shapes as
input. -
(reduction_Reduction, default:'mean') –"none"returns integrated sample losses with shape[B];"mean"averages them;"sum"sums them.
Returns:
-
Tensor–Loss tensor with the shape determined by
reduction.
uniform_volumetric_mse_loss
uniform_volumetric_mse_loss(
input: UniformVolumetricData,
target: UniformVolumetricData,
*,
reduction: _Reduction = "mean",
) -> Tensor
Return the integrated squared L2 error on each uniform grid.
For sample \(b\),
The rows of \(\mathsf H^{(b)}\) are the grid step vectors. Additional value
components are summed as part of the squared L2 norm.
reduction="mean" averages the B integrated sample losses; it does not
divide by grid volume or component count. No factor of \(1/2\) is applied.
Parameters:
-
(inputUniformVolumetricData) –values_real:[sum(num_values), *extra_shape].num_values:[B].step_vectors:[B, 3, 3]. -
(targetUniformVolumetricData) –Target data with the same grid layout and field shapes as
input. -
(reduction_Reduction, default:'mean') –"none"returns integrated sample losses with shape[B];"mean"averages them;"sum"sums them.
Returns:
-
Tensor–Loss tensor with the shape determined by
reduction.