Exemplo n.º 1
0
    def test_gold_dataset(self):
        dir_path = os.path.dirname(os.path.realpath(__file__))
        with open(os.path.join(dir_path, "Au_BP_testdata.pickle"), "rb") as fin:
            try:
                (Gs_train, types_train, E_train,
                Gs_test, types_test, E_test) = pickle.load(fin)
            except UnicodeDecodeError as e: # For Python3.6
                (Gs_train, types_train, E_train,
                Gs_test, types_test, E_test) = pickle.load(fin, encoding='latin1')
        pot = build_BPestimator(["Au"], [len(Gs_train[0][0])], layers = [[64,64]])

        def train_input_fn():
            batch_size = 50
            def gen():
                for i in range(0, len(Gs_train), batch_size):
                    [Au_atoms], [Au_indices] = calculate_bp_indices(
                        1, Gs_train[i:(i+batch_size)], types_train[i:(i+batch_size)])
                    yield({'Au_input':Au_atoms, 'Au_indices':Au_indices}, E_train[i:(i+batch_size)])
            train_data = tf.data.Dataset.from_generator(gen,
                ({'Au_input':tf.float32, 'Au_indices':tf.int32}, tf.float32),
                ({'Au_input':tf.TensorShape([None, len(Gs_train[0][0])]),
                'Au_indices':tf.TensorShape([None,1])}, tf.TensorShape([None,])))
            return train_data.shuffle(1000).repeat()

        [Au_atoms], [Au_indices] = calculate_bp_indices(1, Gs_test, types_test)

        def test_input_fn():
            test_data= tf.data.Dataset.from_tensor_slices(
                ({'Au_input': np.expand_dims(Au_atoms, axis=0).astype(np.float32),
                  'Au_indices': np.expand_dims(Au_indices, axis=0)},
                  np.array(E_test).reshape((1,-1)).astype(np.float32)))
            return test_data
        pot.train(train_input_fn, steps=100)
        print(pot.evaluate(test_input_fn))
Exemplo n.º 2
0
    def predict_val_der(self, x, *args):
        """
        function to allow to use the implemented NEB method
        :param x: prediction pattern, for derivative and function value the same shape = [N_samples, N_features]
        :return: function value prediction, derivative prediction
        """
        xyzs = x.reshape((-1, len(self.types), 3))
        Gs = []
        dGs = []
        for i in range(len(xyzs)):
            Gi, dGi = self.sfs.eval_with_derivatives(self.types, xyzs[i, :, :])
            Gs.append(Gi)
            dGs.append(dGi)
        ANN_inputs, indices, ANN_derivs = calculate_bp_indices(
            len(self.unique_types), Gs, [self.int_types] * len(Gs), dGs=dGs)
        eval_dict = {
            self.pot.target: np.zeros(len(Gs)),
            self.pot.target_forces: np.zeros((len(Gs), len(self.types), 3))
        }
        for i, t in enumerate(self.unique_types):
            eval_dict[self.pot.ANNs[t].input] = (
                ANN_inputs[i] - self.Gs_mean[t]) / self.Gs_std[t]
            eval_dict[self.pot.atom_indices[t]] = indices[i]
            eval_dict[self.pot.ANNs[t].derivatives_input] = np.einsum(
                'ijkl,j->ijkl', ANN_derivs[i], 1.0 / self.Gs_std[t])

        E = self.session.run(self.pot.E_predict, eval_dict)
        F = self.session.run(self.pot.F_predict, eval_dict)
        # Return energy and gradient (negative force)
        return E, -F.reshape(-1), None
Exemplo n.º 3
0
    def predict(self, x):
        xyzs = x.reshape((-1, len(self.types), 3))
        Gs = []
        for i in range(len(xyzs)):
            Gs.append(self.sfs.eval(self.types, xyzs[i, :, :]))
        ANN_inputs, indices, = calculate_bp_indices(len(self.unique_types), Gs,
                                                    [self.int_types] * len(Gs))
        eval_dict = {self.pot.target: np.zeros(len(Gs))}
        for i, t in enumerate(self.unique_types):
            eval_dict[self.pot.ANNs[t].input] = (
                ANN_inputs[i] - self.Gs_mean[t]) / self.Gs_std[t]
            eval_dict[self.pot.atom_indices[t]] = indices[i]

        return self.session.run(self.pot.E_predict, eval_dict)
