def test_log_marginal_likelihood_derivative(self): kernel = RBFKernel(length_scale=2.1) + RBFKernel(length_scale=.345) theta0 = np.exp(kernel.theta) calc = GPRCalculator(kernel=kernel, C1=1e8, C2=1e8, opt_restarts=0) atoms = molecule('C2H6') atoms.set_calculator(EMT()) calc.add_data(atoms) calc.fit() dt = 1e-5 L, dL = calc.log_marginal_likelihood() dL_num = np.zeros_like(dL) for i in range(len(theta0)): dti = np.zeros(len(theta0)) dti[i] = dt calc.kernel.theta = np.log(theta0 + dti) L_plus = calc.log_marginal_likelihood()[0] calc.kernel.theta = np.log(theta0 - dti) L_minus = calc.log_marginal_likelihood()[0] calc.kernel.theta = np.log(theta0) dL_num[i] = (L_plus - L_minus)/(2*dt) np.testing.assert_allclose(dL, dL_num)
def test_RBF_times_ConstantKernel(self): n = 3 m = 4 n_dim = 12 X = np.random.randn(n, n_dim) Y = np.random.randn(m, n_dim) factor = 10.0 prod_kernel = Product(RBFKernel(), ConstantKernel(constant=factor)) ref_kernel = RBFKernel() K_prod, dK_prod = prod_kernel( X, Y, dx=True, dy=True, eval_gradient=True) K_ref, dK_ref1 = ref_kernel(X, Y, dx=True, dy=True, eval_gradient=True) # Derivative with respect to the second hyperparameter: dK_ref2 = np.zeros((n*(1+n_dim), m*(1+n_dim), 1)) dK_ref2[:, :, 0] = K_ref K_ref *= factor dK_ref1 *= factor np.testing.assert_allclose(K_prod, K_ref) np.testing.assert_allclose(dK_prod[:, :, :-1], dK_ref1) np.testing.assert_allclose(dK_prod[:, :, -1:], dK_ref2)
def test_rbf_squared(self): X = np.random.randn(3, 4) Y = np.random.randn(3, 4) kernel1 = Exponentiation(RBFKernel(length_scale=2.0), exponent=4) kernel2 = RBFKernel(length_scale=1.0) K1, dK1 = kernel1(X, Y, dx=True, dy=True, eval_gradient=True) K2, dK2 = kernel2(X, Y, dx=True, dy=True, eval_gradient=True) np.testing.assert_allclose(K1, K2) np.testing.assert_allclose(2*dK1, dK2)
def test_comparison2product(self): X = np.random.randn(3, 4) Y = np.random.randn(3, 4) kernel1 = Exponentiation(RBFKernel(length_scale=1.23), exponent=2) kernel2 = Product(RBFKernel(length_scale=1.23), RBFKernel(length_scale=1.23)) K1 = kernel1(X, Y, dx=True, dy=True) K2 = kernel2(X, Y, dx=True, dy=True) np.testing.assert_allclose(K1, K2)
def test_versus_anisotropic_rbf(self): X = np.random.randn(8, 2) Y = np.random.randn(8, 2) kernel = Product(Masking(RBFKernel(length_scale=1.5), slice(0, 1)), Masking(RBFKernel(length_scale=0.5), slice(1, 2))) ref_kernel = RBFKernel(length_scale=np.array([1.5, 0.5])) K, dK = kernel(X, Y, dx=True, dy=True, eval_gradient=True) K_ref, dK_ref = ref_kernel(X, Y, dx=True, dy=True, eval_gradient=True) np.testing.assert_allclose(K, K_ref) np.testing.assert_allclose(dK, dK_ref)
def test__mul__(self): X = np.random.randn(3, 4) Y = np.random.randn(3, 4) factor = 10.0 kernel1 = RBFKernel() * factor kernel2 = RBFKernel() * ConstantKernel(constant=factor) np.testing.assert_equal(kernel1.theta, kernel2.theta) K1, dK1 = kernel1(X, Y, dx=True, dy=True, eval_gradient=True) K2, dK2 = kernel2(X, Y, dx=True, dy=True, eval_gradient=True) np.testing.assert_allclose(K1, K2) np.testing.assert_allclose(dK1, dK2)
def test_prediction_variance(self): direction = np.array([1., 2., 3.]) direction /= np.linalg.norm(direction) r_train = [0.7, 1.7] images_train = [] for ri in r_train: image = Atoms( ['C', 'O'], positions=np.array([-0.5*ri*direction, 0.5*ri*direction])) image.set_calculator(EMT()) images_train.append(image) r_test = np.linspace(0.5, 1.9, 101) images_test = [] for ri in r_test: image = Atoms( ['C', 'O'], positions=np.array([-0.5*ri*direction, 0.5*ri*direction])) image.set_calculator(EMT()) images_test.append(image) kernel = RBFKernel(constant=100., length_scale=.1) calc = GPRCalculator(kernel=kernel, C1=1E8, C2=1E8, opt_restarts=0) [calc.add_data(im) for im in images_train] calc.fit() prediction_var = [calc.predict_var(im)[0] for im in images_test] max_var = np.argmax(prediction_var) np.testing.assert_equal(r_test[max_var], 1.2)
def test_h2(self): mol = molecule('H2') mol.set_calculator(EMT()) kernel = Rescaling(RBFKernel()) ml_calc = GPRCalculator(kernel=kernel, opt_restarts=1, normalize_y='max+10') r_test = np.linspace(0.6, 2.0) atoms_test = [ Atoms(['H', 'H'], positions=np.array([[0, 0, 0.5 * ri], [0, 0, -0.5 * ri]])) for ri in r_test ] E_test = [EMT().get_potential_energy(atoms) for atoms in atoms_test] # import matplotlib.pyplot as plt def callback_after(ml_calc): plt.plot(r_test, E_test) plt.plot(r_test, [ml_calc.predict(atoms)[0] for atoms in atoms_test]) for atoms in ml_calc.atoms_train: xyzs = atoms.get_positions() r = xyzs[0, 2] - xyzs[1, 2] print(r) plt.plot(r, ml_calc.predict(atoms)[0], 'o') plt.show() opt = MLOptimizer(mol, ml_calc) #, callback_after_ml_opt=callback_after) opt.run()
def test_build_diag(self): direction = np.array([1., 2., 3.]) direction /= np.linalg.norm(direction) r_train = np.linspace(0.5, 7, 11) energies_train = [] images_train = [] for ri in r_train: image = Atoms(['C', 'O'], positions=np.array( [-0.5 * ri * direction, 0.5 * ri * direction])) image.set_calculator(EMT()) energies_train.append(image.get_potential_energy()) images_train.append(image) cut = 6.5 with SymmetryFunctionSet(["C", "O"], cutoff=cut) as sfs: sfs.add_Artrith_Kolpak_set() kernel = RBFKernel(constant=100.0, length_scale=1e-2) calc = GAPCalculator(descriptor_set=sfs, kernel=kernel, C1=1e8, C2=1e8, opt_restarts=0) [calc.add_data(im) for im in images_train] calc.fit() np.testing.assert_allclose( calc.build_kernel_diagonal((calc.Gs_norm, calc.dGs_norm)), np.diag(calc.build_kernel_matrix()))
def test_comparison2product(self): n = 3 m = 4 n_dim = 12 X = np.random.randn(n, n_dim) Y = np.random.randn(m, n_dim) factor = 10.0 kernel1 = Rescaling(RBFKernel(), factor) kernel2 = Product(ConstantKernel(constant=factor), RBFKernel()) K1, dK1 = kernel1(X, Y, dx=True, dy=True, eval_gradient=True) K2, dK2 = kernel2(X, Y, dx=True, dy=True, eval_gradient=True) np.testing.assert_allclose(K1, K2) np.testing.assert_allclose(dK1, dK2)
def test_butadiene(self): mol = molecule('butadiene') mol.set_calculator(EMT()) kernel = Rescaling(RBFKernel()) ml_calc = GPRCalculator(kernel=kernel, opt_restarts=1, normalize_y='max+10') opt = MLOptimizer(mol, ml_calc) opt.run()
def test_co_kernel_derivative(self): direction = np.array([1., 2., 3.]) direction /= np.linalg.norm(direction) atoms = Atoms(['C', 'O'], positions=np.array([-0.5 * direction, 0.5 * direction])) atoms.set_calculator(EMT()) def to_radius(x): xyzs = x.get_positions() r = np.sqrt(np.sum((xyzs[1, :] - xyzs[0, :])**2)) dr = np.zeros((1, 6)) dr[0, 0] = (xyzs[0, 0] - xyzs[1, 0]) / r dr[0, 1] = (xyzs[0, 1] - xyzs[1, 1]) / r dr[0, 2] = (xyzs[0, 2] - xyzs[1, 2]) / r dr[0, 3] = (xyzs[1, 0] - xyzs[0, 0]) / r dr[0, 4] = (xyzs[1, 1] - xyzs[0, 1]) / r dr[0, 5] = (xyzs[1, 2] - xyzs[0, 2]) / r return [r], dr kernel = RBFKernel(constant=100.0, length_scale=0.23) calc = NCGPRCalculator(input_transform=to_radius, kernel=kernel, C1=1e8, C2=1e8, opt_restarts=0) calc.add_data(atoms) K = calc.build_kernel_matrix() K_num = np.zeros_like(K) # kernel value is not tested: K_num[0, 0] = K[0, 0] x0 = atoms.get_positions() dx = 1e-4 def K_fun(x, y): a = np.array([to_radius(Atoms(['C', 'O'], positions=x))[0]]) b = np.array([to_radius(Atoms(['C', 'O'], positions=y))[0]]) return calc.kernel(a, b, dx=True, dy=True)[0, 0] for i in range(6): dxi = np.zeros(6) dxi[i] = dx dxi = dxi.reshape((2, 3)) # Test first derivative K_num[1 + i, 0] = self.num_dx_forth_order(K_fun, x0, x0, dxi) for j in range(6): dxj = np.zeros(6) dxj[j] = dx dxj = dxj.reshape((2, 3)) K_num[1 + i, 1 + j] = self.num_dxdy_forth_order( K_fun, x0, x0, dxi, dxj) # Test symmetry of derivatives K_num[0, 1 + i] = self.num_dy_forth_order(K_fun, x0, x0, dxi) np.testing.assert_allclose(K, K_num, atol=1E-5)
class RescalingTest(KernelTest.KernelTest): kernel = Rescaling(RBFKernel(), 10.0) def test_comparison2product(self): n = 3 m = 4 n_dim = 12 X = np.random.randn(n, n_dim) Y = np.random.randn(m, n_dim) factor = 10.0 kernel1 = Rescaling(RBFKernel(), factor) kernel2 = Product(ConstantKernel(constant=factor), RBFKernel()) K1, dK1 = kernel1(X, Y, dx=True, dy=True, eval_gradient=True) K2, dK2 = kernel2(X, Y, dx=True, dy=True, eval_gradient=True) np.testing.assert_allclose(K1, K2) np.testing.assert_allclose(dK1, dK2) def test__rmul__(self): X = np.random.randn(3, 4) Y = np.random.randn(3, 4) factor = 10.0 kernel1 = factor * RBFKernel() kernel2 = ConstantKernel(constant=factor) * RBFKernel() np.testing.assert_equal(kernel1.theta, kernel2.theta) K1, dK1 = kernel1(X, Y, dx=True, dy=True, eval_gradient=True) K2, dK2 = kernel2(X, Y, dx=True, dy=True, eval_gradient=True) np.testing.assert_allclose(K1, K2) np.testing.assert_allclose(dK1, dK2) def test__mul__(self): X = np.random.randn(3, 4) Y = np.random.randn(3, 4) factor = 10.0 kernel1 = RBFKernel() * factor kernel2 = RBFKernel() * ConstantKernel(constant=factor) np.testing.assert_equal(kernel1.theta, kernel2.theta) K1, dK1 = kernel1(X, Y, dx=True, dy=True, eval_gradient=True) K2, dK2 = kernel2(X, Y, dx=True, dy=True, eval_gradient=True) np.testing.assert_allclose(K1, K2) np.testing.assert_allclose(dK1, dK2)
def test_RBF_plus_ConstantKernel(self): n = 3 m = 4 n_dim = 12 X = np.random.randn(n, n_dim) Y = np.random.randn(m, n_dim) constant = 10.0 sum_kernel = Sum(RBFKernel(), ConstantKernel(constant=constant)) ref_kernel = RBFKernel() K_sum, dK_sum = sum_kernel(X, Y, dx=True, dy=True, eval_gradient=True) K_ref, dK_ref1 = ref_kernel(X, Y, dx=True, dy=True, eval_gradient=True) K_ref[:n, :m] += constant # Derivative with respect to the second hyperparameter: dK_ref2 = np.zeros((n*(1+n_dim), m*(1+n_dim), 1)) dK_ref2[:n, :m, 0] = 1.0 np.testing.assert_allclose(K_sum, K_ref) np.testing.assert_allclose(dK_sum[:, :, :-1], dK_ref1) np.testing.assert_allclose(dK_sum[:, :, -1:], dK_ref2)
def test_equivalence_to_GPR(self): def to_cartesian(atoms): xyzs = atoms.get_positions().flatten() return xyzs, np.eye(len(xyzs)) gpr_calc = GPRCalculator(kernel=RBFKernel(constant=100.0, length_scale=0.456), C1=1e8, C2=1e8, opt_restarts=1) ncgpr_calc = NCGPRCalculator(kernel=RBFKernel(constant=100.0, length_scale=0.456), input_transform=to_cartesian, C1=1e8, C2=1e8, opt_restarts=1) atoms = molecule('cyclobutane') atoms.set_calculator(EMT()) xyzs = atoms.get_positions() gpr_calc.add_data(atoms) ncgpr_calc.add_data(atoms) for i in range(8): a = atoms.copy() a.set_calculator(EMT()) a.set_positions(xyzs + 0.5 * np.random.randn(*xyzs.shape)) gpr_calc.add_data(a) ncgpr_calc.add_data(a) np.testing.assert_allclose(ncgpr_calc.build_kernel_matrix(), gpr_calc.build_kernel_matrix()) gpr_calc.fit() ncgpr_calc.fit() np.testing.assert_allclose(ncgpr_calc.kernel.theta, gpr_calc.kernel.theta) np.testing.assert_allclose(ncgpr_calc.alpha, gpr_calc.alpha)
class RBFKernelTest(KernelTest.KernelTest): kernel = RBFKernel(constant=23.4, factor=1.234, length_scale=1.321) def test_symmetry(self): atomsX = np.random.randn(1, 12) atomsY = np.random.randn(1, 12) constant = 23.4 factor = 1.234 length_scale = 1.321 kernel = RBFKernel(constant=constant, factor=factor, length_scale=length_scale) K1 = kernel(atomsX, atomsY, dx=True, dy=True) K2 = kernel(atomsY, atomsX, dx=True, dy=True) N = len(atomsX) # K and H blocks are symmmetric np.testing.assert_allclose(K1[:N, :N], K2[:N, :N]) np.testing.assert_allclose(K1[N:, N:], K2[N:, N:]) # J and J^T are antisymmetric np.testing.assert_allclose(K1[:N, N:], -K2[:N, N:]) np.testing.assert_allclose(K1[N:, :N], -K2[N:, :N]) def test_equivalence_to_RBFKernel_with_factor(self): atoms = np.random.randn(10, 9) constant = 23.4 factor = 1.234 length_scale = 0.4321 kernel_with = RBFKernel_with_factor(constant=constant, factor=factor, length_scale=length_scale) kernel_without = RBFKernel(constant=constant, factor=factor, length_scale=length_scale) K_with, dK_with = kernel_with(atoms, atoms, dx=True, dy=True, eval_gradient=True) K_without, dK_without = kernel_without(atoms, atoms, dx=True, dy=True, eval_gradient=True) np.testing.assert_allclose(K_with, K_without) np.testing.assert_allclose(dK_with[:, :, 1:], dK_without)
def test_symmetry(self): atomsX = np.random.randn(1, 12) atomsY = np.random.randn(1, 12) constant = 23.4 factor = 1.234 length_scale = 1.321 kernel = RBFKernel(constant=constant, factor=factor, length_scale=length_scale) K1 = kernel(atomsX, atomsY, dx=True, dy=True) K2 = kernel(atomsY, atomsX, dx=True, dy=True) N = len(atomsX) # K and H blocks are symmmetric np.testing.assert_allclose(K1[:N, :N], K2[:N, :N]) np.testing.assert_allclose(K1[N:, N:], K2[N:, N:]) # J and J^T are antisymmetric np.testing.assert_allclose(K1[:N, N:], -K2[:N, N:]) np.testing.assert_allclose(K1[N:, :N], -K2[N:, :N])
class ExponentiationTest(KernelTest.KernelTest): kernel = Exponentiation(RBFKernel(), 2.0) def test_rbf_squared(self): X = np.random.randn(3, 4) Y = np.random.randn(3, 4) kernel1 = Exponentiation(RBFKernel(length_scale=2.0), exponent=4) kernel2 = RBFKernel(length_scale=1.0) K1, dK1 = kernel1(X, Y, dx=True, dy=True, eval_gradient=True) K2, dK2 = kernel2(X, Y, dx=True, dy=True, eval_gradient=True) np.testing.assert_allclose(K1, K2) np.testing.assert_allclose(2*dK1, dK2) def test_dot_product(self): X = np.random.randn(3, 4) Y = np.random.randn(3, 4) kernel1 = Exponentiation(DotProductKernel(exponent=2), exponent=2) kernel2 = DotProductKernel(exponent=4) K1 = kernel1(X, Y, dx=True, dy=True) K2 = kernel2(X, Y, dx=True, dy=True) np.testing.assert_allclose(K1, K2) def test_comparison2product(self): X = np.random.randn(3, 4) Y = np.random.randn(3, 4) kernel1 = Exponentiation(RBFKernel(length_scale=1.23), exponent=2) kernel2 = Product(RBFKernel(length_scale=1.23), RBFKernel(length_scale=1.23)) K1 = kernel1(X, Y, dx=True, dy=True) K2 = kernel2(X, Y, dx=True, dy=True) np.testing.assert_allclose(K1, K2)
def test_co_potential_curve(self): direction = np.array([1., 2., 3.]) direction /= np.linalg.norm(direction) r_train = np.linspace(0.5, 7, 11) energies_train = [] images_train = [] for ri in r_train: image = Atoms( ['C', 'O'], positions=np.array([-0.5*ri*direction, 0.5*ri*direction])) image.set_calculator(EMT()) energies_train.append(image.get_potential_energy()) images_train.append(image) kernel = RBFKernel(constant=100.0, length_scale=1e-1) calc = GPRCalculator(kernel=kernel, C1=1e8, C2=1e8, opt_restarts=0) [calc.add_data(im) for im in images_train] calc.fit() np.testing.assert_allclose( energies_train, [calc.predict(im)[0] for im in images_train])
def test_build_diag(self): direction = np.array([1., 2., 3.]) direction /= np.linalg.norm(direction) r_train = np.linspace(0.5, 7, 11) energies_train = [] images_train = [] for ri in r_train: image = Atoms(['C', 'O'], positions=np.array( [-0.5 * ri * direction, 0.5 * ri * direction])) image.set_calculator(EMT()) energies_train.append(image.get_potential_energy()) images_train.append(image) def to_radius(x): xyzs = x.get_positions() r = np.sqrt(np.sum((xyzs[1, :] - xyzs[0, :])**2)) dr = np.zeros((1, 6)) dr[0, 0] = (xyzs[0, 0] - xyzs[1, 0]) / r dr[0, 1] = (xyzs[0, 1] - xyzs[1, 1]) / r dr[0, 2] = (xyzs[0, 2] - xyzs[1, 2]) / r dr[0, 3] = (xyzs[1, 0] - xyzs[0, 0]) / r dr[0, 4] = (xyzs[1, 1] - xyzs[0, 1]) / r dr[0, 5] = (xyzs[1, 2] - xyzs[0, 2]) / r return [r], dr kernel = RBFKernel(constant=100.0, length_scale=1e-1) calc = NCGPRCalculator(input_transform=to_radius, kernel=kernel, C1=1e8, C2=1e8, opt_restarts=0) [calc.add_data(im) for im in images_train] calc.fit() np.testing.assert_allclose( calc.build_kernel_diagonal((calc.q_train, calc.dq_train)), np.diag(calc.build_kernel_matrix()))
def test_equivalence_to_RBFKernel_with_factor(self): atoms = np.random.randn(10, 9) constant = 23.4 factor = 1.234 length_scale = 0.4321 kernel_with = RBFKernel_with_factor(constant=constant, factor=factor, length_scale=length_scale) kernel_without = RBFKernel(constant=constant, factor=factor, length_scale=length_scale) K_with, dK_with = kernel_with(atoms, atoms, dx=True, dy=True, eval_gradient=True) K_without, dK_without = kernel_without(atoms, atoms, dx=True, dy=True, eval_gradient=True) np.testing.assert_allclose(K_with, K_without) np.testing.assert_allclose(dK_with[:, :, 1:], dK_without)
class RBFtimesRBFTest(KernelTest.KernelTest): kernel = Product(RBFKernel(length_scale=0.8), RBFKernel(length_scale=1.2))
def test_ethane_primitives_kernel_derivative(self): atoms = molecule('C2H6') atoms.set_calculator(EMT()) symbols = atoms.get_chemical_symbols() # Add gaussian noise because of numerical problem for # the 180 degree angle images = [] np.random.seed(123) for _ in range(4): image = atoms.copy() image.set_positions(image.get_positions() + 0.05 * np.random.randn(len(atoms), 3)) image.set_calculator(EMT()) images.append(image) np.random.seed() atomsX = images[:-1] atomsY = images[-1:] n = len(atomsX) m = len(atomsY) # Ethane bonds: bonds = [(0, 1), (0, 2), (0, 3), (0, 4), (1, 5), (1, 6), (1, 7)] transform, _, _ = to_primitives_factory(bonds) kernel = RBFKernel(constant=0.0, length_scale=3.21) calc = NCGPRCalculator(input_transform=transform, kernel=kernel, C1=1e8, C2=1e8, opt_restarts=0) [calc.add_data(a) for a in atomsX] qY, dqY = transform(atomsY[0]) K = calc.build_kernel_matrix(X_star=(qY, dqY)) K_num = np.zeros_like(K) # kernel value is not tested: K_num[:n, :m] = K[:n, :m] dx = 1e-4 calc.atoms_train = [atoms] def K_fun(x, y): qx, dqx = transform(Atoms(symbols, positions=x)) calc.q_train, calc.dq_train = [qx], [dqx] return calc.build_kernel_matrix( X_star=transform(Atoms(symbols, positions=y)))[0, 0] def K_dY(x, y): qx, dqx = transform(Atoms(symbols, positions=x)) calc.q_train, calc.dq_train = [qx], [dqx] return calc.build_kernel_matrix( X_star=transform(Atoms(symbols, positions=y)))[0, 1:] for i in range(n): x0 = atomsX[i].get_positions() for j in range(m): y0 = atomsY[j].get_positions() for k in range(len(atoms) * 3): dxk = np.zeros((len(atoms), 3)) dxk.flat[k] = dx # Test first derivative K_num[n + i * calc.n_dim + k, j] = self.num_dx_forth_order(K_fun, x0, y0, dxk) # Approximate second derivative as numerical derivative of # analytical first derivative K_num[n + i * calc.n_dim + k, m:] = self.num_dx_forth_order(K_dY, x0, y0, dxk) # Test symmetry of derivatives K_num[i, m + j * calc.n_dim + k] = self.num_dy_forth_order( K_fun, x0, y0, dxk) np.testing.assert_allclose(K, K_num, atol=1E-5)
class RBFKernelAnisoTest(KernelTest.KernelTest): kernel = RBFKernel(constant=23.4, factor=1.234, length_scale=np.array([1.321] * 12))
class RBFplusRBFTest(KernelTest.KernelTest): kernel = Sum(RBFKernel(length_scale=0.8), RBFKernel(length_scale=1.2))
from ase.io import read, write from ase.constraints import FixAtoms from ase.calculators.emt import EMT from ase.neb import NEB from mlpot.mlneb import run_mla_neb from mlpot.calculators.gprcalculator import GPRCalculator from mlpot.kernels import RBFKernel initial = read('initial.traj') final = read('final.traj') constraint = FixAtoms(mask=[atom.tag > 0 for atom in initial]) images = [initial] for i in range(5): image = initial.copy() image.set_calculator(EMT()) image.set_constraint(constraint) images.append(image) images.append(final) neb = NEB(images) neb.interpolate() kernel = RBFKernel(constant=100.0, length_scale=1.0) ml_calc = GPRCalculator(kernel=kernel, C1=1E8, C2=1E8, opt_restarts=1) run_mla_neb(neb, ml_calc) write('minimum_energy_path.traj', images)