Skip to content

Commit

Permalink
change property.npy to any name
Browse files Browse the repository at this point in the history
  • Loading branch information
Chengqian-Zhang committed Dec 8, 2024
1 parent ce9aeb3 commit 4a69e22
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 10 deletions.
2 changes: 2 additions & 0 deletions deepmd/dpmodel/output_def.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ def __init__(
r_hessian: bool = False,
magnetic: bool = False,
intensive: bool = False,
sub_var_name: list = None,
) -> None:
self.name = name
self.shape = list(shape)
Expand All @@ -220,6 +221,7 @@ def __init__(
self.r_hessian = r_hessian
self.magnetic = magnetic
self.intensive = intensive
self.sub_var_name = sub_var_name
if self.r_hessian:
if not self.reducible:
raise ValueError("only reducible variable can calculate hessian")
Expand Down
38 changes: 28 additions & 10 deletions deepmd/pt/loss/property.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ def __init__(
loss_func: str = "smooth_mae",
metric: list = ["mae"],
beta: float = 1.00,
property_name: str|list = "property",
property_dim: int|list = 1,
**kwargs,
) -> None:
r"""Construct a layer to compute loss on property.
Expand All @@ -44,6 +46,13 @@ def __init__(
self.loss_func = loss_func
self.metric = metric
self.beta = beta
if isinstance(property_name, str):
property_name = [property_name]
if isinstance(property_dim, int):
property_dim = [property_dim]
self.property_name = property_name
assert self.task_dim == sum(property_dim)
self.property_name_dim_mapping = dict(zip(property_name, property_dim))

def forward(self, input_dict, model, label, natoms, learning_rate=0.0, mae=False):
"""Return loss on properties .
Expand All @@ -69,8 +78,16 @@ def forward(self, input_dict, model, label, natoms, learning_rate=0.0, mae=False
Other losses for display.
"""
model_pred = model(**input_dict)
assert label["property"].shape[-1] == self.task_dim
assert model_pred["property"].shape[-1] == self.task_dim
nbz = model_pred["property"].shape[0]
assert model_pred["property"].shape == (nbz, self.task_dim)

concat_property = []
for property_name in self.property_name:
assert label[property_name].shape == (nbz, self.property_name_dim_mapping[property_name])
concat_property.append(label[property_name])
label["property"] = torch.cat([label["dipole_moment"],label["homo"]],dim=1)
assert label["property"].shape == (nbz, self.task_dim)

loss = torch.zeros(1, dtype=env.GLOBAL_PT_FLOAT_PRECISION, device=env.DEVICE)[0]
more_loss = {}

Expand Down Expand Up @@ -138,13 +155,14 @@ def forward(self, input_dict, model, label, natoms, learning_rate=0.0, mae=False
def label_requirement(self) -> list[DataRequirementItem]:
"""Return data label requirements needed for this loss calculation."""
label_requirement = []
label_requirement.append(
DataRequirementItem(
"property",
ndof=self.task_dim,
atomic=False,
must=False,
high_prec=True,
for property_name in self.property_name:
label_requirement.append(
DataRequirementItem(
property_name,
ndof=self.property_name_dim_mapping[property_name],
atomic=False,
must=True,
high_prec=True,
)
)
)
return label_requirement
3 changes: 3 additions & 0 deletions deepmd/pt/model/task/property.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ def __init__(
bias_atom_p: Optional[torch.Tensor] = None,
intensive: bool = False,
bias_method: str = "normal",
property_name: str|list = "property",
resnet_dt: bool = True,
numb_fparam: int = 0,
numb_aparam: int = 0,
Expand All @@ -95,6 +96,7 @@ def __init__(
self.task_dim = task_dim
self.intensive = intensive
self.bias_method = bias_method
self.property_name = property_name
super().__init__(
var_name="property",
ntypes=ntypes,
Expand Down Expand Up @@ -126,6 +128,7 @@ def output_def(self) -> FittingOutputDef:
r_differentiable=False,
c_differentiable=False,
intensive=self.intensive,
sub_var_name=self.property_name,
),
]
)
Expand Down
2 changes: 2 additions & 0 deletions deepmd/pt/utils/stat.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,8 @@ def compute_output_stats(
]
del keys
keys = new_keys
from IPython import embed
embed()
# split system based on label
atomic_sampled_idx = defaultdict(list)
global_sampled_idx = defaultdict(list)
Expand Down
18 changes: 18 additions & 0 deletions deepmd/utils/argcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -1581,6 +1581,7 @@ def fitting_property():
doc_task_dim = "The dimension of outputs of fitting net"
doc_intensive = "Whether the fitting property is intensive"
doc_bias_method = "The method of applying the bias to each atomic output, user can select 'normal' or 'no_bias'. If 'no_bias' is used, no bias will be added to the atomic output."
doc_property_name = "TODO"
return [
Argument("numb_fparam", int, optional=True, default=0, doc=doc_numb_fparam),
Argument("numb_aparam", int, optional=True, default=0, doc=doc_numb_aparam),
Expand Down Expand Up @@ -1614,6 +1615,7 @@ def fitting_property():
Argument(
"bias_method", str, optional=True, default="normal", doc=doc_bias_method
),
Argument("property_name", [str, list], optional=True, default="property", doc=doc_property_name),
]


Expand Down Expand Up @@ -2481,6 +2483,8 @@ def loss_property():
doc_loss_func = "The loss function to minimize, such as 'mae','smooth_mae'."
doc_metric = "The metric for display. This list can include 'smooth_mae', 'mae', 'mse' and 'rmse'."
doc_beta = "The 'beta' parameter in 'smooth_mae' loss."
doc_property_name = "The names of fitting properties, which should be consistent with the property names in the dataset."
doc_property_dim = "The dimensions of fitting properties, which should be consistent with the property dimensions in the dataset."
return [
Argument(
"loss_func",
Expand All @@ -2503,6 +2507,20 @@ def loss_property():
default=1.00,
doc=doc_beta,
),
Argument(
"property_name",
[str, list],
optional=True,
default="property",
doc=doc_property_name,
),
Argument(
"property_dim",
[int, list],
optional=True,
default=1,
doc=doc_property_dim,
),
]


Expand Down

0 comments on commit 4a69e22

Please sign in to comment.