Exemplo n.º 4
0
    def predict_derivative(self, x):
        xyzs = x.reshape((-1, len(self.types), 3))
        Gs = []
        dGs = []
        for i in range(len(xyzs)):
            Gi, dGi = self.sfs.eval_with_derivatives(self.types, xyzs[i, :, :])
            Gs.append(Gi)
            dGs.append(dGi)
        ANN_inputs, indices, ANN_derivs = calculate_bp_indices(
            len(self.unique_types), Gs, [self.int_types] * len(Gs), dGs=dGs)
        eval_dict = {
            self.pot.target: np.zeros(len(Gs)),
            self.pot.target_forces: np.zeros((len(Gs), len(self.types), 3))
        }
        for i, t in enumerate(self.unique_types):
            eval_dict[self.pot.ANNs[t].input] = (
                ANN_inputs[i] - self.Gs_mean[t]) / self.Gs_std[t]
            eval_dict[self.pot.atom_indices[t]] = indices[i]
            eval_dict[self.pot.ANNs[t].derivatives_input] = np.einsum(
                'ijkl,j->ijkl', ANN_derivs[i], 1.0 / self.Gs_std[t])

        # Return gradient (negative force)
        return -self.session.run(self.pot.F_predict, eval_dict).reshape(-1)
Exemplo n.º 5
0
    def fit(self, x_train, y_train, x_prime_train, y_prime_train):
        """
        Fitting a new model to a given training pattern
        :param x_train: function input pattern shape = [N_samples, N_features]
        :param y_train: function values shape = [N_samples, 1]
        :param x_prime_train: derivative input pattern shape = [N_samples, N_features]
        :param y_prime_train: derivative values shape = [N_samples, N_features]
        :return:
        """

        print('fit called with %d geometries. E_max = %f, E_min = %f' %
              (len(x_train), np.max(y_train), np.min(y_train)))
        # NN Model does not support different geometries for energies and forces
        np.testing.assert_array_equal(x_train, x_prime_train)

        xyzs = x_train.reshape((-1, len(self.types), 3))

        Gs = []
        dGs = []

        for i in range(len(xyzs)):
            Gi, dGi = self.sfs.eval_with_derivatives(self.types, xyzs[i, :, :])
            Gs.append(Gi)
            dGs.append(dGi)
        ANN_inputs, indices, ANN_derivs = calculate_bp_indices(
            len(self.unique_types), Gs, [self.int_types] * len(Gs), dGs=dGs)
        if self.normalize_input:
            for i, t in enumerate(self.unique_types):
                self.Gs_mean[t] = np.mean(ANN_inputs[i], axis=0)
                # Small offset for numerical stability
                self.Gs_std[t] = np.std(ANN_inputs[i], axis=0) + 1E-6
        train_dict = {
            self.pot.target:
            y_train,
            self.pot.target_forces:
            -y_prime_train.reshape((-1, len(self.types), 3)),
            self.pot.rmse_weights:
            np.ones(len(Gs))
        }
        for i, t in enumerate(self.unique_types):
            train_dict[self.pot.ANNs[t].input] = (
                ANN_inputs[i] - self.Gs_mean[t]) / self.Gs_std[t]
            train_dict[self.pot.atom_indices[t]] = indices[i]
            train_dict[self.pot.ANNs[t].derivatives_input] = np.einsum(
                'ijkl,j->ijkl', ANN_derivs[i], 1.0 / self.Gs_std[t])

        if self.reset_fit:
            self.session.run(tf.initializers.variables(self.pot.variables))
        regularizer = tf.contrib.layers.l2_regularizer(scale=1.0)
        reg_variables = tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)
        reg_term = tf.contrib.layers.apply_regularization(
            regularizer, reg_variables)
        optimizer = tf.contrib.opt.ScipyOptimizerInterface(
            self.C1 * self.pot.rmse + self.C2 * self.pot.rmse_forces +
            reg_term,
            method='L-BFGS-B')
        optimizer.minimize(self.session, train_dict)
        e_rmse, f_rmse = self.session.run(
            [self.pot.rmse, self.pot.rmse_forces], train_dict)
        print('fit finished with energy rmse '
              '%f and gradient rmse %f' % (e_rmse, f_rmse))
Exemplo n.º 6
0
 def gen():
     for i in range(0, len(Gs_train), batch_size):
         [Au_atoms], [Au_indices] = calculate_bp_indices(
             1, Gs_train[i:(i+batch_size)], types_train[i:(i+batch_size)])
         yield({'Au_input':Au_atoms, 'Au_indices':Au_indices}, E_train[i:(i+batch_size)])
