Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion deepmd/pt/loss/loss.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@
from deepmd.utils.data import (
DataRequirementItem,
)
from deepmd.utils.plugin import (
make_plugin_registry,
)


class TaskLoss(torch.nn.Module, ABC):
class TaskLoss(torch.nn.Module, ABC, make_plugin_registry("loss")):
def __init__(self, **kwargs):
"""Construct loss."""
super().__init__()
Expand All @@ -38,3 +41,23 @@ def display_if_exist(loss: torch.Tensor, find_property: float) -> torch.Tensor:
whether the property is found
"""
return loss if bool(find_property) else torch.nan

@classmethod
def get_loss(cls, loss_params: dict) -> "TaskLoss":
"""Get the loss module by the parameters.

By default, all the parameters are directly passed to the constructor.
If not, override this method.

Parameters
----------
loss_params : dict
The loss parameters

Returns
-------
TaskLoss
The loss module
"""
loss = cls(**loss_params)
return loss
4 changes: 3 additions & 1 deletion deepmd/pt/train/training.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
EnergySpinLoss,
EnergyStdLoss,
PropertyLoss,
TaskLoss,
TensorLoss,
)
from deepmd.pt.model.model import (
Expand Down Expand Up @@ -1260,7 +1261,8 @@ def get_loss(loss_params, start_lr, _ntypes, _model):
loss_params["task_dim"] = task_dim
return PropertyLoss(**loss_params)
else:
raise NotImplementedError
loss_params["starter_learning_rate"] = start_lr
return TaskLoss.get_class_by_type(loss_type).get_loss(loss_params)
Comment thread
ChiahsinChu marked this conversation as resolved.


def get_single_model(
Expand Down