From 5061492268ef53fc5b9bbcf94dc13f0c0b0b0f4f Mon Sep 17 00:00:00 2001 From: Han Wang Date: Wed, 16 Sep 2020 13:32:53 +0800 Subject: [PATCH 1/2] global_polar: print loss not normalized by sqrt(natoms). add dp test for global_polar --- source/train/Loss.py | 7 ++++--- source/train/test.py | 27 +++++++++++++++++++++++---- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/source/train/Loss.py b/source/train/Loss.py index d939273f26..1f336325a3 100644 --- a/source/train/Loss.py +++ b/source/train/Loss.py @@ -301,11 +301,12 @@ def build (self, polar_hat = label_dict[self.label_name] polar = model_dict[self.tensor_name] l2_loss = tf.reduce_mean( tf.square(self.scale*(polar - polar_hat)), name='l2_'+suffix) + more_loss = {'nonorm': l2_loss} if not self.atomic : atom_norm = 1./ global_cvt_2_tf_float(natoms[0]) l2_loss = l2_loss * atom_norm self.l2_l = l2_loss - more_loss = {} + self.l2_more = more_loss['nonorm'] return l2_loss, more_loss @@ -321,10 +322,10 @@ def print_on_training(self, feed_dict_test, feed_dict_batch) : error_test\ - = sess.run([self.l2_l], \ + = sess.run([self.l2_more], \ feed_dict=feed_dict_test) error_train\ - = sess.run([self.l2_l], \ + = sess.run([self.l2_more], \ feed_dict=feed_dict_batch) print_str = "" prop_fmt = " %9.2e %9.2e" diff --git a/source/train/test.py b/source/train/test.py index d8639020ce..c01b81d4d0 100644 --- a/source/train/test.py +++ b/source/train/test.py @@ -12,6 +12,7 @@ from deepmd import DeepPot from deepmd import DeepDipole from deepmd import DeepPolar +from deepmd import DeepGlobalPolar from deepmd import DeepWFC from tensorflow.python.framework import ops @@ -28,6 +29,8 @@ def test (args): dp = DeepDipole(args.model) elif de.model_type == 'polar': dp = DeepPolar(args.model) + elif de.model_type == 'global_polar': + dp = DeepGlobalPolar(args.model) elif de.model_type == 'wfc': dp = DeepWFC(args.model) else : @@ -41,7 +44,9 @@ def test (args): elif de.model_type == 'dipole': err, siz = test_dipole(dp, args) elif de.model_type == 'polar': - err, siz = test_polar(dp, args) + err, siz = test_polar(dp, args, global_polar=False) + elif de.model_type == 'global_polar': + err, siz = test_polar(dp, args, global_polar=True) elif de.model_type == 'wfc': err, siz = test_wfc(dp, args) else : @@ -61,6 +66,8 @@ def test (args): print_dipole_sys_avg(avg_err) elif de.model_type == 'polar': print_polar_sys_avg(avg_err) + elif de.model_type == 'global_polar': + print_polar_sys_avg(avg_err) elif de.model_type == 'wfc': print_wfc_sys_avg(avg_err) else : @@ -223,12 +230,15 @@ def print_wfc_sys_avg(avg): print ("WFC L2err : %e eV/A" % avg[0]) -def test_polar (dp, args) : +def test_polar (dp, args, global_polar = False) : if args.rand_seed is not None : np.random.seed(args.rand_seed % (2**32)) data = DeepmdData(args.system, args.set_prefix, shuffle_test = args.shuffle_test) - data.add('polarizability', 9, atomic=True, must=True, high_prec=False, type_sel = dp.get_sel_type()) + if not global_polar: + data.add('polarizability', 9, atomic=True, must=True, high_prec=False, type_sel = dp.get_sel_type()) + else: + data.add('polarizability', 9, atomic=False, must=True, high_prec=False, type_sel = dp.get_sel_type()) test_data = data.get_test () numb_test = args.numb_test natoms = len(test_data["type"][0]) @@ -239,12 +249,21 @@ def test_polar (dp, args) : box = test_data["box"][:numb_test] atype = test_data["type"][0] polar = dp.eval(coord, box, atype) + sel_type = dp.get_sel_type() + sel_natoms = 0 + for ii in sel_type: + sel_natoms += sum(atype == ii) polar = polar.reshape([numb_test,-1]) l2f = (l2err (polar - test_data["polarizability"] [:numb_test])) + l2fs = l2f/np.sqrt(sel_natoms) + l2fa = l2f/sel_natoms print ("# number of test data : %d " % numb_test) - print ("Polarizability L2err : %e eV/A" % l2f) + print ("Polarizability L2err : %e eV/A" % l2f) + if global_polar: + print ("Polarizability L2err/sqrtN : %e eV/A" % l2fs) + print ("Polarizability L2err/N : %e eV/A" % l2fa) detail_file = args.detail_file if detail_file is not None : From e110ec462fb32ace1f0ca26493bd885a2c3c54b0 Mon Sep 17 00:00:00 2001 From: Han Wang Date: Thu, 17 Sep 2020 14:47:40 +0800 Subject: [PATCH 2/2] correct way of getting site package path --- setup.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/setup.py b/setup.py index 99a86d3da8..2e147160e1 100644 --- a/setup.py +++ b/setup.py @@ -4,17 +4,16 @@ from setuptools_scm import get_version from packaging.version import LegacyVersion from os import path, makedirs -import imp, sys, platform +import os, imp, sys, platform, sysconfig def get_dp_install_path() : - site_packages_path = path.join(path.dirname(path.__file__), 'site-packages') - dp_scm_version = get_version(root="./", relative_to=__file__) + site_packages_path = sysconfig.get_paths()['purelib'] + dp_scm_version = get_version(root=".", relative_to=__file__) python_version = 'py' + str(sys.version_info.major + sys.version_info.minor * 0.1) os_info = sys.platform machine_info = platform.machine() - dp_pip_install_path = site_packages_path + '/deepmd' - dp_setup_install_path = site_packages_path + '/deepmd_kit-' + dp_scm_version + '-' + python_version + '-' + os_info + '-' + machine_info + '.egg/deepmd' - + dp_pip_install_path = os.path.join(site_packages_path, 'deepmd') + dp_setup_install_path = os.path.join(site_packages_path, 'deepmd_kit-' + dp_scm_version + '-' + python_version + '-' + os_info + '-' + machine_info + '.egg', 'deepmd') return dp_pip_install_path, dp_setup_install_path readme_file = path.join(path.dirname(path.abspath(__file__)), 'README.md')