Exemplo n.º 7
0
    def test_hf_derivatives(self):
        tf.reset_default_graph()
        dir_path = os.path.dirname(os.path.realpath(__file__))
        with open(os.path.join(dir_path, "HF_dataset.pickle"), "rb") as fin:
            try:
                (Gs_test, types_test, E_test, dGs_test,
                 F_test) = pickle.load(fin)
            except UnicodeDecodeError as e:  # For Python3.6
                (Gs_test, types_test, E_test, dGs_test,
                 F_test) = pickle.load(fin, encoding='latin1')
        pot = BPpotential(
            ["H", "F"],
            [len(Gs_test[0][0]), len(Gs_test[0][1])],
            build_forces=True)

        ([H_atoms, F_atoms], [H_indices, F_indices],
         [H_derivs, F_derivs]) = calculate_bp_indices(2,
                                                      Gs_test,
                                                      types_test,
                                                      dGs=dGs_test)
        test_data = tf.data.Dataset.from_tensor_slices(({
            'H_input':
            H_atoms[np.newaxis, ...].astype(np.float32),
            'H_indices':
            H_indices[np.newaxis, ...],
            'H_derivatives_input':
            H_derivs[np.newaxis, ...].astype(np.float32),
            'F_input':
            F_atoms[np.newaxis, ...].astype(np.float32),
            'F_indices':
            F_indices[np.newaxis, ...],
            'F_derivatives_input':
            F_derivs[np.newaxis, ...].astype(np.float32),
            'error_weights':
            np.expand_dims(
                1.0 / np.array(list(map(len, Gs_test)), dtype=np.float32)**2,
                axis=0)
        }, {
            'energy':
            np.array(E_test).reshape((1, -1)).astype(np.float32),
            'forces':
            np.array(F_test)[np.newaxis, ...].astype(np.float32)
        }))

        test_dict = {
            pot.ANNs['H'].input: H_atoms,
            pot.atom_indices['H']: H_indices,
            pot.ANNs['H'].derivatives_input: H_derivs,
            pot.ANNs['F'].input: F_atoms,
            pot.atom_indices['F']: F_indices,
            pot.ANNs['F'].derivatives_input: F_derivs,
            pot.target: E_test,
            pot.target_forces: F_test,
            pot.error_weights: 1.0 / np.array(list(map(len, Gs_test)))**2
        }

        init_op = pot.iterator.make_initializer(test_data)

        with tf.Session() as sess:
            # Not relying on tf.set_seed() as graph level seed depends on
            # the order the graph is build
            np.random.seed(1234)
            for v in pot.variables:
                sess.run(v.assign(np.random.randn(*v.shape)))

            E_control = np.array([
                5.702612, 5.56348, 5.4710846, 5.4215145, 5.4079914, 5.424957,
                5.4680867, 5.5339046, 5.619496, 5.7223215, 5.840103, 5.970681,
                6.1118464, 6.261231, 6.416313, 6.5745687, 6.7336607, 6.8916163,
                7.046926, 7.1985497, 7.345853, 7.488504, 7.6263633
            ])
            F_control = np.zeros((23, 2, 3))
            F_control[:, :, -1] = [[-3.2141814, 3.2141814],
                                   [-2.3172617, 2.3172617],
                                   [-1.3963969, 1.3963969],
                                   [-0.60993123, 0.60993123],
                                   [0.05062687, -0.05062687],
                                   [0.61372644, -0.61372644],
                                   [1.0999537, -1.0999537],
                                   [1.5231014, -1.5231014],
                                   [1.8921306, -1.8921306],
                                   [2.2133608, -2.2133608],
                                   [2.4907703, -2.4907703],
                                   [2.7250333, -2.7250333],
                                   [2.9136925, -2.9136925],
                                   [3.0531886, -3.0531886],
                                   [3.1416233, -3.1416233],
                                   [3.1807523, -3.1807523],
                                   [3.17631, -3.17631], [3.136878, -3.136878],
                                   [3.0720663, -3.0720663],
                                   [2.9908042, -2.9908042],
                                   [2.9002442, -2.9002442],
                                   [2.805337, -2.805337],
                                   [2.7089603, -2.7089603]]

            # Test feeding the inputs as usual
            np.testing.assert_allclose(sess.run(pot.E_predict, test_dict),
                                       E_control,
                                       rtol=1e-5)

            np.testing.assert_array_equal(sess.run(pot.num_atoms, test_dict),
                                          np.array([2] * 23))

            np.testing.assert_allclose(sess.run(pot.F_predict, test_dict),
                                       F_control,
                                       rtol=1e-4)

            # Test using the reinitializable iterator
            sess.run(init_op)
            np.testing.assert_allclose(sess.run(pot.E_predict),
                                       E_control,
                                       rtol=1e-5)

            sess.run(init_op)
            np.testing.assert_array_equal(sess.run(pot.num_atoms),
                                          np.array([2] * 23))

            sess.run(init_op)
            np.testing.assert_allclose(sess.run(pot.F_predict),
                                       F_control,
                                       rtol=1e-4)
