-
Notifications
You must be signed in to change notification settings - Fork 629
Feat(pt): Support fitting_net input statistics. #4504
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
Merged
Merged
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
ec820ff
Init branch
Chengqian-Zhang 2cac1c6
Delete files
Chengqian-Zhang c561efe
Delete file:test_neighbor_stat.py
Chengqian-Zhang af5e589
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] e7dfe91
Merge branch 'devel' into fitting_stat
Chengqian-Zhang bc7a0aa
Add aparam
Chengqian-Zhang 95229c5
Delete fparam.npy
Chengqian-Zhang 9e0b2ff
Add unittest of fitting_stat
Chengqian-Zhang 66cab94
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 3072fb1
Add protection
Chengqian-Zhang 1a4836c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 1b0184b
Merge branch 'devel' into fitting_stat
Chengqian-Zhang 3e5cbe4
Add near zero UT
Chengqian-Zhang f968b76
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 2d808af
Delete useless import
Chengqian-Zhang 4f1e009
Merge branch 'fitting_stat' of github.com:Chengqian-Zhang/deepmd-kit …
Chengqian-Zhang 68b0f61
Fix UT
Chengqian-Zhang 1bd0854
Delete checkpoint
Chengqian-Zhang 9010763
Rerun YT
Chengqian-Zhang 3bc746d
Delete ignore
Chengqian-Zhang bdd7b3e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] b766b0c
Add noqa
Chengqian-Zhang b034bee
Solve conflict
Chengqian-Zhang 7dd3609
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] e651bf4
Fix pre-coommit
Chengqian-Zhang 9cb04eb
Solve pre-commit
Chengqian-Zhang 1b033a9
Delete ignore
Chengqian-Zhang 3aed1b4
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| # SPDX-License-Identifier: LGPL-3.0-or-later | ||
| import unittest | ||
|
|
||
| import numpy as np | ||
|
|
||
| from deepmd.pt.model.descriptor import ( | ||
| DescrptSeA, | ||
| ) | ||
| from deepmd.pt.model.task import ( | ||
| EnergyFittingNet, | ||
| ) | ||
| from deepmd.pt.utils.utils import ( | ||
| to_numpy_array, | ||
| to_torch_tensor, | ||
| ) | ||
|
|
||
|
|
||
| def _make_fake_data_pt(sys_natoms, sys_nframes, avgs, stds): | ||
| merged_output_stat = [] | ||
| nsys = len(sys_natoms) | ||
| ndof = len(avgs) | ||
| for ii in range(nsys): | ||
| sys_dict = {} | ||
| tmp_data_f = [] | ||
| tmp_data_a = [] | ||
| for jj in range(ndof): | ||
| rng = np.random.default_rng(2025 * ii + 220 * jj) | ||
| tmp_data_f.append( | ||
| rng.normal(loc=avgs[jj], scale=stds[jj], size=(sys_nframes[ii], 1)) | ||
| ) | ||
| rng = np.random.default_rng(220 * ii + 1636 * jj) | ||
| tmp_data_a.append( | ||
| rng.normal( | ||
| loc=avgs[jj], scale=stds[jj], size=(sys_nframes[ii], sys_natoms[ii]) | ||
| ) | ||
| ) | ||
| tmp_data_f = np.transpose(tmp_data_f, (1, 2, 0)) | ||
| tmp_data_a = np.transpose(tmp_data_a, (1, 2, 0)) | ||
| sys_dict["fparam"] = to_torch_tensor(tmp_data_f) | ||
| sys_dict["aparam"] = to_torch_tensor(tmp_data_a) | ||
| merged_output_stat.append(sys_dict) | ||
| return merged_output_stat | ||
|
|
||
|
|
||
| def _brute_fparam_pt(data, ndim): | ||
| adata = [to_numpy_array(ii["fparam"]) for ii in data] | ||
| all_data = [] | ||
| for ii in adata: | ||
| tmp = np.reshape(ii, [-1, ndim]) | ||
| if len(all_data) == 0: | ||
| all_data = np.array(tmp) | ||
| else: | ||
| all_data = np.concatenate((all_data, tmp), axis=0) | ||
| avg = np.average(all_data, axis=0) | ||
| std = np.std(all_data, axis=0) | ||
| return avg, std | ||
|
|
||
|
|
||
| def _brute_aparam_pt(data, ndim): | ||
| adata = [to_numpy_array(ii["aparam"]) for ii in data] | ||
| all_data = [] | ||
| for ii in adata: | ||
| tmp = np.reshape(ii, [-1, ndim]) | ||
| if len(all_data) == 0: | ||
| all_data = np.array(tmp) | ||
| else: | ||
| all_data = np.concatenate((all_data, tmp), axis=0) | ||
| avg = np.average(all_data, axis=0) | ||
| std = np.std(all_data, axis=0) | ||
| return avg, std | ||
|
|
||
|
|
||
| class TestEnerFittingStat(unittest.TestCase): | ||
| def test(self) -> None: | ||
| descrpt = DescrptSeA(6.0, 5.8, [46, 92], neuron=[25, 50, 100], axis_neuron=16) | ||
| fitting = EnergyFittingNet( | ||
| descrpt.get_ntypes(), | ||
| descrpt.get_dim_out(), | ||
| neuron=[240, 240, 240], | ||
| resnet_dt=True, | ||
| numb_fparam=3, | ||
| numb_aparam=3, | ||
| ) | ||
| avgs = [0, 10, 100] | ||
| stds = [2, 0.4, 0.00001] | ||
| sys_natoms = [10, 100] | ||
| sys_nframes = [5, 2] | ||
| all_data = _make_fake_data_pt(sys_natoms, sys_nframes, avgs, stds) | ||
| frefa, frefs = _brute_fparam_pt(all_data, len(avgs)) | ||
| arefa, arefs = _brute_aparam_pt(all_data, len(avgs)) | ||
| fitting.compute_input_stats(all_data, protection=1e-2) | ||
| frefs_inv = 1.0 / frefs | ||
| arefs_inv = 1.0 / arefs | ||
| frefs_inv[frefs_inv > 100] = 100 | ||
| arefs_inv[arefs_inv > 100] = 100 | ||
| np.testing.assert_almost_equal(frefa, to_numpy_array(fitting.fparam_avg)) | ||
| np.testing.assert_almost_equal( | ||
| frefs_inv, to_numpy_array(fitting.fparam_inv_std) | ||
| ) | ||
| np.testing.assert_almost_equal(arefa, to_numpy_array(fitting.aparam_avg)) | ||
| np.testing.assert_almost_equal( | ||
| arefs_inv, to_numpy_array(fitting.aparam_inv_std) | ||
| ) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.