Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(pt): support eval_typeebd for DeepEval #4110

Merged
merged 1 commit into from
Sep 9, 2024
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
34 changes: 34 additions & 0 deletions deepmd/pt/infer/deep_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
from deepmd.pt.model.model import (
get_model,
)
from deepmd.pt.model.network.network import (
TypeEmbedNetConsistent,
)
from deepmd.pt.train.wrapper import (
ModelWrapper,
)
Expand All @@ -61,6 +64,7 @@
RESERVED_PRECISON_DICT,
)
from deepmd.pt.utils.utils import (
to_numpy_array,
to_torch_tensor,
)

Expand Down Expand Up @@ -556,6 +560,36 @@
else:
raise RuntimeError("unknown category")

def eval_typeebd(self) -> np.ndarray:
"""Evaluate output of type embedding network by using this model.

Returns
-------
np.ndarray
The output of type embedding network. The shape is [ntypes, o_size] or [ntypes + 1, o_size],
where ntypes is the number of types, and o_size is the number of nodes
in the output layer. If there are multiple type embedding networks,
these outputs will be concatenated along the second axis.

Raises
------
KeyError
If the model does not enable type embedding.

See Also
--------
deepmd.pt.model.network.network.TypeEmbedNetConsistent :
The type embedding network.
"""
out = []
for mm in self.dp.model["Default"].modules():
if mm.original_name == TypeEmbedNetConsistent.__name__:
out.append(mm(DEVICE))
if not out:
raise KeyError("The model has no type embedding networks.")

Check warning on line 589 in deepmd/pt/infer/deep_eval.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/infer/deep_eval.py#L589

Added line #L589 was not covered by tests
typeebd = torch.cat(out, dim=1)
return to_numpy_array(typeebd)

njzjz marked this conversation as resolved.
Show resolved Hide resolved

# For tests only
def eval_model(
Expand Down
8 changes: 8 additions & 0 deletions source/tests/pt/model/test_deeppot.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ def test_uni(self):
self.assertIsInstance(dp, DeepPot)
# its methods has been tested in test_dp_test

def test_eval_typeebd(self):
dp = DeepPot(str(self.model))
eval_typeebd = dp.eval_typeebd()
self.assertEqual(
eval_typeebd.shape, (len(self.config["model"]["type_map"]) + 1, 8)
)
np.testing.assert_allclose(eval_typeebd[-1], np.zeros_like(eval_typeebd[-1]))

njzjz marked this conversation as resolved.
Show resolved Hide resolved

class TestDeepPotFrozen(TestDeepPot):
def setUp(self):
Expand Down