Exemplo n.º 1
0
    def test_gradient_descent():
        """Test gradient_descent function"""

        X = np.array([[0, 1, 0, 1], [0, 0, 0, 0], [1, 1, 1, 1], [1, 1, 1, 1],
                      [0, 0, 1, 1], [1, 0, 0, 0]])

        y = np.reshape(np.array([1, 1, 0, 0, 1, 1]), [6, 1])

        nodes = [4, 2, 1]

        fitness = NetworkWeights(X,
                                 y,
                                 nodes,
                                 activation=identity,
                                 bias=False,
                                 is_classifier=False,
                                 learning_rate=0.1)

        problem = ContinuousOpt(10,
                                fitness,
                                maximize=False,
                                min_val=-1,
                                max_val=1,
                                step=0.1)

        test_weights = np.array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1])
        test_fitness = -1 * problem.eval_fitness(test_weights)

        best_state, best_fitness, _ = gradient_descent(problem)

        assert (len(best_state) == 10 and min(best_state) >= -1
                and max(best_state) <= 1 and best_fitness < test_fitness)
Exemplo n.º 2
0
    def _run_with_gd(self, init_weights, num_nodes, problem):
        if init_weights is None:
            init_weights = np.random.uniform(-1, 1, num_nodes)

        fitted_weights, loss, fitness_curve = gradient_descent(
            problem,
            max_attempts=self.max_attempts
            if self.early_stopping else self.max_iters,
            max_iters=self.max_iters,
            curve=self.curve,
            init_state=init_weights)

        return ([] if fitness_curve is None else
                fitness_curve), fitted_weights, loss
Exemplo n.º 3
0
    def test_gradient_descent_iter1():
        """Test gradient_descent function gets the correct answer after a
        single iteration"""

        X = np.array([[0, 1, 0, 1], [0, 0, 0, 0], [1, 1, 1, 1], [1, 1, 1, 1],
                      [0, 0, 1, 1], [1, 0, 0, 0]])

        y = np.reshape(np.array([1, 1, 0, 0, 1, 1]), [6, 1])

        nodes = [4, 2, 1]

        fitness = NetworkWeights(X,
                                 y,
                                 nodes,
                                 activation=identity,
                                 bias=False,
                                 is_classifier=False,
                                 learning_rate=0.1)

        problem = ContinuousOpt(10,
                                fitness,
                                maximize=False,
                                min_val=-1,
                                max_val=1,
                                step=0.1)

        init_weights = np.array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1])

        best_state, best_fitness, _ = gradient_descent(problem,
                                                       max_iters=1,
                                                       init_state=init_weights)

        x = np.array([-0.7, -0.7, -0.9, -0.9, -0.9, -0.9, -1, -1, -1, -1])

        assert (np.allclose(best_state, x, atol=0.001)
                and round(best_fitness, 2) == 19.14)