loss
#
Functionality for computing the loss.
Classes:
-
LossRecord–Container for different loss components
Functions:
-
compute_overall_loss_and_grad–Compute loss and optionally gradients for memory efficiency.
-
compute_regularisation_loss–Compute regularisation penalty
-
get_loss_closure_fn–Return a closure function for use with Levenberg-Marquardt optimizer.
-
predict_with_weights–Predict the relative energies and forces with associated weights from the dataset.
LossRecord
#
Bases: NamedTuple
Container for different loss components
_compute_molecule_energy_force_loss
#
_compute_molecule_energy_force_loss(
energy_ref: Tensor,
energy_pred: Tensor,
forces_ref: Tensor,
forces_pred: Tensor,
energy_weights: Tensor,
forces_weights: Tensor,
n_atoms: int,
device: device | str,
) -> tuple[Tensor, Tensor]
Compute weighted energy and force loss for a single molecule.
Args: energy_ref: Reference energies with shape (n_confs,). energy_pred: Predicted energies with shape (n_confs,). forces_ref: Reference forces with shape (n_confs * n_atoms, 3). forces_pred: Predicted forces with shape (n_confs * n_atoms, 3). energy_weights: Per-conformation energy weights with shape (n_confs,). forces_weights: Per-conformation force weights with shape (n_confs,). n_atoms: Number of atoms in the molecule. device: Device for tensor creation.
Returns: Tuple of (energy_loss, force_loss) for this molecule.
Source code in presto/loss.py
_compute_molecule_total_loss_and_grad
#
_compute_molecule_total_loss_and_grad(
force_field: TensorForceField,
dataset: Dataset,
topology: TensorTopology,
trainable_parameters: Tensor,
device_type: str,
compute_grad: bool = True,
) -> tuple[Tensor, Tensor, Tensor | None]
Compute total loss and optionally gradient for a single molecule.
This function computes the loss for one molecule and optionally computes its gradient with respect to trainable parameters. The loss is detached after gradient computation to free memory.
Args: force_field: The force field to use for predictions. dataset: Dataset for this molecule. topology: Topology for this molecule. trainable_parameters: Parameters to compute gradients for. device_type: Device type for computations. compute_grad: Whether to compute gradients (default: True).
Returns: Tuple of (energy_loss_detached, force_loss_detached, gradient_or_none)
Source code in presto/loss.py
compute_overall_loss_and_grad
#
compute_overall_loss_and_grad(
datasets_list: list[Dataset],
trainable: Trainable,
trainable_parameters: Tensor,
initial_parameters: Tensor,
topologies: list[TensorTopology],
regularisation_target: Literal["initial", "zero"],
device_type: str,
compute_grad: bool = True,
) -> tuple[LossRecord, Tensor | None]
Compute loss and optionally gradients for memory efficiency.
This function computes gradients for each molecule separately using torch.autograd.grad, then accumulates them. This is more memory-efficient than computing the full loss and calling .backward(), especially when training with multiple molecules.
Args: datasets_list: List of datasets to predict the energies and forces of. trainable: The trainable object containing the force field. trainable_parameters: The parameters to be optimized. initial_parameters: The initial parameters before training. topologies: List of topologies of the molecules in the datasets. regularisation_target: The type of regularisation to apply ('initial' or 'zero'). device_type: The device type (e.g., 'cpu' or 'cuda'). compute_grad: Whether to compute gradients (default: True).
Returns: Tuple of (LossRecord, accumulated_gradient or None)
Source code in presto/loss.py
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 | |
compute_regularisation_loss
#
compute_regularisation_loss(
trainable: Trainable,
trainable_parameters: Tensor,
initial_parameters: Tensor,
regularisation_target: Literal["initial", "zero"],
) -> Tensor
Compute regularisation penalty
Source code in presto/loss.py
get_loss_closure_fn
#
get_loss_closure_fn(
datasets_list: list[Dataset],
trainable: Trainable,
trainable_parameters: Tensor,
initial_parameters: Tensor,
topologies: list[TensorTopology],
regularisation_target: Literal["initial", "zero"],
) -> ClosureFn
Return a closure function for use with Levenberg-Marquardt optimizer.
This closure uses memory-efficient gradient computation where gradients are computed per-molecule and accumulated manually, consistent with the Adam optimizer implementation.
Args: datasets_list: List of datasets to predict energies and forces of. trainable: The trainable object containing the force field. trainable_parameters: The parameters to be optimized. initial_parameters: The initial parameters before training. topologies: List of topologies of the molecules in the datasets. regularisation_target: Type of regularisation ('initial' or 'zero').
Returns: A closure function that takes a tensor and returns the loss, gradient (if requested), and Hessian (if requested).
Source code in presto/loss.py
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 | |
predict_with_weights
#
predict_with_weights(
dataset: Dataset,
force_field: TensorForceField,
topologies: dict[str, TensorTopology],
reference: Literal["mean", "min", "median"] = "mean",
normalize: bool = True,
device_type: str = "cpu",
create_graph: bool = False,
) -> tuple[Tensor, Tensor, Tensor, Tensor, Tensor, Tensor]
Predict the relative energies and forces with associated weights from the dataset.
This function is similar to predict but also returns the energy and force weights
from the dataset entries.
Args: dataset: The dataset to predict the energies and forces of. force_field: The force field to use to predict the energies and forces. topologies: The topologies of the molecules in the dataset. reference: The reference energy to compute the relative energies with respect to. normalize: Whether to scale the relative energies and forces. device_type: The device type (e.g., 'cpu' or 'cuda'). create_graph: Whether to create a computation graph for gradients.
Returns: Tuple of (energy_ref, energy_pred, forces_ref, forces_pred, energy_weights, forces_weights)
Source code in presto/loss.py
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 | |