コード例 #1
0
def validation(
        nn: NeuralNetwork,
        training_set: Sequence[Pattern],
        validation_set: Sequence[Pattern],
        error_calculator: ErrorCalculator = ErrorCalculator.MSE
) -> ValidationResult:
    """

    :param nn
    :param training_set
    :param validation_set
    :param error_calculator
    :return: it returns the score and the respective epoch of that score.

    It fits the neural network and applies to the error calculator the validation curve.
    """
    old_error = nn.error_calculator

    nn.error_calculator = error_calculator

    nn.fit(training_set, validation_set, training_curve=False)
    learning_curve_validation = nn.validation_curve

    idx, score = error_calculator.choose(learning_curve_validation)

    nn.error_calculator = old_error

    return ValidationResult(
        epoch=idx + 1,
        score_validation=score,
    )
コード例 #2
0
def train(XTrain, YTrain, args):
    """
    This function is used for the training phase.
    Parameters
    ----------
    XTrain : numpy matrix
        The matrix containing samples features (not indices) for training.
    YTrain : numpy matrix
        The array containing labels for training.
    args : List
        The list of parameters to set up the NN model.
    Returns
    -------
    NN : NeuralNetwork object
        This should be the trained NN object.
    """
    # 1. Initializes a network object with given args.
    nn = NeuralNetwork(args["NNodes"], args["activate"], args["deltaActivate"],
                       args["task"])

    # 2. Train the model with the function "fit".
    # (hint: use the plotDecisionBoundary function to visualize after training)
    # Parameters TODO: arguments or script
    # Neural Network Execution
    nn.fit(XTrain, YTrain, args["learningRate"], args["epochs"],
           args["regLambda"], args["batchSize"])

    # 3. Return the model.
    return nn
コード例 #3
0
class TestNNBoolFunc(unittest.TestCase):
    def setUp(self):
        self.nn = NN(seed=0,
                     learning_algorithm=batch,
                     error_calculator=ErrorCalculator.MIS,
                     architecture=MultilayerPerceptron(
                         2,
                         activation=sigmoid,
                         activation_hidden=relu,
                         alambd=0,
                         alpha=0.9,
                         eta=0.9,
                     ))

    def test_and(self):
        self.try_data([
            ([0, 0], [0]),
            ([0, 1], [0]),
            ([1, 0], [0]),
            ([1, 1], [1]),
        ])

    def test_or(self):
        self.try_data([
            ([0, 0], [0]),
            ([0, 1], [1]),
            ([1, 0], [1]),
            ([1, 1], [1]),
        ])

    def test_xor(self):
        self.try_data([
            ([0, 0], [0]),
            ([0, 1], [1]),
            ([1, 0], [1]),
            ([1, 1], [0]),
        ])

    def try_data(self, data):
        self.nn.set()

        while True:
            self.nn.fit(data)
            print(self.nn.compute_error(data))
            if self.nn.compute_error(data) == 0:
                break

        self.assertEqual(self.nn.compute_error(data), 0)
コード例 #4
0
ファイル: test_monk.py プロジェクト: CarCas/NeuralNetwork
    def test_monk1(self):
        nn = NN(seed=4,
                epochs_limit=400,
                learning_algorithm=batch,
                error_calculator=ErrorCalculator.MSE,
                architecture=MultilayerPerceptron(
                    4,
                    activation=sigmoid,
                    activation_hidden=relu,
                    eta=0.5,
                    alambd=0,
                    alpha=0.8,
                ))

        train_data, test_data = read_monk(1)

        nn.fit(train_data)
        train_errs = nn.compute_learning_curve(train_data, ErrorCalculator.MIS)

        test_errs = nn.compute_learning_curve(test_data, ErrorCalculator.MIS)

        error_train = 0
        for x, d in train_data:
            error_train += (round(nn(x)[0][-1]) - d[0])**2

        error_test = 0
        for x, d in test_data:
            error_test += (round(nn(x)[0][-1]) - d[0])**2

        print(
            'train:',
            str(((len(train_data) - error_train) / len(train_data)) * 100) +
            '%')
        print(
            'test: ',
            str(((len(test_data) - error_test) / len(test_data)) * 100) + '%')

        self.assertEqual(error_train, 0)
        self.assertEqual(error_test, 0)

        nn.error_calculator = ErrorCalculator.MIS
        self.assertEqual(nn.compute_error(train_data), 0)
        self.assertEqual(nn.compute_error(test_data), 0)
コード例 #5
0
    def test_batch_explicit(self):
        np.random.seed(0)

        nn = NeuralNetwork(architecture=MultilayerPerceptron(
            layers=[[[0, 1.5, 2], [0, 3, 0.5]], [[0, -1.5, 1.5], [0, -0.5,
                                                                  2]]],
            alambd=0,
            alpha=0,
            eta=0.5,
            activation=sigmoid,
            activation_hidden=sigmoid,
        ),
                           learning_algorithm=minibatch(.5))

        nn.fit([([1, 1], [0, 1]), ([2, 2], [1, 1])])

        nn = nn._current_network
        np.testing.assert_array_almost_equal(
            nn.layers[0], [[-9.153689e-05, 1.499817e+00, 1.999817e+00],
                           [1.101478e-04, 3.000220e+00, 5.002203e-01]])

        np.testing.assert_array_almost_equal(
            nn.layers[1],
            [[0.0625, -1.437557, 1.562443], [0.013631, -0.486381, 2.013619]])
