Esempio n. 1
0
def main():
    numpy.random.seed(3)

    if not os.path.exists("cache"):
        os.makedirs("cache")

    if os.path.exists("cache/training_data.npy"):
        training_data = numpy.load('cache/training_data.npy')
    else:
        training_data = read_images(TRAIN['data'], DATA_FOLDER)
        numpy.save('cache/training_data', training_data)

    if os.path.exists("cache/training_labels.npy"):
        training_labels = numpy.load('cache/training_labels.npy')
    else:
        training_labels = read_labels(TRAIN['labels'], DATA_FOLDER)
        numpy.save('cache/training_labels', training_labels)

    training_data = training_data.reshape(training_data.shape[0], -1)

    number_of_inputs = training_data.shape[1]
    number_of_labels = numpy.unique(training_labels).size
    topology = [number_of_inputs, 50, 30, number_of_labels]

    if os.path.exists("cache/test_data.npy"):
        test_data = numpy.load('cache/test_data.npy')
    else:
        test_data = read_images(TEST['data'], DATA_FOLDER)
        numpy.save('cache/test_data', test_data)

    if os.path.exists("cache/test_labels.npy"):
        test_labels = numpy.load('cache/test_labels.npy')
    else:
        test_labels = read_labels(TEST['labels'], DATA_FOLDER)
        numpy.save('cache/test_labels', test_labels)

    test_data = test_data.reshape(test_data.shape[0], -1)

    normalized = Normalizer(training_data=training_data, test_data=test_data)

    for i in range(1):
        print("Running {}th time...".format(i))
        neural_network = NeuralNetwork(topology)
        neural_network.train(normalized.training_data(),
                             training_labels,
                             test_data=normalized.test_data(),
                             test_labels=test_labels)