Ejemplo n.º 1
0
def test_percent_error():
    number_of_classes = 10
    number_of_test_samples_to_use = 100
    (X_train, y_train), (X_test, y_test) = tf.keras.datasets.mnist.load_data()
    X_test_vectorized = ((X_test.reshape(
        X_test.shape[0], -1)).T)[:, 0:number_of_test_samples_to_use]
    y_test = y_test[0:number_of_test_samples_to_use]
    input_dimensions = X_test_vectorized.shape[0]
    model = Hebbian(input_dimensions=input_dimensions,
                    number_of_classes=number_of_classes,
                    transfer_function="Sigmoid",
                    seed=5)
    percent_error = model.calculate_percent_error(X_test_vectorized, y_test)
    np.testing.assert_almost_equal(percent_error, 0.86, decimal=2)
    model = Hebbian(input_dimensions=input_dimensions,
                    number_of_classes=number_of_classes,
                    transfer_function="Linear",
                    seed=15)
    percent_error = model.calculate_percent_error(X_test_vectorized, y_test)
    np.testing.assert_almost_equal(percent_error, 0.96, decimal=2)
    model = Hebbian(input_dimensions=input_dimensions,
                    number_of_classes=number_of_classes,
                    transfer_function="Hard_limit",
                    seed=8)
    percent_error = model.calculate_percent_error(X_test_vectorized, y_test)
    np.testing.assert_almost_equal(percent_error, 0.91, decimal=2)
Ejemplo n.º 2
0
def test_training():
    number_of_classes = 10
    number_of_training_samples_to_use = 1000
    number_of_test_samples_to_use = 100
    (X_train, y_train), (X_test, y_test) = tf.keras.datasets.mnist.load_data()
    X_train_vectorized = ((X_train.reshape(
        X_train.shape[0], -1)).T)[:, 0:number_of_training_samples_to_use]
    y_train = y_train[0:number_of_training_samples_to_use]
    X_test_vectorized = ((X_test.reshape(
        X_test.shape[0], -1)).T)[:, 0:number_of_test_samples_to_use]
    y_test = y_test[0:number_of_test_samples_to_use]
    input_dimensions = X_test_vectorized.shape[0]
    model = Hebbian(input_dimensions=input_dimensions,
                    number_of_classes=number_of_classes,
                    transfer_function="Hard_limit",
                    seed=5)
    model.initialize_all_weights_to_zeros()
    percent_error = []
    for k in range(10):
        model.train(X_train_vectorized,
                    y_train,
                    batch_size=300,
                    num_epochs=2,
                    alpha=0.1,
                    gamma=0.1,
                    learning="Delta")
        percent_error.append(
            model.calculate_percent_error(X_test_vectorized, y_test))
    confusion_matrix = model.calculate_confusion_matrix(
        X_test_vectorized, y_test)
    assert (np.array_equal(confusion_matrix, np.array( \
        [[8., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
         [1., 13., 0., 0., 0., 0., 0., 0., 0., 0.],
         [1., 0., 7., 0., 0., 0., 0., 0., 0., 0.],
         [2., 0., 1., 8., 0., 0., 0., 0., 0., 0.],
         [1., 0., 0., 1., 12., 0., 0., 0., 0., 0.],
         [4., 0., 1., 0., 0., 2., 0., 0., 0., 0.],
         [3., 0., 2., 0., 0., 0., 5., 0., 0., 0.],
         [1., 0., 0., 2., 0., 0., 0., 11., 0., 1.],
         [2., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
         [1., 0., 0., 0., 0., 0., 0., 1., 0., 9.]]))) or \
           (np.array_equal(confusion_matrix, np.array( \
               [[8., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
                [1., 13., 0., 0., 0., 0., 0., 0., 0., 0.],
                [1., 0., 6., 0., 0., 0., 1., 0., 0., 0.],
                [2., 0., 1., 8., 0., 0., 0., 0., 0., 0.],
                [2., 0., 0., 1., 11., 0., 0., 0., 0., 0.],
                [4., 0., 1., 0., 0., 2., 0., 0., 0., 0.],
                [4., 0., 1., 0., 0., 0., 5., 0., 0., 0.],
                [2., 0., 0., 1., 0., 0., 0., 12., 0., 0.],
                [1., 0., 0., 0., 0., 0., 0., 0., 1., 0.],
                [3., 0., 0., 0., 0., 0., 0., 3., 0., 5.]])))

    assert np.allclose(percent_error,
    np.array([0.74, 0.35, 0.32, 0.3, 0.28, 0.32, 0.25, 0.26, 0.3, 0.25]),rtol=1e-3, atol=1e-3) or \
           np.allclose(percent_error,
                        np.array([0.8 ,0.37,0.36,0.32,0.31,0.31,0.29,0.29,0.24,0.29]), rtol=1e-3, atol=1e-3)
    model = Hebbian(input_dimensions=input_dimensions,
                    number_of_classes=number_of_classes,
                    transfer_function="Linear",
                    seed=5)
    percent_error = []
    for k in range(10):
        model.train(X_train_vectorized,
                    y_train,
                    batch_size=300,
                    num_epochs=2,
                    alpha=0.1,
                    gamma=0.1,
                    learning="Filtered")
        percent_error.append(
            model.calculate_percent_error(X_test_vectorized, y_test))
    confusion_matrix = model.calculate_confusion_matrix(
        X_test_vectorized, y_test)
    assert np.array_equal(confusion_matrix, np.array( \
        [[8., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
         [0., 11., 0., 1., 0., 0., 0., 0., 2., 0.],
         [2., 0., 4., 1., 0., 0., 0., 1., 0., 0.],
         [2., 0., 1., 8., 0., 0., 0., 0., 0., 0.],
         [1., 0., 0., 0., 11., 0., 0., 1., 0., 1.],
         [5., 0., 0., 1., 0., 0., 0., 1., 0., 0.],
         [3., 0., 1., 0., 0., 0., 6., 0., 0., 0.],
         [0., 0., 0., 0., 0., 0., 0., 15., 0., 0.],
         [0., 0., 1., 0., 0., 0., 0., 0., 1., 0.],
         [0., 0., 0., 0., 0., 0., 0., 8., 1., 2.]]))
    np.testing.assert_almost_equal(
        percent_error,
        [0.34, 0.34, 0.34, 0.34, 0.34, 0.34, 0.34, 0.34, 0.34, 0.34],
        decimal=2)