def test_sfs(): # test the BP symmetry functions against manual calculations # units in the original runner format is Bohr from helpers import get_trivial_runner_ds from pinn.networks import bpnn from pinn.io import sparse_batch bohr2ang = 0.5291772109 dataset = get_trivial_runner_ds().apply(sparse_batch(1)) sf_spec = [ {'type': 'G2', 'i': 1, 'j': 'ALL', 'eta': [0.01/(bohr2ang**2)], 'Rs': [0.5*bohr2ang]}, {'type': 'G3', 'i': 1, 'j': 8, 'k': 1, 'eta': [0.01/(bohr2ang**2)], 'lambd': [1.0], 'zeta': [1.0]}, {'type': 'G4', 'i': 1, 'j': 8, 'k': 1, 'eta': [0.01/(bohr2ang**2)], 'lambd': [1.0], 'zeta': [1.0]} ] nn_spec = {8: [35, 35], 1: [35, 35]} tensors = dataset.make_one_shot_iterator().get_next() tensors = bpnn(tensors, sf_spec=sf_spec, nn_spec=nn_spec, rc=12*bohr2ang, preprocess=True) with tf.Session() as sess: out = sess.run(tensors) print(out) g2_a, g3_a, g4_a = _manual_sfs() assert_almost_equal(out['fp_0'][0], g2_a) assert_almost_equal(out['fp_1'][0], g3_a) assert_almost_equal(out['fp_2'][0], g4_a)
def _dataset_fn(fname): dataset = load_tfrecord(fname) if batch_size is not None: dataset = dataset.apply(sparse_batch(batch_size)) if preprocess: if isinstance(params['network'], str): network_fn = getattr(networks, params['network']) else: network_fn = params['network'] pre_fn = lambda tensors: network_fn( tensors, preprocess=True, **params['network_params']) if scratch_dir is None: return dataset.map(pre_fn) else: _, tmp = tempfile.mkstemp(dir=scratch_dir) scratches.append(tmp) write_tfrecord(tmp + '.yml', dataset, pre_fn=pre_fn) return lambda: load_tfrecord(tmp + '.yml')
def test_write(): from pinn.io import load_tfrecord, write_tfrecord, sparse_batch ds = get_trivial_runner_ds().repeat(20) write_tfrecord('test.yml', ds) ds_tfr = load_tfrecord('test.yml') ds_batch = ds.apply(sparse_batch(20)) write_tfrecord('test_batch.yml', ds_batch) ds_batch_tfr = load_tfrecord('test_batch.yml') with tf.Session() as sess: label = sess.run(ds.make_one_shot_iterator().get_next()) out = sess.run(ds_tfr.make_one_shot_iterator().get_next()) for k in out.keys(): assert_almost_equal(label[k], out[k]) label = sess.run(ds_batch.make_one_shot_iterator().get_next()) out = sess.run(ds_batch_tfr.make_one_shot_iterator().get_next()) for k in out.keys(): assert_almost_equal(label[k], out[k])
datalist = (glob( r'C:\Users\phy\Desktop\hema\Materials_Design/Dataset/QM9/dsgdb9nsd/*.xyz')) filelist = [] r = [] r = random.sample(range(len(datalist)), 10) #Choosing 10 random samples of molecules for j in range(len(r)): filelist.append(datalist[ r[j]]) #appends the randomly selected molecular data in a list len(filelist) dataset = lambda: load_qm9(filelist, split=1, label_map={'d_data': 'mu'} ) #Selects only the d_data i.e., the Dipole Moment d = dataset().cache().repeat().apply(sparse_batch(10)) tensors = d.make_one_shot_iterator().get_next( ) #Iterators duplicate data from given data for creating datasets of 'similar' pattern with tf.Session() as sess: for i in range(10): sess.run(tensors) # "Warm up" the graph get_ipython().run_line_magic('timeit', 'sess.run(tensors)') # This speed indicates the IO limit of our current setting. # Now let's cache the dataset to the memory. d = dataset().cache().repeat().apply( sparse_batch(10) ) #These dataset iterators help create multiple datas with similar patterns for easier model building tensors = d.make_one_shot_iterator().get_next() with tf.Session() as sess:
if 'METRICS' not in k: continue else: print("| {:15s} | {:17f} |".format(k.split('/')[1], v)) print("---------------------------------------") print('\n') ############################################################################################################### ########Training and Evaluation################################################################################ print("Loaded tensorflow..\nStarting..") pre_fn = lambda tensors: pinet(tensors, preprocess=True) dataset = lambda: load_mimic(tpr, trr, ener, split=split) train = lambda: dataset()['train'].cache().repeat().shuffle(1000).\ apply(sparse_batch(batch_size['train'])).map(pre_fn, 8) valid = lambda: dataset()['vali'].cache().repeat().apply( sparse_batch(batch_size['vali'])).map(pre_fn, 8) valid = lambda: dataset()['test'].cache().repeat().apply( sparse_batch(batch_size['test'])).map(pre_fn, 8) params = { 'model_dir': model_dir, 'network': 'pinet', 'network_params': {}, 'model_params': { 'learning_rate': 1e-4 } } config = tf.contrib.learn.RunConfig(log_step_count_steps=10,\ save_summary_steps=10, keep_checkpoint_max=None, save_checkpoints_steps=20)
def test_potential_model(): """A simple example to test training and using a potential""" from ase import Atoms from ase.calculators.lj import LennardJones from pinn.io import load_numpy, sparse_batch from pinn.models import potential_model def three_body_sample(atoms, a, r): x = a * np.pi / 180 pos = [[0, 0, 0], [0, 2, 0], [0, r * np.cos(x), r * np.sin(x)]] atoms.set_positions(pos) return atoms tmp = tempfile.mkdtemp(prefix='pinn_test') atoms = Atoms('H3', calculator=LennardJones()) na, nr = 50, 50 arange = np.linspace(30, 180, na) rrange = np.linspace(1, 3, nr) # Truth agrid, rgrid = np.meshgrid(arange, rrange) egrid = np.zeros([na, nr]) for i in range(na): for j in range(nr): atoms = three_body_sample(atoms, arange[i], rrange[j]) egrid[i, j] = atoms.get_potential_energy() # Samples nsample = 50 asample, rsample = [], [] distsample = [] data = {'e_data': [], 'f_data': [], 'elems': [], 'coord': []} for i in range(nsample): a, r = np.random.choice(arange), np.random.choice(rrange) atoms = three_body_sample(atoms, a, r) dist = atoms.get_all_distances() dist = dist[np.nonzero(dist)] data['e_data'].append(atoms.get_potential_energy()) data['f_data'].append(atoms.get_forces()) data['coord'].append(atoms.get_positions()) data['elems'].append(atoms.numbers) asample.append(a) rsample.append(r) distsample.append(dist) data = {k: np.array(v) for k, v in data.items()} dataset = lambda: load_numpy(data) train = lambda: dataset()['train'].shuffle(100).repeat().apply( sparse_batch(100)) test = lambda: dataset()['test'].repeat().apply(sparse_batch(100)) params = { 'model_dir': tmp, 'network': 'pinet', 'network_params': { 'ii_nodes': [8, 8], 'pi_nodes': [8, 8], 'pp_nodes': [8, 8], 'en_nodes': [8, 8], 'rc': 3.0, 'atom_types': [1] }, 'model_params': { 'use_force': True } } model = potential_model(params) train_spec = tf.estimator.TrainSpec(input_fn=train, max_steps=200) eval_spec = tf.estimator.EvalSpec(input_fn=test, steps=10) tf.estimator.train_and_evaluate(model, train_spec, eval_spec) rmtree(tmp, ignore_errors=True)