def test_calculate_updates(): """Test calculate_updates method""" 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=1) a = list(np.arange(8) + 1) b = list(0.01 * (np.arange(2) + 1)) weights = a + b fitness.evaluate(weights) updates = fitness.calculate_updates() update1 = np.array([[-0.0017, -0.0034], [-0.0046, -0.0092], [-0.0052, -0.0104], [0.0014, 0.0028]]) update2 = np.array([[-3.17], [-4.18]]) assert (np.allclose(updates[0], update1, atol=0.001) and np.allclose(updates[1], update2, atol=0.001))
def test_evaluate_no_bias_classifier(): """Test evaluate method for binary classifier with no bias term""" 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) a = list(np.arange(8) + 1) b = list(0.01 * (np.arange(2) + 1)) weights = a + b assert round(fitness.evaluate(weights), 4) == 0.7393
def test_evaluate_no_bias_multi(): """Test evaluate method for multivariate classifier with no bias term""" 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.array([[1, 1], [1, 0], [0, 0], [0, 0], [1, 0], [1, 1]]) nodes = [4, 2, 2] fitness = NetworkWeights(X, y, nodes, activation=identity, bias=False) a = list(np.arange(8) + 1) b = list(0.01 * (np.arange(4) + 1)) weights = a + b assert round(fitness.evaluate(weights), 4) == 0.7183
def test_evaluate_bias_regressor(): """Test evaluate method for regressor with bias term""" 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 = [5, 2, 1] fitness = NetworkWeights(X, y, nodes, activation=identity, bias=True, is_classifier=False) a = list(np.arange(10) + 1) b = list(0.01 * (np.arange(2) + 1)) weights = a + b assert round(fitness.evaluate(weights), 4) == 0.4363