Exemplo n.º 8
0
    def test_gold_dataset(self):
        tf.reset_default_graph()
        dir_path = os.path.dirname(os.path.realpath(__file__))
        with open(os.path.join(dir_path, "Au_BP_testdata.pickle"),
                  "rb") as fin:
            try:
                (Gs_train, types_train, E_train, Gs_test, types_test,
                 E_test) = pickle.load(fin)
            except UnicodeDecodeError as e:  # For Python3.6
                (Gs_train, types_train, E_train, Gs_test, types_test,
                 E_test) = pickle.load(fin, encoding='latin1')
        pot = BPpotential(["Au"], [len(Gs_train[0][0])], layers=[[64, 64]])

        [Au_atoms], [Au_indices] = calculate_bp_indices(1, Gs_test, types_test)
        test_data = tf.data.Dataset.from_tensor_slices(({
            'Au_input':
            np.expand_dims(Au_atoms, axis=0).astype(np.float32),
            'Au_indices':
            np.expand_dims(Au_indices, axis=0),
            'error_weights':
            np.expand_dims(
                1.0 / np.array(list(map(len, Gs_test)), dtype=np.float32)**2,
                axis=0)
        }, {
            'energy':
            np.array(E_test).reshape((1, -1)).astype(np.float32)
        }))

        test_dict = {
            pot.ANNs["Au"].input: Au_atoms,
            pot.atom_indices["Au"]: Au_indices,
            pot.target: E_test,
            pot.error_weights: 1.0 / np.array(list(map(len, Gs_test)))**2
        }

        init_op = pot.iterator.make_initializer(test_data)

        with tf.Session() as sess:
            # Not relying on tf.set_seed() as graph level seed depends on
            # the order the graph is build
            np.random.seed(1234)
            for v in pot.variables:
                sess.run(v.assign(np.random.randn(*v.shape)))

            # Test feeding the inputs as usual
            np.testing.assert_allclose(
                sess.run(pot.E_predict, test_dict),
                np.array([
                    -13.735861, -9.856386, -8.934874, -13.685179, -13.685591,
                    -12.313505, -12.989342, -13.678537, -12.663105, -13.094957,
                    -10.074066, -7.7194157, -13.338873, -8.050451, -7.3590875,
                    -11.71219, -10.556736, -17.370564, -13.613234, -13.5924,
                    -12.43917, -13.568087, -7.9591656, -12.175657, -13.432264,
                    -19.11342, -13.68409, -12.032116, -11.541302, -8.347027,
                    -7.5450783
                ]),
                rtol=1e-5)

            np.testing.assert_array_equal(sess.run(pot.num_atoms, test_dict),
                                          np.array([2] * 31))

            # Test using the reinitializable iterator
            sess.run(init_op)
            np.testing.assert_allclose(
                sess.run(pot.E_predict),
                np.array([
                    -13.735861, -9.856386, -8.934874, -13.685179, -13.685591,
                    -12.313505, -12.989342, -13.678537, -12.663105, -13.094957,
                    -10.074066, -7.7194157, -13.338873, -8.050451, -7.3590875,
                    -11.71219, -10.556736, -17.370564, -13.613234, -13.5924,
                    -12.43917, -13.568087, -7.9591656, -12.175657, -13.432264,
                    -19.11342, -13.68409, -12.032116, -11.541302, -8.347027,
                    -7.5450783
                ]),
                rtol=1e-5)

            sess.run(init_op)
            np.testing.assert_array_equal(sess.run(pot.num_atoms),
                                          np.array([2] * 31))