def rca2_decompose(dataset, n): rca = GaussianRandomProjection(n_components=n) reduced_features = rca.fit_transform(dataset.all.features) training_size = dataset.training_size training = Data(reduced_features[:training_size, :], dataset.all.target[:training_size]) testing = Data(reduced_features[training_size:, :], dataset.all.target[training_size:]) return DataSet(training, testing)
def ica_decompose(dataset, n): ica = FastICA(n_components=n) reduced_features = ica.fit_transform(dataset.all.features) training_size = dataset.training_size training = Data(reduced_features[:training_size, :], dataset.all.target[:training_size]) testing = Data(reduced_features[training_size:, :], dataset.all.target[training_size:]) return DataSet(training, testing)
def lda_decompose(dataset, n): lda = LDA(n_components=n) reduced_features = lda.fit_transform(dataset.all.features, dataset.all.target) training_size = dataset.training_size training = Data(reduced_features[:training_size, :], dataset.all.target[:training_size]) testing = Data(reduced_features[training_size:, :], dataset.all.target[training_size:]) return DataSet(training, testing)
def test_pbc_small_box(self): data0 = Data() data1 = Data(box_scale=2) inter0 = Inter() inter1 = Inter() inter0.setUp(data0, pbc=True) inter1.setUp(data1, pbc=False) inter0.net_w_i = np.copy(np.ones(inter0.ndescrpt)) inter1.net_w_i = np.copy(np.ones(inter1.ndescrpt)) t_energy0, t_force0, t_virial0 \ = inter0.comp_ef (inter0.coord, inter0.box, inter0.type, inter0.tnatoms, name = "test_lf_pbc_sbox_true") t_energy1, t_force1, t_virial1 \ = inter1.comp_ef (inter1.coord, inter1.box, inter1.type, inter1.tnatoms, name = "test_lf_pbc_sbox_false") inter0.sess.run(tf.global_variables_initializer()) inter1.sess.run(tf.global_variables_initializer()) dcoord, dbox, dtype = data0.get_data() [e0, f0, v0] = inter0.sess.run( [t_energy0, t_force0, t_virial0], feed_dict={ inter0.coord: dcoord, inter0.box: dbox, inter0.type: dtype, inter0.tnatoms: inter0.natoms }) dcoord, dbox, dtype = data1.get_data() [e1, f1, v1] = inter1.sess.run( [t_energy1, t_force1, t_virial1], feed_dict={ inter1.coord: dcoord, inter1.box: dbox, inter1.type: dtype, inter1.tnatoms: inter1.natoms }) self.assertAlmostEqual(e0[0], e1[0]) for ii in range(f0[0].size): # print(ii) self.assertAlmostEqual(f0[0][ii], f1[0][ii]) for ii in range(v0[0].size): # print(ii) self.assertAlmostEqual(v0[0][ii], v1[0][ii])
def gen_data() : tmpdata = Data(rand_pert = 0.1, seed = 1) sys = dpdata.LabeledSystem() sys.data['coords'] = tmpdata.coord sys.data['atom_types'] = tmpdata.atype sys.data['cells'] = tmpdata.cell nframes = tmpdata.nframes natoms = tmpdata.natoms print(sys.data['coords']) sys.data['coords'] = sys.data['coords'].reshape([nframes,natoms,3]) sys.data['cells'] = sys.data['cells'].reshape([nframes,3,3]) sys.data['energies'] = np.zeros([nframes,1]) sys.data['forces'] = np.zeros([nframes,natoms,3]) sys.data['virials'] = [] sys.to_deepmd_npy('system', prec=np.float64) np.save('system/set.000/fparam.npy', tmpdata.fparam)
def test_pbc(self): data = Data() inter0 = Inter() inter1 = Inter() inter0.setUp(data, pbc=True, sess=self.test_session().__enter__()) inter1.setUp(data, pbc=False, sess=self.test_session().__enter__()) inter0.net_w_i = np.copy(np.ones(inter0.ndescrpt)) inter1.net_w_i = np.copy(np.ones(inter1.ndescrpt)) t_energy0, t_force0, t_virial0 \ = inter0.comp_ef (inter0.coord, inter0.box, inter0.type, inter0.tnatoms, name = "test_ser_pbc_true") t_energy1, t_force1, t_virial1 \ = inter1.comp_ef (inter1.coord, inter1.box, inter1.type, inter1.tnatoms, name = "test_ser_pbc_false") inter0.sess.run(tf.global_variables_initializer()) inter1.sess.run(tf.global_variables_initializer()) dcoord, dbox, dtype = data.get_data() [e0, f0, v0] = inter0.sess.run( [t_energy0, t_force0, t_virial0], feed_dict={ inter0.coord: dcoord, inter0.box: dbox, inter0.type: dtype, inter0.tnatoms: inter0.natoms }) [e1, f1, v1] = inter1.sess.run( [t_energy1, t_force1, t_virial1], feed_dict={ inter1.coord: dcoord, inter1.box: dbox, inter1.type: dtype, inter1.tnatoms: inter1.natoms }) self.assertAlmostEqual(e0[0], e1[0]) np.testing.assert_almost_equal(f0[0], f1[0]) np.testing.assert_almost_equal(v0[0], v1[0])
def setUp(self): self.places = 5 data = Data() Inter.setUp(self, data, sess=self.test_session().__enter__())
def __init__(self, *args, **kwargs): self.places = 5 data = Data() Inter.__init__(self, data) unittest.TestCase.__init__(self, *args, **kwargs) self.controller = object()
def setUp(self): self.places = 5 data = Data() Inter.setUp(self, data)
# # module.init_optimizer(kvstore="local", # optimizer="sgd", # optimizer_params={'learning_rate'=0.01}) # # while True: # for data_batch in data_train: # module.forward_backward(data_batch) # module.update() if __name__ == "__main__": logger = logging.getLogger() logger.setLevel(logging.DEBUG) # load data lld = Data('lld') lld.load_training_data() lld.load_test_data() # Permutation np.random.seed(1234) p = np.random.permutation(lld.feature_train.shape[0]) X = lld.feature_train[p] Y = lld.label_train[p] lld.feature_train = X lld.label_train = Y # Define Network Architectures nLayers = 3 nodesPerLayer = 1024 batchsize = 256 sym = DNN_def(nLayers, nodesPerLayer)
def _test_fv(self): dcm = DipoleChargeModifier( str(tests_path / os.path.join(modifier_datapath, "dipole.pb")), [-8], [6, 1], 1, 0.25) data = Data() coord, box, atype = data.get_data() atype = atype[0] ve, vf, vv = dcm.eval(coord, box, atype) hh = global_default_fv_hh hh = 1e-4 places = global_default_places places = 1 nframes = coord.shape[0] ndof = coord.shape[1] natoms = ndof // 3 vf = np.reshape(vf, [nframes, -1]) for ii in range(ndof): coordp = np.copy(coord) coordm = np.copy(coord) coordp[:, ii] += hh coordm[:, ii] -= hh ep, _, __ = dcm.eval(coordp, box, atype, eval_fv=False) em, _, __ = dcm.eval(coordm, box, atype, eval_fv=False) num_f = -(ep - em) / (2. * hh) np.testing.assert_almost_equal(vf[:, ii].ravel(), num_f.ravel(), places, err_msg='dof %d does not match' % (ii)) box3 = np.reshape(box, [nframes, 3, 3]) rbox3 = np.linalg.inv(box3) coord3 = np.reshape(coord, [nframes, natoms, 3]) rcoord3 = np.matmul(coord3, rbox3) num_deriv = np.zeros([nframes, 3, 3]) for ii in range(3): for jj in range(3): box3p = np.copy(box3) box3m = np.copy(box3) box3p[:, ii, jj] = box3[:, ii, jj] + hh box3m[:, ii, jj] = box3[:, ii, jj] - hh boxp = np.reshape(box3p, [-1, 9]) boxm = np.reshape(box3m, [-1, 9]) coord3p = np.matmul(rcoord3, box3p) coord3m = np.matmul(rcoord3, box3m) coordp = np.reshape(coord3p, [nframes, -1]) coordm = np.reshape(coord3m, [nframes, -1]) ep, _, __ = dcm.eval(coordp, boxp, atype, eval_fv=False) em, _, __ = dcm.eval(coordm, boxm, atype, eval_fv=False) num_deriv[:, ii, jj] = -(ep - em) / (2. * hh) # box3t = np.transpose(box3, [0,2,1]) # t_esti = np.matmul(num_deriv, box3t) num_deriv = np.transpose(num_deriv, [0, 2, 1]) t_esti = np.matmul(num_deriv, box3) # print(t_esti, '\n', vv.reshape([-1, 3, 3])) np.testing.assert_almost_equal(t_esti.ravel(), vv.ravel(), places, err_msg="virial component failed")