コード例 #6
0
ファイル: mnist.py プロジェクト: SimenCodes/ML-a3
network = NeuralNetwork(
    layer_dimensions=[X_train.shape[1], 32, 16, y_train.shape[1]],
    activations=[Sigmoid, Sigmoid, Sigmoid],
    keep_prob=[1.0, 0.8, 0.9, 1.0],
    # keep_prob=[1.0, 0.8, 0.7, 1.0],
    he_initialization=True)

print(X.shape, y.shape)

network.draw(filename='mnist', format='png')

network.fit(X_train.T,
            y_train.T,
            X_val.T,
            y_val.T,
            learning_rate=0.1,
            epochs=25000,
            verbose=100)

y_val_pred = np.argmax(network.predict(X_val.T), axis=0)
cm = confusion_matrix(np.argmax(y_val.T, axis=0),
                      y_val_pred,
                      labels=list(range(10)))

print(cm)

plt.imshow(cm)
plt.xlabel("predicted")
plt.ylabel("true")
plt.xticks(list(range(10)))
コード例 #7
0
ファイル: main.py プロジェクト: manthan1412/Neural-network
                else:
                    labels.append(0)
    if verbose:
        # print(data)
        # print(files)
        print(labels)
        print()

    return data, labels


if __name__ == '__main__':
    train_data, train_target = fetch_data('downgesture_train.list')
    neuralnet = NeuralNetwork()
    neuralnet.add_layer(size=1, input_size=len(train_data[0]), type='input')
    neuralnet.add_layer(size=100)
    neuralnet.add_layer(size=1, type='output')

    neuralnet.fit(data=train_data,
                  target=train_target,
                  eta=0.1,
                  verbose=verbose)
    # if needed clean data

    # fit a model
    # train a model
    test_data, test_target = fetch_data('downgesture_test.list')
    predicted_target = neuralnet.predict(test_data)
    accuracy = neuralnet.accuracy(test_target, predicted_target)
    print("Accuracy:", accuracy)
コード例 #8
0
ファイル: xor.py プロジェクト: SimenCodes/ML-a3
import matplotlib.pylab as plt
import numpy as np

from activation_functions import Sigmoid, ReLU, Swich
from nn import NeuralNetwork

network = NeuralNetwork(
    layer_dimensions=[2, 2, 1],
    activations=[Sigmoid, Sigmoid],
    keep_prob=[1.0, 1.0, 1.0],
    he_initialization=True,
)
x = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]).T
y = np.array([[0], [1], [1], [0]]).T

network.draw(filename='xor', view=False, format='png')

network.fit(x, y, learning_rate=0.3, epochs=10000, verbose=100)

print(network.predict(x))
plt.plot(network.cost, label='loss')
plt.plot(network.acc, label='acc')
plt.legend()
plt.show()
コード例 #9
0
        seed=seed,
        epochs_limit=epochs_limit,
        learning_algorithm=batch,
        n_init=1,
        error_calculator=ErrorCalculator.MSE,
        architecture=MultilayerPerceptron(
            size_hidden_layers=(2, 2),
            eta=eta,
            alpha=alpha,
            alambd=alambd,
            activation=tanh_classification,
            activation_hidden=relu,
        ),
    )

    nn.fit(train_set)

    nn.error_calculator = ErrorCalculator.MSE
    print('mse', nn.compute_error(train_set), nn.compute_error(validation_set),
          nn.compute_error(test_data))

    nn.error_calculator = ErrorCalculator.MEE
    print('mee', nn.compute_error(train_set), nn.compute_error(validation_set),
          nn.compute_error(test_data))

    nn.error_calculator = ErrorCalculator.ACC
    print('acc', nn.compute_error(train_set), nn.compute_error(validation_set),
          nn.compute_error(test_data))

    # MSE
    nn.error_calculator = ErrorCalculator.MSE
コード例 #10
0
        a[i, x[i]] = 1
    return a


if __name__ == '__main__':

    digits = datasets.load_digits()
    X = preprocessing.scale(digits.data.astype(float))
    y = targetToVector(digits.target)

    X_train, X_test, y_train, y_test = model_selection.train_test_split(
        X, y, test_size=0.2, random_state=0)

    NN = NeuralNetwork(64, 60, 10, output_act='softmax')
    NN.fit(X_train,
           y_train,
           epochs=50,
           learning_rate=.1,
           learning_rate_decay=.01,
           verbose=1)

    y_predicted = NN.predict(X_test)

    y_predicted = np.argmax(y_predicted, axis=1).astype(int)
    y_test = np.argmax(y_test, axis=1).astype(int)

    print("\nClassification report for classifier:\n\n%s\n" %
          (metrics.classification_report(y_test, y_predicted)))
    print("Confusion matrix:\n\n%s" %
          metrics.confusion_matrix(y_test, y_predicted))
コード例 #11
0
from nn import NeuralNetwork

import numpy as np

# lst = ['a', 'b', 'c']

# pool = cycle(lst)

# for item in pool:
# 	print(item)

X = np.array([[0, 81, 75, 0]])
y = np.array([[1]])
nn = NeuralNetwork(X,y)
nn.add(7)

nn.fit(12,100,0.5,0.001)