def periodic_0th_bfgs_step_test(): ########################################################################### # Making the list of images images = [ Atoms(symbols='PdOPd', pbc=np.array([True, False, False], dtype=bool), cell=np.array([[2., 0., 0.], [0., 2., 0.], [0., 0., 2.]]), positions=np.array([[0.5, 1., 0.5], [1., 0.5, 1.], [1.5, 1.5, 1.5]])), Atoms(symbols='PdO', pbc=np.array([True, True, False], dtype=bool), cell=np.array([[2., 0., 0.], [0., 2., 0.], [0., 0., 2.]]), positions=np.array([[0.5, 1., 0.5], [1., 0.5, 1.]])), Atoms(symbols='Cu', pbc=np.array([True, True, False], dtype=bool), cell=np.array([[1.8, 0., 0.], [0., 1.8, 0.], [0., 0., 1.8]]), positions=np.array([[0., 0., 0.]])) ] for image in images: image.set_calculator(EMT()) image.get_potential_energy(apply_constraint=False) image.get_forces(apply_constraint=False) ########################################################################### # Parameters Gs = { 'O': [{ 'type': 'G2', 'element': 'Pd', 'eta': 0.8 }, { 'type': 'G4', 'elements': ['O', 'Pd'], 'eta': 0.3, 'gamma': 0.6, 'zeta': 0.5 }], 'Pd': [{ 'type': 'G2', 'element': 'Pd', 'eta': 0.2 }, { 'type': 'G4', 'elements': ['Pd', 'Pd'], 'eta': 0.9, 'gamma': 0.75, 'zeta': 1.5 }], 'Cu': [{ 'type': 'G2', 'element': 'Cu', 'eta': 0.8 }, { 'type': 'G4', 'elements': ['Cu', 'Cu'], 'eta': 0.3, 'gamma': 0.6, 'zeta': 0.5 }] } hiddenlayers = {'O': (2), 'Pd': (2), 'Cu': (2)} weights = OrderedDict([ ('O', OrderedDict([(1, np.matrix([[-2.0, 6.0], [3.0, -3.0], [1.5, -0.9]])), (2, np.matrix([[5.5], [3.6], [1.4]]))])), ('Pd', OrderedDict([(1, np.matrix([[-1.0, 3.0], [2.0, 4.2], [1.0, -0.7]])), (2, np.matrix([[4.0], [0.5], [3.0]]))])), ('Cu', OrderedDict([(1, np.matrix([[0.0, 1.0], [-1.0, -2.0], [2.5, -1.9]])), (2, np.matrix([[0.5], [1.6], [-1.4]]))])) ]) scalings = OrderedDict([ ('O', OrderedDict([('intercept', -2.3), ('slope', 4.5)])), ('Pd', OrderedDict([('intercept', 1.6), ('slope', 2.5)])), ('Cu', OrderedDict([('intercept', -0.3), ('slope', -0.5)])) ]) ########################################################################### # Correct values correct_cost = 8004.292841472513 correct_energy_rmse = 43.736001940333836 correct_force_rmse = 137.4099476110887 correct_der_cost_fxn = [ 0.0814166874813534, 0.03231235582927526, 0.04388650395741291, 0.017417514465933048, 0.0284312765975806, 0.011283700608821421, 0.09416957265766414, -0.12322258890997816, 0.12679918754162384, 63.5396007548815, 0.016247700195771732, -86.62639558745185, -0.017777528287386473, 86.22415217678898, 0.017745913074805372, 104.58358033260711, -96.7328020983672, -99.09843648854351, -8.302880631971407, -1.2590007162073242, 8.3028773468822, 1.258759884181224, -8.302866610677315, -1.2563833805673688, 28.324298392677846, 28.09315509472324, -29.378744559315365, -11.247473567051799, 11.119951466671642, -87.08582317485761, -20.93948523898559, -125.73267675714658, -35.13852440758523 ] ########################################################################### # Testing pure-python and fortran versions of Gaussian-neural on different # number of processes for global_search in [None, 'SA']: for fortran in [False, True]: for extend_variables in [False, True]: for data_format in ['db', 'json']: for save_memory in [False]: for cores in range(1, 5): string = 'CuOPdbp/2/%s-%s-%s-%s-%s-%i' label = string % (global_search, fortran, extend_variables, data_format, save_memory, cores) if global_search is 'SA': gs = \ SimulatedAnnealing(temperature=10, steps=5) elif global_search is None: gs = None print label calc = Amp(descriptor=Gaussian(cutoff=4., Gs=Gs), regression=NeuralNetwork( hiddenlayers=hiddenlayers, weights=weights, scalings=scalings, activation='tanh', ), fortran=fortran, label=label) calc.train(images=images, energy_goal=10.**10., force_goal=10.**10, force_coefficient=0.04, cores=cores, data_format=data_format, save_memory=save_memory, global_search=gs, extend_variables=extend_variables) assert (abs(calc.cost_function - correct_cost) < 10.**(-7.)), \ 'The calculated value of cost function is \ wrong!' assert (abs(calc.energy_per_atom_rmse - correct_energy_rmse) < 10.**(-10.)), \ 'The calculated value of energy per atom RMSE \ is wrong!' assert (abs(calc.force_rmse - correct_force_rmse) < 10 ** (-8.)), \ 'The calculated value of force RMSE is wrong!' for _ in range(len(correct_der_cost_fxn)): assert(abs(calc.der_variables_cost_function[ _] - correct_der_cost_fxn[_]) < 10 ** (-8)), \ 'The calculated value of cost function \ derivative is wrong!' dblabel = label secondlabel = '_' + label calc = Amp(descriptor=Gaussian(cutoff=4., Gs=Gs), regression=NeuralNetwork( hiddenlayers=hiddenlayers, weights=weights, scalings=scalings, activation='tanh', ), fortran=fortran, label=secondlabel, dblabel=dblabel) calc.train(images=images, energy_goal=10.**10., force_goal=10.**10, force_coefficient=0.04, cores=cores, data_format=data_format, save_memory=save_memory, global_search=gs, extend_variables=extend_variables) assert (abs(calc.cost_function - correct_cost) < 10.**(-7.)), \ 'The calculated value of cost function is \ wrong!' assert (abs(calc.energy_per_atom_rmse - correct_energy_rmse) < 10.**(-10.)), \ 'The calculated value of energy per atom RMSE \ is wrong!' assert (abs(calc.force_rmse - correct_force_rmse) < 10 ** (-8.)), \ 'The calculated value of force RMSE is wrong!' for _ in range(len(correct_der_cost_fxn)): assert(abs(calc.der_variables_cost_function[ _] - correct_der_cost_fxn[_] < 10 ** (-8))), \ 'The calculated value of cost function \
def non_periodic_0th_bfgs_step_test(): pwd = os.getcwd() os.mkdir(os.path.join(pwd, 'CuOPdbp')) os.mkdir(os.path.join(pwd, '_CuOPdbp')) os.mkdir(os.path.join(pwd, 'CuOPdbp/0')) os.mkdir(os.path.join(pwd, '_CuOPdbp/0')) os.mkdir(os.path.join(pwd, 'CuOPdbp/1')) os.mkdir(os.path.join(pwd, '_CuOPdbp/1')) os.mkdir(os.path.join(pwd, 'CuOPdbp/2')) os.mkdir(os.path.join(pwd, '_CuOPdbp/2')) ########################################################################### # Making the list of periodic image images = [ Atoms(symbols='PdOPd2', pbc=np.array([False, False, False], dtype=bool), cell=np.array([[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]]), positions=np.array([[0., 0., 0.], [0., 2., 0.], [0., 0., 3.], [1., 0., 0.]])), Atoms(symbols='PdOPd2', pbc=np.array([False, False, False], dtype=bool), cell=np.array([[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]]), positions=np.array([[0., 1., 0.], [1., 2., 1.], [-1., 1., 2.], [1., 3., 2.]])), Atoms(symbols='PdO', pbc=np.array([False, False, False], dtype=bool), cell=np.array([[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]]), positions=np.array([[2., 1., -1.], [1., 2., 1.]])), Atoms(symbols='Pd2O', pbc=np.array([False, False, False], dtype=bool), cell=np.array([[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]]), positions=np.array([[-2., -1., -1.], [1., 2., 1.], [3., 4., 4.]])), Atoms(symbols='Cu', pbc=np.array([False, False, False], dtype=bool), cell=np.array([[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]]), positions=np.array([[0., 0., 0.]])) ] for image in images: image.set_calculator(EMT()) image.get_potential_energy(apply_constraint=False) image.get_forces(apply_constraint=False) ########################################################################### # Parameters Gs = { 'O': [{ 'type': 'G2', 'element': 'Pd', 'eta': 0.8 }, { 'type': 'G4', 'elements': ['Pd', 'Pd'], 'eta': 0.2, 'gamma': 0.3, 'zeta': 1 }, { 'type': 'G4', 'elements': ['O', 'Pd'], 'eta': 0.3, 'gamma': 0.6, 'zeta': 0.5 }], 'Pd': [{ 'type': 'G2', 'element': 'Pd', 'eta': 0.2 }, { 'type': 'G4', 'elements': ['Pd', 'Pd'], 'eta': 0.9, 'gamma': 0.75, 'zeta': 1.5 }, { 'type': 'G4', 'elements': ['O', 'Pd'], 'eta': 0.4, 'gamma': 0.3, 'zeta': 4 }], 'Cu': [{ 'type': 'G2', 'element': 'Cu', 'eta': 0.8 }, { 'type': 'G4', 'elements': ['Cu', 'O'], 'eta': 0.2, 'gamma': 0.3, 'zeta': 1 }, { 'type': 'G4', 'elements': ['Cu', 'Cu'], 'eta': 0.3, 'gamma': 0.6, 'zeta': 0.5 }] } hiddenlayers = {'O': (2), 'Pd': (2), 'Cu': (2)} weights = OrderedDict([ ('O', OrderedDict([(1, np.matrix([[-2.0, 6.0], [3.0, -3.0], [1.5, -0.9], [-2.5, -1.5]])), (2, np.matrix([[5.5], [3.6], [1.4]]))])), ('Pd', OrderedDict([(1, np.matrix([[-1.0, 3.0], [2.0, 4.2], [1.0, -0.7], [-3.0, 2.0]])), (2, np.matrix([[4.0], [0.5], [3.0]]))])), ('Cu', OrderedDict([(1, np.matrix([[0.0, 1.0], [-1.0, -2.0], [2.5, -1.9], [-3.5, 0.5]])), (2, np.matrix([[0.5], [1.6], [-1.4]]))])) ]) scalings = OrderedDict([ ('O', OrderedDict([('intercept', -2.3), ('slope', 4.5)])), ('Pd', OrderedDict([('intercept', 1.6), ('slope', 2.5)])), ('Cu', OrderedDict([('intercept', -0.3), ('slope', -0.5)])) ]) ########################################################################### # Correct values correct_cost = 7144.810783950215 correct_energy_rmse = 24.318837496017185 correct_force_rmse = 144.70282475062052 correct_der_cost_fxn = [ 0, 0, 0, 0, 0, 0, 0.01374139170953901, 0.36318423812749656, 0.028312691567496464, 0.6012336354445753, 0.9659002689921986, -1.2897770059416218, -0.5718960935176884, -2.6425667221503035, -1.1960399246712894, 0, 0, -2.7256379713943852, -0.9080181026559658, -0.7739948323247023, -0.2915789426043727, -2.05998290443513, -0.6156374289747903, -0.0060865174621348985, -0.8296785483640939, 0.0008092646748983969, 0.041613027034688874, 0.003426469079592851, -0.9578004568876517, -0.006281929608090211, -0.28835884773094056, -4.2457774110285245, -4.317412094174614, -8.02385959091948, -3.240512651984099, -27.289862194996896, -26.8177742762254, -82.45107056053345, -80.6816768350809 ] ########################################################################### # Testing pure-python and fortran versions of Gaussian-neural on different # number of processes for global_search in [None, 'SA']: for fortran in [False, True]: for extend_variables in [False, True]: for data_format in ['db', 'json']: for save_memory in [False]: for cores in range(1, 7): string = 'CuOPdbp/0/%s-%s-%s-%s-%s-%i' label = string % (global_search, fortran, extend_variables, data_format, save_memory, cores) if global_search is 'SA': gs = \ SimulatedAnnealing(temperature=10, steps=5) elif global_search is None: gs = None print label calc = Amp(descriptor=Gaussian(cutoff=6.5, Gs=Gs), regression=NeuralNetwork( hiddenlayers=hiddenlayers, weights=weights, scalings=scalings, activation='sigmoid', ), fortran=fortran, label=label) calc.train(images=images, energy_goal=10.**10., force_goal=10.**10., force_coefficient=0.04, cores=cores, data_format=data_format, save_memory=save_memory, global_search=gs, extend_variables=extend_variables) assert (abs(calc.cost_function - correct_cost) < 10.**(-5.)), \ 'The calculated value of cost function is \ wrong!' assert (abs(calc.energy_per_atom_rmse - correct_energy_rmse) < 10.**(-10.)), \ 'The calculated value of energy per atom RMSE \ is wrong!' assert (abs(calc.force_rmse - correct_force_rmse) < 10 ** (-7)), \ 'The calculated value of force RMSE is wrong!' for _ in range(len(correct_der_cost_fxn)): assert(abs(calc.der_variables_cost_function[ _] - correct_der_cost_fxn[_]) < 10 ** (-9)), \ 'The calculated value of cost function \ derivative is wrong!' dblabel = label secondlabel = '_' + label calc = Amp(descriptor=Gaussian(cutoff=6.5, Gs=Gs), regression=NeuralNetwork( hiddenlayers=hiddenlayers, weights=weights, scalings=scalings, activation='sigmoid', ), fortran=fortran, label=secondlabel, dblabel=dblabel) calc.train(images=images, energy_goal=10.**10., force_goal=10.**10., force_coefficient=0.04, cores=cores, data_format=data_format, save_memory=save_memory, global_search=gs, extend_variables=extend_variables) assert (abs(calc.cost_function - correct_cost) < 10.**(-5.)), \ 'The calculated value of cost function is \ wrong!' assert (abs(calc.energy_per_atom_rmse - correct_energy_rmse) < 10.**(-10.)), \ 'The calculated value of energy per atom RMSE \ is wrong!' assert (abs(calc.force_rmse - correct_force_rmse) < 10 ** (-7)), \ 'The calculated value of force RMSE is wrong!' for _ in range(len(correct_der_cost_fxn)): assert(abs(calc.der_variables_cost_function[ _] - correct_der_cost_fxn[_] < 10 ** (-9))), \ 'The calculated value of cost function \
def test(): pwd = os.getcwd() os.mkdir(os.path.join(pwd, 'CuOPdnone')) os.mkdir(os.path.join(pwd, '_CuOPdnone')) ########################################################################### # Parameters weights = OrderedDict([(1, np.array([[1., 2.5], [0., 1.5], [0., -1.5], [3., 9.], [1., -2.5], [2., 3.], [2., 2.5], [3., 0.], [-3.5, 1.], [5., 3.], [-2., 2.5], [-4., 4.], [0., 0.]])), (2, np.array([[1.], [2.], [0.]])), (3, np.array([[3.5], [0.]]))]) scalings = OrderedDict([('intercept', 3.), ('slope', 2.)]) images = generate_images() ########################################################################### # Testing pure-python and fortran versions of Gaussian-neural on different # number of processes for global_search in [None, 'SA']: for fortran in [False, True]: for extend_variables in [False, True]: for data_format in ['db', 'json']: for save_memory in [False]: for cores in range(1, 7): string = 'CuOPdnone/%s-%s-%s-%s-%s-%i' label = string % (global_search, fortran, extend_variables, data_format, save_memory, cores) if global_search is 'SA': gs = \ SimulatedAnnealing(temperature=10, steps=5) elif global_search is None: gs = None print label calc = Amp( descriptor=None, regression=NeuralNetwork( hiddenlayers=(2, 1), activation='tanh', weights=weights, scalings=scalings, ), fortran=fortran, label=label, ) calc.train(images=images, energy_goal=10.**10., force_goal=10.**10., force_coefficient=0.04, cores=cores, data_format=data_format, save_memory=save_memory, global_search=gs, extend_variables=extend_variables) # Check for consistency between the two models assert (abs(calc.cost_function - cost_function) < 10.**(-5.)), \ 'The calculated value of cost function is \ wrong!' assert (abs(calc.energy_per_atom_rmse - energy_rmse) < 10.**(-5.)), \ 'The calculated value of energy per atom RMSE \ is wrong!' assert (abs(calc.force_rmse - force_rmse) < 10 ** (-5)), \ 'The calculated value of force RMSE is wrong!' dblabel = label secondlabel = '_' + label calc = Amp(descriptor=None, regression=NeuralNetwork( hiddenlayers=(2, 1), activation='tanh', weights=weights, scalings=scalings, ), fortran=fortran, label=secondlabel, dblabel=dblabel) calc.train(images=images, energy_goal=10.**10., force_goal=10.**10., force_coefficient=0.04, cores=cores, data_format=data_format, save_memory=save_memory, global_search=gs, extend_variables=extend_variables) # Check for consistency between the two models assert (abs(calc.cost_function - cost_function) < 10.**(-5.)), \ 'The calculated value of cost function is \ wrong!' assert (abs(calc.energy_per_atom_rmse - energy_rmse) < 10.**(-5.)), \ 'The calculated value of energy per atom RMSE \ is wrong!' assert (abs(calc.force_rmse - force_rmse) < 10 ** (-5)), \ 'The calculated value of force RMSE is wrong!'
#!/usr/bin/env python from amp import Amp from amp.descriptor import Gaussian from amp.regression import NeuralNetwork from ase.db import connect from amp import SimulatedAnnealing db = connect('../../../database/master.db') images = [] for d in db.select('train_set=True'): atoms = db.get_atoms(d.id) del atoms.constraints images += [atoms] for n in [2, 3]: calc = Amp(label='./', dblabel='../../', descriptor=Gaussian(cutoff=6.5), regression=NeuralNetwork(hiddenlayers=(2, n))) calc.train(images=images, data_format='db', cores=4, energy_goal=1e-2, force_goal=1e-1, global_search=SimulatedAnnealing(temperature=70, steps=50), extend_variables=False)
def test(): images = make_training_images() for descriptor in [None, Gaussian()]: for global_search in [ None, SimulatedAnnealing(temperature=10, steps=5) ]: for data_format in ['json', 'db']: for save_memory in [ False, ]: for fortran in [False, True]: for cores in range(1, 4): print(descriptor, global_search, data_format, save_memory, fortran, cores) pwd = os.getcwd() testdir = 'read_write_test' os.mkdir(testdir) os.chdir(testdir) regression = NeuralNetwork(hiddenlayers=( 5, 5, )) calc = Amp( label='calc', descriptor=descriptor, regression=regression, fortran=fortran, ) # Should start with new variables calc.train( images, energy_goal=0.01, force_goal=10., global_search=global_search, extend_variables=True, data_format=data_format, save_memory=save_memory, cores=cores, ) # Test that we cannot overwrite. (Strange code # here because we *want* it to raise an # exception...) try: calc.train( images, energy_goal=0.01, force_goal=10., global_search=global_search, extend_variables=True, data_format=data_format, save_memory=save_memory, cores=cores, ) except IOError: pass else: raise RuntimeError( 'Code allowed to overwrite!') # Test that we can manually overwrite. # Should start with existing variables calc.train( images, energy_goal=0.01, force_goal=10., global_search=global_search, extend_variables=True, data_format=data_format, save_memory=save_memory, overwrite=True, cores=cores, ) label = 'testdir' if not os.path.exists(label): os.mkdir(label) # New directory calculator. calc = Amp( label='testdir/calc', descriptor=descriptor, regression=regression, fortran=fortran, ) # Should start with new variables calc.train( images, energy_goal=0.01, force_goal=10., global_search=global_search, extend_variables=True, data_format=data_format, save_memory=save_memory, cores=cores, ) # Open existing, save under new name. calc = Amp( load='calc', label='calc2', descriptor=descriptor, regression=regression, fortran=fortran, ) # Should start with existing variables calc.train( images, energy_goal=0.01, force_goal=10., global_search=global_search, extend_variables=True, data_format=data_format, save_memory=save_memory, cores=cores, ) label = 'calc_new' if not os.path.exists(label): os.mkdir(label) # Change label and re-train calc.set_label('calc_new/calc') # Should start with existing variables calc.train( images, energy_goal=0.01, force_goal=10., global_search=global_search, extend_variables=True, data_format=data_format, save_memory=save_memory, cores=cores, ) # Open existing without specifying new name. calc = Amp( load='calc', descriptor=descriptor, regression=regression, fortran=fortran, ) # Should start with existing variables calc.train( images, energy_goal=0.01, force_goal=10., global_search=global_search, extend_variables=True, data_format=data_format, save_memory=save_memory, cores=cores, ) os.chdir(pwd) shutil.rmtree(testdir, ignore_errors=True) del calc, regression
def test(): pwd = os.getcwd() os.mkdir(os.path.join(pwd, 'consistnone')) images = generate_images() count = 0 for global_search in [None, 'SA']: for fortran in [False, True]: for extend_variables in [False, True]: for data_format in ['db', 'json']: for save_memory in [False]: for cores in range(1, 7): string = 'consistnone/%s-%s-%s-%s-%s-%i' label = string % (global_search, fortran, extend_variables, data_format, save_memory, cores) if global_search is 'SA': global_search = \ SimulatedAnnealing(temperature=10, steps=5) calc = Amp(descriptor=None, regression=NeuralNetwork( hiddenlayers=(2, 1), activation='tanh', weights=weights, scalings=scalings, ), fortran=fortran, label=label) calc.train(images=images, energy_goal=10.**10., force_goal=10.**10., cores=cores, data_format=data_format, save_memory=save_memory, global_search=global_search, extend_variables=extend_variables) if count == 0: reference_cost_function = calc.cost_function reference_energy_rmse = \ calc.energy_per_atom_rmse reference_force_rmse = calc.force_rmse ref_cost_fxn_variable_derivatives = \ calc.der_variables_cost_function else: assert (abs(calc.cost_function - reference_cost_function) < 10.**(-10.)), \ '''Cost function value for %r fortran, %r data format, %r save_memory, and %i cores is not consistent with the value of python version on single core.''' % (fortran, data_format, save_memory, cores) assert (abs(calc.energy_per_atom_rmse - reference_energy_rmse) < 10.**(-10.)), \ '''Energy rmse value for %r fortran, %r data format, %r save_memory, and %i cores is not consistent with the value of python version on single core.''' % (fortran, data_format, save_memory, cores) assert (abs(calc.force_rmse - reference_force_rmse) < 10.**(-10.)), \ '''Force rmse value for %r fortran, %r data format, %r save_memory, and %i cores is not consistent with the value of python version on single core.''' % (fortran, data_format, save_memory, cores) for _ in range( len(ref_cost_fxn_variable_derivatives)): assert (calc.der_variables_cost_function[_] - ref_cost_fxn_variable_derivatives[_] < 10.**(-10.)) '''Derivative of the cost function for %r fortran, %r data format, %r save_memory, and %i cores is not consistent with the value of python version on single core. ''' % (fortran, data_format, save_memory, cores) count = count + 1