def test_model(self): jfile = 'water_se_a_type.json' jdata = j_loader(jfile) systems = j_must_have(jdata, 'systems') set_pfx = j_must_have(jdata, 'set_prefix') batch_size = j_must_have(jdata, 'batch_size') test_size = j_must_have(jdata, 'numb_test') batch_size = 1 test_size = 1 stop_batch = j_must_have(jdata, 'stop_batch') rcut = j_must_have (jdata['model']['descriptor'], 'rcut') data = DataSystem(systems, set_pfx, batch_size, test_size, rcut, run_opt = None) test_data = data.get_test () numb_test = 1 jdata['model']['descriptor'].pop('type', None) descrpt = DescrptSeA(**jdata['model']['descriptor'], uniform_seed = True) jdata['model']['fitting_net']['descrpt'] = descrpt fitting = EnerFitting(**jdata['model']['fitting_net'], uniform_seed = True) typeebd_param = jdata['model']['type_embedding'] typeebd = TypeEmbedNet( neuron = typeebd_param['neuron'], resnet_dt = typeebd_param['resnet_dt'], seed = typeebd_param['seed'], uniform_seed = True) model = EnerModel(descrpt, fitting, typeebd) # model._compute_dstats([test_data['coord']], [test_data['box']], [test_data['type']], [test_data['natoms_vec']], [test_data['default_mesh']]) input_data = {'coord' : [test_data['coord']], 'box': [test_data['box']], 'type': [test_data['type']], 'natoms_vec' : [test_data['natoms_vec']], 'default_mesh' : [test_data['default_mesh']] } model._compute_input_stat(input_data) model.descrpt.bias_atom_e = data.compute_energy_shift() t_prop_c = tf.placeholder(tf.float32, [5], name='t_prop_c') t_energy = tf.placeholder(GLOBAL_ENER_FLOAT_PRECISION, [None], name='t_energy') t_force = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None], name='t_force') t_virial = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None], name='t_virial') t_atom_ener = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None], name='t_atom_ener') t_coord = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None], name='i_coord') t_type = tf.placeholder(tf.int32, [None], name='i_type') t_natoms = tf.placeholder(tf.int32, [model.ntypes+2], name='i_natoms') t_box = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None, 9], name='i_box') t_mesh = tf.placeholder(tf.int32, [None], name='i_mesh') is_training = tf.placeholder(tf.bool) t_fparam = None inputs_dict = {} model_pred \ = model.build (t_coord, t_type, t_natoms, t_box, t_mesh, inputs_dict, suffix = "se_a_type", reuse = False) energy = model_pred['energy'] force = model_pred['force'] virial = model_pred['virial'] atom_ener = model_pred['atom_ener'] feed_dict_test = {t_prop_c: test_data['prop_c'], t_energy: test_data['energy'] [:numb_test], t_force: np.reshape(test_data['force'] [:numb_test, :], [-1]), t_virial: np.reshape(test_data['virial'] [:numb_test, :], [-1]), t_atom_ener: np.reshape(test_data['atom_ener'][:numb_test, :], [-1]), t_coord: np.reshape(test_data['coord'] [:numb_test, :], [-1]), t_box: test_data['box'] [:numb_test, :], t_type: np.reshape(test_data['type'] [:numb_test, :], [-1]), t_natoms: test_data['natoms_vec'], t_mesh: test_data['default_mesh'], is_training: False} sess = self.test_session().__enter__() sess.run(tf.global_variables_initializer()) [e, f, v] = sess.run([energy, force, virial], feed_dict = feed_dict_test) # print(sess.run(model.type_embedding)) # np.savetxt('tmp.out', sess.run(descrpt.dout, feed_dict = feed_dict_test), fmt='%.10e') # # print(sess.run(model.atype_embed, feed_dict = feed_dict_test)) # print(sess.run(fitting.inputs, feed_dict = feed_dict_test)) # print(sess.run(fitting.outs, feed_dict = feed_dict_test)) # print(sess.run(fitting.atype_embed, feed_dict = feed_dict_test)) e = e.reshape([-1]) f = f.reshape([-1]) v = v.reshape([-1]) np.savetxt('e.out', e.reshape([1, -1]), delimiter=',') np.savetxt('f.out', f.reshape([1, -1]), delimiter=',') np.savetxt('v.out', v.reshape([1, -1]), delimiter=',') refe = [6.049065170680415804e+01] reff = [1.021832439441947293e-01,1.122650466359011306e-01,3.927874278714531091e-03,1.407089812207832635e-01,1.312473824343091400e-01,-1.228371057389851181e-02,-1.109672154547165501e-01,6.582735820731049070e-02,1.251568633647655391e-03,7.933758749748777428e-02,-1.831777072317984367e-01,-6.173090134630876760e-03,-2.703597126460742794e-01,4.817856571062521104e-02,1.491963457594796399e-02,5.909711543832503466e-02,-1.743406457563475287e-01,-1.642276779780762769e-03] refv = [-6.932736357193732823e-01,1.453756052949563837e-01,2.138263139115256783e-02,1.453756052949564392e-01,-3.880901656480436612e-01,-7.782259726407755700e-03,2.138263139115256437e-02,-7.782259726407749628e-03,-1.225285973678705374e-03] refe = np.reshape(refe, [-1]) reff = np.reshape(reff, [-1]) refv = np.reshape(refv, [-1]) places = 10 np.testing.assert_almost_equal(e, refe, places) np.testing.assert_almost_equal(f, reff, places) np.testing.assert_almost_equal(v, refv, places)
def _init_param(self, jdata): # model config model_param = j_must_have(jdata, 'model') descrpt_param = j_must_have(model_param, 'descriptor') fitting_param = j_must_have(model_param, 'fitting_net') typeebd_param = model_param.get('type_embedding', None) self.model_param = model_param self.descrpt_param = descrpt_param # descriptor try: descrpt_type = descrpt_param['type'] except KeyError: raise KeyError('the type of descriptor should be set by `type`') self.descrpt = Descriptor(**descrpt_param) # fitting net fitting_type = fitting_param.get('type', 'ener') self.fitting_type = fitting_type fitting_param.pop('type', None) fitting_param['descrpt'] = self.descrpt if fitting_type == 'ener': self.fitting = EnerFitting(**fitting_param) # elif fitting_type == 'wfc': # self.fitting = WFCFitting(fitting_param, self.descrpt) elif fitting_type == 'dipole': if descrpt_type == 'se_e2_a': self.fitting = DipoleFittingSeA(**fitting_param) else : raise RuntimeError('fitting dipole only supports descrptors: se_e2_a') elif fitting_type == 'polar': # if descrpt_type == 'loc_frame': # self.fitting = PolarFittingLocFrame(fitting_param, self.descrpt) if descrpt_type == 'se_e2_a': self.fitting = PolarFittingSeA(**fitting_param) else : raise RuntimeError('fitting polar only supports descrptors: loc_frame and se_e2_a') elif fitting_type == 'global_polar': if descrpt_type == 'se_e2_a': self.fitting = GlobalPolarFittingSeA(**fitting_param) else : raise RuntimeError('fitting global_polar only supports descrptors: loc_frame and se_e2_a') else : raise RuntimeError('unknow fitting type ' + fitting_type) # type embedding if typeebd_param is not None: self.typeebd = TypeEmbedNet( neuron=typeebd_param['neuron'], resnet_dt=typeebd_param['resnet_dt'], activation_function=typeebd_param['activation_function'], precision=typeebd_param['precision'], trainable=typeebd_param['trainable'], seed=typeebd_param['seed'] ) else: self.typeebd = None # init model # infer model type by fitting_type if fitting_type == 'ener': self.model = EnerModel( self.descrpt, self.fitting, self.typeebd, model_param.get('type_map'), model_param.get('data_stat_nbatch', 10), model_param.get('data_stat_protect', 1e-2), model_param.get('use_srtab'), model_param.get('smin_alpha'), model_param.get('sw_rmin'), model_param.get('sw_rmax') ) # elif fitting_type == 'wfc': # self.model = WFCModel(model_param, self.descrpt, self.fitting) elif fitting_type == 'dipole': self.model = DipoleModel( self.descrpt, self.fitting, model_param.get('type_map'), model_param.get('data_stat_nbatch', 10), model_param.get('data_stat_protect', 1e-2) ) elif fitting_type == 'polar': self.model = PolarModel( self.descrpt, self.fitting, model_param.get('type_map'), model_param.get('data_stat_nbatch', 10), model_param.get('data_stat_protect', 1e-2) ) elif fitting_type == 'global_polar': self.model = GlobalPolarModel( self.descrpt, self.fitting, model_param.get('type_map'), model_param.get('data_stat_nbatch', 10), model_param.get('data_stat_protect', 1e-2) ) else : raise RuntimeError('get unknown fitting type when building model') # learning rate lr_param = j_must_have(jdata, 'learning_rate') scale_by_worker = lr_param.get('scale_by_worker', 'linear') if scale_by_worker == 'linear': self.scale_lr_coef = float(self.run_opt.world_size) elif scale_by_worker == 'sqrt': self.scale_lr_coef = np.sqrt(self.run_opt.world_size).real else: self.scale_lr_coef = 1. lr_type = lr_param.get('type', 'exp') if lr_type == 'exp': self.lr = LearningRateExp(lr_param['start_lr'], lr_param['stop_lr'], lr_param['decay_steps']) else : raise RuntimeError('unknown learning_rate type ' + lr_type) # loss # infer loss type by fitting_type loss_param = jdata.get('loss', None) loss_type = loss_param.get('type', 'ener') if fitting_type == 'ener': loss_param.pop('type', None) loss_param['starter_learning_rate'] = self.lr.start_lr() if loss_type == 'ener': self.loss = EnerStdLoss(**loss_param) elif loss_type == 'ener_dipole': self.loss = EnerDipoleLoss(**loss_param) else: raise RuntimeError('unknow loss type') elif fitting_type == 'wfc': self.loss = TensorLoss(loss_param, model = self.model, tensor_name = 'wfc', tensor_size = self.model.get_out_size(), label_name = 'wfc') elif fitting_type == 'dipole': self.loss = TensorLoss(loss_param, model = self.model, tensor_name = 'dipole', tensor_size = 3, label_name = 'dipole') elif fitting_type == 'polar': self.loss = TensorLoss(loss_param, model = self.model, tensor_name = 'polar', tensor_size = 9, label_name = 'polarizability') elif fitting_type == 'global_polar': self.loss = TensorLoss(loss_param, model = self.model, tensor_name = 'global_polar', tensor_size = 9, atomic = False, label_name = 'polarizability') else : raise RuntimeError('get unknown fitting type when building loss function') # training tr_data = jdata['training'] self.disp_file = tr_data.get('disp_file', 'lcurve.out') self.disp_freq = tr_data.get('disp_freq', 1000) self.save_freq = tr_data.get('save_freq', 1000) self.save_ckpt = tr_data.get('save_ckpt', 'model.ckpt') self.display_in_training = tr_data.get('disp_training', True) self.timing_in_training = tr_data.get('time_training', True) self.profiling = self.run_opt.is_chief and tr_data.get('profiling', False) self.profiling_file = tr_data.get('profiling_file', 'timeline.json') self.tensorboard = self.run_opt.is_chief and tr_data.get('tensorboard', False) self.tensorboard_log_dir = tr_data.get('tensorboard_log_dir', 'log') self.tensorboard_freq = tr_data.get('tensorboard_freq', 1) # self.sys_probs = tr_data['sys_probs'] # self.auto_prob_style = tr_data['auto_prob'] self.useBN = False if fitting_type == 'ener' and self.fitting.get_numb_fparam() > 0 : self.numb_fparam = self.fitting.get_numb_fparam() else : self.numb_fparam = 0 if tr_data.get("validation_data", None) is not None: self.valid_numb_batch = tr_data["validation_data"].get("numb_btch", 1) else: self.valid_numb_batch = 1 # if init the graph with the frozen model self.frz_model = None self.model_type = None
def test_model(self): jfile = 'water_se_a_fparam.json' jdata = j_loader(jfile) systems = j_must_have(jdata, 'systems') set_pfx = j_must_have(jdata, 'set_prefix') batch_size = j_must_have(jdata, 'batch_size') test_size = j_must_have(jdata, 'numb_test') batch_size = 1 test_size = 1 stop_batch = j_must_have(jdata, 'stop_batch') rcut = j_must_have(jdata['model']['descriptor'], 'rcut') data = DataSystem(systems, set_pfx, batch_size, test_size, rcut, run_opt=None) test_data = data.get_test() numb_test = 1 jdata['model']['descriptor'].pop('type', None) descrpt = DescrptSeA(**jdata['model']['descriptor'], uniform_seed=True) jdata['model']['fitting_net']['descrpt'] = descrpt fitting = EnerFitting(**jdata['model']['fitting_net'], uniform_seed=True) # descrpt = DescrptSeA(jdata['model']['descriptor']) # fitting = EnerFitting(jdata['model']['fitting_net'], descrpt) model = EnerModel(descrpt, fitting) # model._compute_dstats([test_data['coord']], [test_data['box']], [test_data['type']], [test_data['natoms_vec']], [test_data['default_mesh']]) input_data = { 'coord': [test_data['coord']], 'box': [test_data['box']], 'type': [test_data['type']], 'natoms_vec': [test_data['natoms_vec']], 'default_mesh': [test_data['default_mesh']], 'fparam': [test_data['fparam']], } model._compute_input_stat(input_data) model.descrpt.bias_atom_e = data.compute_energy_shift() t_prop_c = tf.placeholder(tf.float32, [5], name='t_prop_c') t_energy = tf.placeholder(GLOBAL_ENER_FLOAT_PRECISION, [None], name='t_energy') t_force = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None], name='t_force') t_virial = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None], name='t_virial') t_atom_ener = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None], name='t_atom_ener') t_coord = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None], name='i_coord') t_type = tf.placeholder(tf.int32, [None], name='i_type') t_natoms = tf.placeholder(tf.int32, [model.ntypes + 2], name='i_natoms') t_box = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None, 9], name='i_box') t_mesh = tf.placeholder(tf.int32, [None], name='i_mesh') t_fparam = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None], name='i_fparam') is_training = tf.placeholder(tf.bool) input_dict = {} input_dict['fparam'] = t_fparam model_pred\ = model.build (t_coord, t_type, t_natoms, t_box, t_mesh, input_dict, suffix = "se_a_fparam", reuse = False) energy = model_pred['energy'] force = model_pred['force'] virial = model_pred['virial'] atom_ener = model_pred['atom_ener'] feed_dict_test = { t_prop_c: test_data['prop_c'], t_energy: test_data['energy'][:numb_test], t_force: np.reshape(test_data['force'][:numb_test, :], [-1]), t_virial: np.reshape(test_data['virial'][:numb_test, :], [-1]), t_atom_ener: np.reshape(test_data['atom_ener'][:numb_test, :], [-1]), t_coord: np.reshape(test_data['coord'][:numb_test, :], [-1]), t_box: test_data['box'][:numb_test, :], t_type: np.reshape(test_data['type'][:numb_test, :], [-1]), t_natoms: test_data['natoms_vec'], t_mesh: test_data['default_mesh'], t_fparam: np.reshape(test_data['fparam'][:numb_test, :], [-1]), is_training: False } sess = self.test_session().__enter__() sess.run(tf.global_variables_initializer()) [e, f, v] = sess.run([energy, force, virial], feed_dict=feed_dict_test) e = e.reshape([-1]) f = f.reshape([-1]) v = v.reshape([-1]) refe = [61.35473702079649] reff = [ 7.789591210641927388e-02, 9.411176646369459609e-02, 3.785806413688173194e-03, 1.430830954178063386e-01, 1.146964190520970150e-01, -1.320340288927138173e-02, -7.308720494747594776e-02, 6.508269338140809657e-02, 5.398739145542804643e-04, 5.863268336973800898e-02, -1.603409523950408699e-01, -5.083084610994957619e-03, -2.551569799443983988e-01, 3.087934885732580501e-02, 1.508590526622844222e-02, 4.863249399791078065e-02, -1.444292753594846324e-01, -1.125098094204559241e-03 ] refv = [ -6.069498397488943819e-01, 1.101778888191114192e-01, 1.981907430646132409e-02, 1.101778888191114608e-01, -3.315612988100872793e-01, -5.999739184898976799e-03, 1.981907430646132756e-02, -5.999739184898974197e-03, -1.198656608172396325e-03 ] refe = np.reshape(refe, [-1]) reff = np.reshape(reff, [-1]) refv = np.reshape(refv, [-1]) places = 10 np.testing.assert_almost_equal(e, refe, places) np.testing.assert_almost_equal(f, reff, places) np.testing.assert_almost_equal(v, refv, places)
def test_model(self): jfile = 'water_se_a_srtab.json' jdata = j_loader(jfile) systems = j_must_have(jdata, 'systems') set_pfx = j_must_have(jdata, 'set_prefix') batch_size = j_must_have(jdata, 'batch_size') test_size = j_must_have(jdata, 'numb_test') batch_size = 1 test_size = 1 stop_batch = j_must_have(jdata, 'stop_batch') rcut = j_must_have (jdata['model']['descriptor'], 'rcut') data = DataSystem(systems, set_pfx, batch_size, test_size, rcut, run_opt = None) test_data = data.get_test () numb_test = 1 jdata['model']['descriptor'].pop('type', None) descrpt = DescrptSeA(**jdata['model']['descriptor'], uniform_seed = True) jdata['model']['fitting_net']['descrpt'] = descrpt fitting = EnerFitting(**jdata['model']['fitting_net'], uniform_seed = True) # descrpt = DescrptSeA(jdata['model']['descriptor']) # fitting = EnerFitting(jdata['model']['fitting_net'], descrpt) model = EnerModel( descrpt, fitting, None, jdata['model'].get('type_map'), jdata['model'].get('data_stat_nbatch'), jdata['model'].get('data_stat_protect'), jdata['model'].get('use_srtab'), jdata['model'].get('smin_alpha'), jdata['model'].get('sw_rmin'), jdata['model'].get('sw_rmax') ) # model._compute_dstats([test_data['coord']], [test_data['box']], [test_data['type']], [test_data['natoms_vec']], [test_data['default_mesh']]) input_data = {'coord' : [test_data['coord']], 'box': [test_data['box']], 'type': [test_data['type']], 'natoms_vec' : [test_data['natoms_vec']], 'default_mesh' : [test_data['default_mesh']] } model._compute_input_stat(input_data) model.descrpt.bias_atom_e = data.compute_energy_shift() t_prop_c = tf.placeholder(tf.float32, [5], name='t_prop_c') t_energy = tf.placeholder(GLOBAL_ENER_FLOAT_PRECISION, [None], name='t_energy') t_force = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None], name='t_force') t_virial = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None], name='t_virial') t_atom_ener = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None], name='t_atom_ener') t_coord = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None], name='i_coord') t_type = tf.placeholder(tf.int32, [None], name='i_type') t_natoms = tf.placeholder(tf.int32, [model.ntypes+2], name='i_natoms') t_box = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None, 9], name='i_box') t_mesh = tf.placeholder(tf.int32, [None], name='i_mesh') is_training = tf.placeholder(tf.bool) t_fparam = None model_pred\ = model.build (t_coord, t_type, t_natoms, t_box, t_mesh, t_fparam, suffix = "se_a_srtab", reuse = False) energy = model_pred['energy'] force = model_pred['force'] virial = model_pred['virial'] atom_ener = model_pred['atom_ener'] feed_dict_test = {t_prop_c: test_data['prop_c'], t_energy: test_data['energy'] [:numb_test], t_force: np.reshape(test_data['force'] [:numb_test, :], [-1]), t_virial: np.reshape(test_data['virial'] [:numb_test, :], [-1]), t_atom_ener: np.reshape(test_data['atom_ener'][:numb_test, :], [-1]), t_coord: np.reshape(test_data['coord'] [:numb_test, :], [-1]), t_box: test_data['box'] [:numb_test, :], t_type: np.reshape(test_data['type'] [:numb_test, :], [-1]), t_natoms: test_data['natoms_vec'], t_mesh: test_data['default_mesh'], is_training: False} sess = self.test_session().__enter__() sess.run(tf.global_variables_initializer()) [e, f, v] = sess.run([energy, force, virial], feed_dict = feed_dict_test) e = e.reshape([-1]) f = f.reshape([-1]) v = v.reshape([-1]) refe = [1.141610882066236599e+02] reff = [-1.493121233165248043e+02,-1.831419491743885715e+02,-8.439542992300344437e+00,-1.811987095947552859e+02,-1.476380826187439084e+02,1.264271856742560018e+01,1.544377958934875323e+02,-7.816520233903435866e+00,1.287925245463442225e+00,-4.000393268449002449e+00,1.910748885843098890e+02,7.134789955349889468e+00,1.826908441979261113e+02,3.677156386479059513e+00,-1.122312112141401741e+01,-2.617413911684622008e+00,1.438445070562470391e+02,-1.402769654524568033e+00] refv = [3.585047655925112622e+02,-7.569252978336677984e+00,-1.068382043878426124e+01,-7.569252978336677096e+00,3.618439481685132932e+02,5.448668500896081568e+00,-1.068382043878426302e+01,5.448668500896082456e+00,1.050393462151727686e+00] refe = np.reshape(refe, [-1]) reff = np.reshape(reff, [-1]) refv = np.reshape(refv, [-1]) places = 10 np.testing.assert_almost_equal(e, refe, places) np.testing.assert_almost_equal(f, reff, places) np.testing.assert_almost_equal(v, refv, places)
def test_model(self): jfile = 'water_se_t.json' jdata = j_loader(jfile) systems = j_must_have(jdata, 'systems') set_pfx = j_must_have(jdata, 'set_prefix') batch_size = j_must_have(jdata, 'batch_size') test_size = j_must_have(jdata, 'numb_test') batch_size = 1 test_size = 1 stop_batch = j_must_have(jdata, 'stop_batch') rcut = j_must_have(jdata['model']['descriptor'], 'rcut') data = DataSystem(systems, set_pfx, batch_size, test_size, rcut, run_opt=None) test_data = data.get_test() numb_test = 1 jdata['model']['descriptor'].pop('type', None) descrpt = DescrptSeT(**jdata['model']['descriptor'], uniform_seed=True) jdata['model']['fitting_net']['descrpt'] = descrpt fitting = EnerFitting(**jdata['model']['fitting_net'], uniform_seed=True) model = EnerModel(descrpt, fitting) input_data = { 'coord': [test_data['coord']], 'box': [test_data['box']], 'type': [test_data['type']], 'natoms_vec': [test_data['natoms_vec']], 'default_mesh': [test_data['default_mesh']] } model._compute_input_stat(input_data) model.descrpt.bias_atom_e = data.compute_energy_shift() t_prop_c = tf.placeholder(tf.float32, [5], name='t_prop_c') t_energy = tf.placeholder(GLOBAL_ENER_FLOAT_PRECISION, [None], name='t_energy') t_force = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None], name='t_force') t_virial = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None], name='t_virial') t_atom_ener = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None], name='t_atom_ener') t_coord = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None], name='i_coord') t_type = tf.placeholder(tf.int32, [None], name='i_type') t_natoms = tf.placeholder(tf.int32, [model.ntypes + 2], name='i_natoms') t_box = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None, 9], name='i_box') t_mesh = tf.placeholder(tf.int32, [None], name='i_mesh') is_training = tf.placeholder(tf.bool) t_fparam = None model_pred\ = model.build (t_coord, t_type, t_natoms, t_box, t_mesh, t_fparam, suffix = "se_t", reuse = False) energy = model_pred['energy'] force = model_pred['force'] virial = model_pred['virial'] atom_ener = model_pred['atom_ener'] feed_dict_test = { t_prop_c: test_data['prop_c'], t_energy: test_data['energy'][:numb_test], t_force: np.reshape(test_data['force'][:numb_test, :], [-1]), t_virial: np.reshape(test_data['virial'][:numb_test, :], [-1]), t_atom_ener: np.reshape(test_data['atom_ener'][:numb_test, :], [-1]), t_coord: np.reshape(test_data['coord'][:numb_test, :], [-1]), t_box: test_data['box'][:numb_test, :], t_type: np.reshape(test_data['type'][:numb_test, :], [-1]), t_natoms: test_data['natoms_vec'], t_mesh: test_data['default_mesh'], is_training: False } sess = self.test_session().__enter__() sess.run(tf.global_variables_initializer()) [e, f, v] = sess.run([energy, force, virial], feed_dict=feed_dict_test) e = e.reshape([-1]) f = f.reshape([-1]) v = v.reshape([-1]) np.savetxt('e.out', e.reshape([1, -1])) np.savetxt('f.out', f.reshape([1, -1]), delimiter=',') np.savetxt('v.out', v.reshape([1, -1]), delimiter=',') refe = [4.8436558582194039e+01] reff = [ 5.2896335066946598e+00, 5.5778402259211131e+00, 2.6839994229557251e-01, 5.3528786387686784e+00, 5.2477755362164968e+00, -4.0486366542657343e-01, -5.1297084055340498e+00, 3.4607112287117253e-01, -5.1800783428369482e-02, 1.5557068351407846e-01, -5.9071343228741506e+00, -2.2012359669589748e-01, -5.9156735320857488e+00, 8.8397615509389127e-02, 3.6701215949753935e-01, 2.4729910864238122e-01, -5.3529501776440211e+00, 4.1375943757728552e-02 ] refv = [ -1.3159448660141607e+01, 4.6952048725161544e-01, 3.5482003698976106e-01, 4.6952048725161577e-01, -1.2178990983673918e+01, -1.6867277410496895e-01, 3.5482003698976106e-01, -1.6867277410496900e-01, -3.3986741457321945e-02 ] refe = np.reshape(refe, [-1]) reff = np.reshape(reff, [-1]) refv = np.reshape(refv, [-1]) places = 6 np.testing.assert_almost_equal(e, refe, places) np.testing.assert_almost_equal(f, reff, places) np.testing.assert_almost_equal(v, refv, places)
def test_model(self): jfile = 'water_se_r.json' jdata = j_loader(jfile) systems = j_must_have(jdata, 'systems') set_pfx = j_must_have(jdata, 'set_prefix') batch_size = j_must_have(jdata, 'batch_size') test_size = j_must_have(jdata, 'numb_test') batch_size = 1 test_size = 1 stop_batch = j_must_have(jdata, 'stop_batch') rcut = j_must_have(jdata['model']['descriptor'], 'rcut') data = DataSystem(systems, set_pfx, batch_size, test_size, rcut, run_opt=None) test_data = data.get_test() numb_test = 1 jdata['model']['descriptor'].pop('type', None) descrpt = DescrptSeR(**jdata['model']['descriptor'], uniform_seed=True) jdata['model']['fitting_net']['descrpt'] = descrpt fitting = EnerFitting(**jdata['model']['fitting_net'], uniform_seed=True) # fitting = EnerFitting(jdata['model']['fitting_net'], descrpt) model = EnerModel(descrpt, fitting) # model._compute_dstats([test_data['coord']], [test_data['box']], [test_data['type']], [test_data['natoms_vec']], [test_data['default_mesh']]) input_data = { 'coord': [test_data['coord']], 'box': [test_data['box']], 'type': [test_data['type']], 'natoms_vec': [test_data['natoms_vec']], 'default_mesh': [test_data['default_mesh']] } model._compute_input_stat(input_data) model.descrpt.bias_atom_e = data.compute_energy_shift() t_prop_c = tf.placeholder(tf.float32, [5], name='t_prop_c') t_energy = tf.placeholder(GLOBAL_ENER_FLOAT_PRECISION, [None], name='t_energy') t_force = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None], name='t_force') t_virial = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None], name='t_virial') t_atom_ener = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None], name='t_atom_ener') t_coord = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None], name='i_coord') t_type = tf.placeholder(tf.int32, [None], name='i_type') t_natoms = tf.placeholder(tf.int32, [model.ntypes + 2], name='i_natoms') t_box = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None, 9], name='i_box') t_mesh = tf.placeholder(tf.int32, [None], name='i_mesh') is_training = tf.placeholder(tf.bool) t_fparam = None model_pred\ = model.build (t_coord, t_type, t_natoms, t_box, t_mesh, t_fparam, suffix = "se_r", reuse = False) energy = model_pred['energy'] force = model_pred['force'] virial = model_pred['virial'] atom_ener = model_pred['atom_ener'] feed_dict_test = { t_prop_c: test_data['prop_c'], t_energy: test_data['energy'][:numb_test], t_force: np.reshape(test_data['force'][:numb_test, :], [-1]), t_virial: np.reshape(test_data['virial'][:numb_test, :], [-1]), t_atom_ener: np.reshape(test_data['atom_ener'][:numb_test, :], [-1]), t_coord: np.reshape(test_data['coord'][:numb_test, :], [-1]), t_box: test_data['box'][:numb_test, :], t_type: np.reshape(test_data['type'][:numb_test, :], [-1]), t_natoms: test_data['natoms_vec'], t_mesh: test_data['default_mesh'], is_training: False } sess = self.test_session().__enter__() sess.run(tf.global_variables_initializer()) [e, f, v] = sess.run([energy, force, virial], feed_dict=feed_dict_test) e = e.reshape([-1]) f = f.reshape([-1]) v = v.reshape([-1]) refe = [6.152085988309423925e+01] reff = [ -1.714443151616400110e-04, -1.315836609370952051e-04, -5.584120460897444674e-06, -7.197863450669731334e-05, -1.384609799994930676e-04, 8.856091902774708468e-06, 1.120578238869146797e-04, -7.428703645877488470e-05, 9.370560731488587317e-07, -1.048347129617610465e-04, 1.977876923815685781e-04, 7.522050342771599598e-06, 2.361772659657814205e-04, -5.774651813388292487e-05, -1.233143271630744828e-05, 2.257277740226381951e-08, 2.042905031476775584e-04, 6.003548585097267914e-07 ] refv = [ 1.035180911513190792e-03, -1.118982949050497126e-04, -2.383287813436022850e-05, -1.118982949050497126e-04, 4.362023915782403281e-04, 8.119543218224559240e-06, -2.383287813436022850e-05, 8.119543218224559240e-06, 1.201142938802945237e-06 ] refe = np.reshape(refe, [-1]) reff = np.reshape(reff, [-1]) refv = np.reshape(refv, [-1]) places = 6 for ii in range(e.size): self.assertAlmostEqual(e[ii], refe[ii], places=places) for ii in range(f.size): self.assertAlmostEqual(f[ii], reff[ii], places=places) for ii in range(v.size): self.assertAlmostEqual(v[ii], refv[ii], places=places)