Esempio n. 1
0
    def testSaveAndLoad(self):
        nn = neuralnet.NeuralNet(2, [2, 2], neuralnet.Sigmoid())
        nn.layers[0][0].weights=[0.01, 0.02]
        nn.layers[0][0].bias=0.03
        nn.layers[0][1].weights=[0.04, 0.05]
        nn.layers[0][1].bias=0.06
        nn.layers[1][0].weights=[0.07, 0.08]
        nn.layers[1][0].bias=0.09
        nn.layers[1][1].weights=[0.10, 0.11]
        nn.layers[1][1].bias=0.12

        tmpfile = tempfile.mkstemp()[1]
        nn.save(tmpfile)

        nn2 = neuralnet.NeuralNet(1, [1, 1], neuralnet.ReLu())
        nn2.load(tmpfile)

        # This should give a more readable error when there are differences
        self.assertEqual(nn.__str__(), nn2.__str__())

        # This ensure the previous test didn't miss any important thing
        self.assertEqual(nn2.activation.name(), nn.activation.name())
        self.assertEqual(nn2.average_gradient, nn.average_gradient)
        for i in range(0, 2):
            for j in range(0, 2):
                for k in range(0, 2):
                    self.assertAlmostEqual(nn.layers[i][j].weights[k], nn2.layers[i][j].weights[k])
                self.assertAlmostEqual(nn.layers[i][j].bias, nn2.layers[i][j].bias)
Esempio n. 2
0
 def cross_over(self):
     depth_a = self.father.get_network_depth()
     depth_b = self.mother.get_network_depth()
     if depth_a > depth_b:
         warp_length = depth_a
     else:
         warp_length = depth_b
     self.father.warp_layer_probablity(warp_length)
     self.mother.warp_layer_probablity(warp_length)
     layer_warp_a = self.father.get_warp_probability()
     layer_warp_b = self.mother.get_warp_probability()
     layer_output = (layer_warp_a + layer_warp_b) / 2
     new_list_layers = []
     for col_ind, layer_prob in enumerate(layer_output.T):
         layer_prob_a = layer_warp_a[:, col_ind]
         layer_prob_b = layer_warp_b[:, col_ind]
         select_a = self.select_one_parent(layer_prob, layer_prob_a,
                                           layer_prob_b)
         if select_a == True:
             layer = self.select_layer_from_parent(self.father, col_ind)
         else:
             layer = self.select_layer_from_parent(self.mother, col_ind)
         new_list_layers.append(layer)
     new_net = nn.NeuralNet(self.family_num, self.child_num)
     new_net.set_list_layers(
         new_list_layers, max(self.father.layer_num, self.mother.layer_num))
     if not test:
         c = combine(self.father, self.mother, new_net)
         c.combine()
     return new_net
Esempio n. 3
0
 def mutation_inter_layer(self, net):
     if not test:
         print('net ' + net.get_fnum_gen() + ' inter mutation')
     mutated_net = nn.NeuralNet(self.family_num, self.child_num)
     mutated_net.set_list_layers(net.get_list_layers(), net.layer_num)
     mutated_net.adjust_layer_list_random()
     return mutated_net
Esempio n. 4
0
def train_network(filename):
    params = load_params(filename)
    limit = params['limit']

    train_labels, train_images, train_examples = load_data(
        TRAIN_LABELS, TRAIN_IMAGES, limit)
    test_labels, test_images, test_examples = load_data(
        TEST_LABELS, TEST_IMAGES, limit)

    layers = params['layers']
    output_file = params['output_file']
    average_gradient = params['average_gradient']
    activation = neuralnet.activations[params['activation']]

    nn = neuralnet.NeuralNet(784, layers, activation, average_gradient)
    # Uncomment to continue an already started training
    #nn.load(params['output_file'])

    nb_epochs = params['nb_epochs']
    training_rounds = params['rounds_per_epoch']
    samples_per_round = params['samples_per_round']
    learning_rates = params['learning_rates']

    # Each loop takes ~1000s ~= 16m
    for epoch in range(0, nb_epochs):
        learning_rate = learning_rates[epoch]
        score = evaluate_network(nn, test_examples, test_labels, test_images)
        print("Epoch %s, Score before: %s, Learning rate: %s" %
              (epoch, score, learning_rate))
        nn.train(training_rounds, samples_per_round, learning_rate,
                 train_examples, train_labels)
        nn.save(output_file)
Esempio n. 5
0
 def __init__(self, name=None, path=None):
     neuron.Neuron.__init__(self, name)
     self.Path = path
     if path is None:
         self.Net = neuralnet.NeuralNet()
     else:
         self.Net = neuralnetio.NeuralNetIO().read(path)
Esempio n. 6
0
    def testMultipleLayersNetworkActivation(self):
        nn = neuralnet.NeuralNet(2, [2, 2], neuralnet.ReLu())
        # 1 -- 0.5 --> [-0.1](A) -- 0.7 --> [-0.2](C)
        #     ^  |              ^  |
        #     |  0.7            |  0.8
        #   0.2    |          0.5    |
        #   |      v          |      v
        # 0 -- 0.6 --> [-0.1](B) -- 0.9 --> [-0.3](D)
        # (A) = 0.5 - 0.1 = 0.4
        # (B) = 0.7 - 0.1 = 0.6
        # (C) = 0.4x0.7 + 0.6x0.5 - 0.2 = 0.38
        # (D) = 0.9x0.6 + 0.8x0.4 - 0.3 = 0.56
        nn.layers[0][0].weights=[0.5, 0.2]
        nn.layers[0][0].bias=-0.1
        nn.layers[0][1].weights=[0.7, 0.6]
        nn.layers[0][1].bias=-0.1
        nn.layers[1][0].weights=[0.7, 0.5]
        nn.layers[1][0].bias=-0.2
        nn.layers[1][1].weights=[0.8, 0.9]
        nn.layers[1][1].bias=-0.3

        neuronA_output = nn.layers[0][0].output([1, 0], for_training=False)
        self.assertAlmostEqual(0.4, neuronA_output)
        neuronB_output = nn.layers[0][1].output([1, 0], for_training=False)
        self.assertAlmostEqual(0.6, neuronB_output)

        output = nn.evaluate([1, 0])

        self.assertAlmostEqual(0.38, output[0])
        self.assertAlmostEqual(0.56, output[1])
Esempio n. 7
0
def main():
    print("Machine Learning Tutorial 2 - Making a Network")

    n = neuralnet.NeuralNet()
    x = numpy.array([2, 3])
    result = n.feedforward(x)

    print(result)  # 0.7216325609518421
Esempio n. 8
0
def league():
    """
    현재 존재하는 신경망들끼리 리그 게임을 진행한 후,
    승률이 높은 신경망들만 살리고 각각 신경망들의 자손을 만든다.
    남은 자리는 다시 랜덤 신경망으로 채운다.
    """
    global learningList, leagueNum
    leagueNum += 1
    winlose = 0
    print('[League {}] Start!'.format(leagueNum))
    for i in range(LEARNING_COUNT):
        learningList[i].winScore = 0
    for i in range(LEARNING_COUNT):
        for j in range(i + 1, LEARNING_COUNT):
            leagueGame = game.Game()
            print('[League {}] No.{} vs No.{}'.format(leagueNum, i, j))
            while 1:
                predict1 = learningList[i].predict(leagueGame, 1)
                if predict1 is not None:
                    leagueGame.place(predict1[0], predict1[1], 1)

                predict2 = learningList[j].predict(leagueGame, -1)
                if predict2 is not None:
                    leagueGame.place(predict2[0], predict2[1], -1)

                if predict1 is None and predict2 is None:
                    score = leagueGame.getScore()
                    if score[0] > score[1]:
                        learningList[i].winScore += (score[0] - score[1])
                    elif score[0] < score[1]:
                        learningList[j].winScore += (score[1] - score[0])
                    print('[League {}] Game over! {}:{}'.format(leagueNum, score[0], score[1]))
                    '''
                    for u in range(8):
                        for v in range(8):
                            if leagueGame.gameBoard[u][v] == 1:
                                print('+', end='')
                            elif leagueGame.gameBoard[u][v] == -1:
                                print('-', end='')
                            else:
                                print('0', end='')
                        print('')
                    '''
                    if score[0] > score[1]:
                        winlose += 1
                    elif score[0] < score[1]:
                        winlose -= 1
                    break
    print('[League {}] W/L Point: {}'.format(leagueNum, winlose))
    learningList.sort(key=lambda nn: nn.winScore, reverse=True)
    newLearningList = []
    for i in range(WIN_COUNT):
        for j in range(i + 1, WIN_COUNT):
            newLearningList.append(learningList[i].gen(learningList[j]))
    while len(newLearningList) < LEARNING_COUNT:
        newLearningList.append(neuralnet.NeuralNet())
    learningList = newLearningList
    save()
Esempio n. 9
0
 def mutation_intra_layer(self, net):
     if not test:
         print('net ' + net.get_fnum_gen() + ' intra mutation')
     mutated_net = nn.NeuralNet(self.family_num, self.child_num)
     mutated_net.set_list_layers(net.get_list_layers(), net.layer_num)
     diff = randint(0, int(len(net.list_layers)))
     for _ in range(diff):
         mutated_net.adjust_layer_attr_random()
     return mutated_net
Esempio n. 10
0
 def testTrainAndPredictOnXor(self):
     dataset = [
         # Both negatives
         ([-0.5, -0.5], 1),
         ([-0.5, -0.3], 1),
         ([-0.5, -0.2], 1),
         ([-0.3, -0.5], 1),
         ([-0.3, -0.3], 1),
         ([-0.3, -0.2], 1),
         ([-0.2, -0.5], 1),
         ([-0.2, -0.3], 1),
         ([-0.2, -0.2], 1),
         # First negative, second positive
         ([-0.5, 0.5], 0),
         ([-0.5, 0.3], 0),
         ([-0.5, 0.2], 0),
         ([-0.3, 0.5], 0),
         ([-0.3, 0.3], 0),
         ([-0.3, 0.2], 0),
         ([-0.2, 0.5], 0),
         ([-0.2, 0.3], 0),
         ([-0.2, 0.2], 0),
         # Both positives
         ([0.5, 0.5], 1),
         ([0.5, 0.3], 1),
         ([0.5, 0.2], 1),
         ([0.3, 0.5], 1),
         ([0.3, 0.3], 1),
         ([0.3, 0.2], 1),
         ([0.2, 0.5], 1),
         ([0.2, 0.3], 1),
         ([0.2, 0.2], 1),
         # First positive, second negative
         ([0.5, -0.5], 0),
         ([0.5, -0.3], 0),
         ([0.5, -0.2], 0),
         ([0.3, -0.5], 0),
         ([0.3, -0.3], 0),
         ([0.3, -0.2], 0),
         ([0.2, -0.5], 0),
         ([0.2, -0.3], 0),
         ([0.2, -0.2], 0),
     ]
     examples = [a[0] for a in dataset]
     labels = [a[1] for a in dataset]
     nn = neuralnet.NeuralNet(2, [2, 2], neuralnet.Sigmoid())
     nn.train(20*20*20, 10, 1.0, examples, labels)
     print("")
     print(nn.evaluate([0.4, 0.4]))   # 1
     print(nn.evaluate([-0.3, 0.3]))  # 0
     print(nn.evaluate([-0.3, -0.4])) # 1
     print(nn.evaluate([0.4, -0.4]))  # 0
     print("")
     self.assertEqual(1, nn.predict([0.4, 0.4]))
     self.assertEqual(0, nn.predict([-0.3, 0.3]))
     self.assertEqual(1, nn.predict([-0.3, -0.4]))
     self.assertEqual(0, nn.predict([0.4, -0.4]))
Esempio n. 11
0
    def __init__(self,
                 surface,
                 model_filepath=None,
                 whiskers_on=False,
                 smell_on=False):

        # Assign physical properties
        self.health = 1

        self.orientation = 0
        self.position = np.random.rand(2) * surface.get_size()

        self.velocity = np.array([0.0, 0.0],
                                 dtype='float')  #np.random.rand(2) * 10 - 5
        self.velocity_max = 0.1

        self.acceleration = np.array([0, 0], dtype='float')
        self.acceleration_max = 0.001

        # Assign variables used in draw()
        self.draw_velocity = False
        self.draw_whiskers = False

        self.surface = surface
        self.body_draw_size = 6.0
        self.velocity_line_length = 1.5
        self.colour_inner = np.random.rand(3) * 255
        self.colour_outer = np.array([255, 255, 255])

        # Assign variables for whiskers()
        self.whiskers_on = whiskers_on
        self.n_whiskers = 15
        self.vision_range = 30

        # Assign variables for smell()
        self.smell_on = smell_on
        self.smell_memory_length = 5
        self.smell_memory = range(self.smell_memory_length)

        # Assign variables for eat()
        self.eat_range = 3

        # Assign a neural net as a brain
        self.nn_input_size = 0
        if self.whiskers_on:
            self.nn_input_size += self.n_whiskers
        if self.smell_on:
            self.nn_input_size += self.smell_memory_length
        self.model_filepath = model_filepath
        self.brain = neuralnet.NeuralNet(input_size=self.nn_input_size,
                                         output_size=9,
                                         model_filepath=self.model_filepath)
        self.state_previous = None
        self.action_previous = None
        self.reward_previous = 0
Esempio n. 12
0
def load():
    """
    파일에 저장된 학습 데이터를 불러온다.
    """
    with open('learn.json', 'r') as jsonFile:
        global learningList
        loadList = []
        for w in json.load(jsonFile):
            loadList.append(neuralnet.NeuralNet(w))
        learningList = loadList
        jsonFile.close()
Esempio n. 13
0
 def create_seed_net(self, num, net_name=None, load_network=None):
     # create seed network by name in seed library
     seed_net = nn.NeuralNet(0,
                             num,
                             net_name=net_name,
                             load_network=load_network,
                             create_seed=True,
                             is_seed=True)
     seed_net.kick_simulation()
     seed_net.output_deploy_network()
     return seed_net
Esempio n. 14
0
def init():
    """
    이미 학습된 데이터가 있다면 불러오고 없으면 새로운 학습 데이터를 만든다.
    """
    from pathlib import Path
    if Path('learn.json').is_file():
        print('File exists')
        load()
    else:
        print('File not exists. Initializing')
        for i in range(LEARNING_COUNT):
            learningList.append(neuralnet.NeuralNet())
        save()
Esempio n. 15
0
def reverse_evaluate(filename):
    nn = neuralnet.NeuralNet(0, [], neuralnet.Sigmoid())
    nn.load(filename)
    inputs = len(nn.layers[-1])
    layers = [len(l) for l in reversed(nn.layers[:-1])]
    layers.append(len(nn.layers[0][0].weights))
    rev_nn = neuralnet.NeuralNet(inputs, layers, nn.activation)
    for li, l in enumerate(nn.layers):
        rl = rev_nn.layers[-li - 1]
        for ni, n in enumerate(l):
            rn = rl[ni]
            rn.weights = [0] * len(n.weights)
            for wi, w in enumerate(n.weights):
                rn.weights[wi] = w
            rn.bias = -n.bias
    results = []
    for i in range(inputs):
        inp = [0] * inputs
        inp[i] = 1
        result = rev_nn.evaluate(inp, for_training=False)
        results.append(result)
    return results
Esempio n. 16
0
 def cross_to_new_net_point(self):
     if not test:
         print('net ' + self.father.get_fnum_gen() + ' and net ' +
               self.mother.get_fnum_gen() + ' cross new net')
     cross_point = randint(0, self.father.get_network_depth())
     new_layer_list = self.father.list_layers[:
                                              cross_point] + self.mother.list_layers[
                                                  cross_point:]
     new_net = nn.NeuralNet(self.family_num, self.child_num)
     new_net.set_list_layers(
         new_layer_list, max(self.father.layer_num, self.mother.layer_num))
     if not test:
         c = combine(self.father, self.mother, new_net)
         c.combine()
     return new_net
Esempio n. 17
0
def main(data):
    nn = NNet.NeuralNet(64, 56, 20)
    nn.set_param(0.8, 0.3)
    ev = eps + 1
    while (eps < ev):
        nn.learn(data)
        ev = nn.evaluate(data)
        print(ev)

    print(nn.forward(ds.ldata_a[0][0]))
    print(nn.test(ds.ldata_a))
    print(nn.test(ds.tdata_a))
    print(nn.test(ds.ldata_b))
    print(nn.test(ds.tdata_b))
    print(nn.test(ds.ldata))
    print(nn.test(ds.tdata))
Esempio n. 18
0
    def testTrainOnSimpleExample(self):
        # Simple example that only depends on the first parameter.
        # A linear separation would be enough but it has the benefit of training fast :-)
        """ Created with:
        import random
        print(random.random()*4, random.random()*5, 0) for i in range(0, 5)
        print(5+random.random()*4, random.random()*5, 1) for i in range(0, 5)
        """
        dataset = [
            ([2.7810836, 2.550537003],   0),
            ([1.465489372, 2.362125076], 0),
            ([3.396561688, 4.400293529], 0),
            ([1.38807019, 1.850220317],  0),
            ([3.06407232, 3.005305973],  0),
            ([7.627531214, 2.759262235], 1),
            ([5.332441248, 2.088626775], 1),
            ([6.922596716, 1.77106367],  1),
            ([8.675418651, -0.242068655],1),
            ([7.673756466, 3.508563011], 1),
        ]
        examples = [a[0] for a in dataset]
        labels = [a[1] for a in dataset]

        nn = neuralnet.NeuralNet(2, [2, 2], neuralnet.Sigmoid())
        # This empirically proves to be good parameters to train on this
        nn.train(10*20*20, 1, 1.0, examples, labels)
        # TODO: This should work like this too:
        #nn.train(20*20, 10, 1.0, examples, labels)

        """
        print("")
        print(nn.evaluate([2, 5]))
        print(nn.evaluate([2.5, 2.5]))
        print(nn.evaluate([3, 0]))
        print(nn.evaluate([6, 0]))
        print(nn.evaluate([7.5, 2.5]))
        print(nn.evaluate([9, 5]))
        print("")
        """
        self.assertEqual(0, nn.predict([2, 5]))
        self.assertEqual(0, nn.predict([2.5, 2.5]))
        # This usually fails pretty badly on this one which is not necessarily surprising: there's no example close to it
        #self.assertEqual(0, nn.predict([3, 0]))
        self.assertEqual(1, nn.predict([6, 0]))
        self.assertEqual(1, nn.predict([7.5, 2.5]))
        self.assertEqual(1, nn.predict([9, 5]))
Esempio n. 19
0
def test_network(filename):
    test_labels, test_images, test_examples = load_data(
        TEST_LABELS, TEST_IMAGES, None)
    labels = set(test_labels)
    confusion = [[0] * len(labels) for i in range(len(labels))]
    nn = neuralnet.NeuralNet(784, [10],
                             neuralnet.Sigmoid(),
                             average_gradient=False)
    nn.load(filename)
    score = evaluate_network(nn,
                             test_examples,
                             test_labels,
                             test_images,
                             verbose=False,
                             confusion=confusion)
    print("Score: %s" % score)
    show_confusion(confusion)
Esempio n. 20
0
def read(file_name):
    f = open(file_name, 'r')
    lines = f.readlines()
    depth = int(lines[0].replace('\n', ''))
    topology = []
    for i in range(1, depth + 1):
        topology.append(int(lines[i].replace('\n', '')))
    net = nn.NeuralNet(topology)
    skip = 0
    for l in range(depth - 1):
        for i in range(topology[l] + 1):
            line = lines[depth + 1 + i + skip].replace('\n', '')
            weights = line.split(' ')
            for j in range(topology[l + 1]):
                net.theta[l][i, j] = float(weights[j])
        skip += topology[l] + 1
    f.close()
    return net
Esempio n. 21
0
def test(x, y, lam, maxiter, normalize, step):
    """Test the given network on the train and validation sets
    
    Arguments:\n
        [X] must be a file path to a CSV which holds your training data\n
        [Y] must be a file path to a CSV which holds your expected outputs for the training examples

    (neural cli will ommit the first row for column headers for both CSVs)
    """

    X = np.loadtxt(x, delimiter=",", skiprows=1, dtype="float")
    Y = np.loadtxt(y, delimiter=",", skiprows=1, dtype="float")

    nn = neuralnet.NeuralNet(X=X,
                             Y=Y,
                             writer=writer,
                             lam=lam,
                             maxiter=maxiter,
                             norm=normalize)
    nn.test(step)
Esempio n. 22
0
def train(x, y, output, lam, maxiter, normalize, verbose):
    """Train a neural network with the given X and Y parameters.
    
    Arguments:\n
        [X] must be a file path to a CSV which holds your training data\n
        [Y] must be a file path to a CSV which holds your expected outputs for the training examples

    (neural cli will ommit the first row for column headers for both CSVs)
    """
    X = np.loadtxt(x, delimiter=",", skiprows=1, dtype="float")
    Y = np.loadtxt(y, delimiter=",", skiprows=1, dtype="float")

    nn = neuralnet.NeuralNet(X=X,
                             Y=Y,
                             writer=writer,
                             output=output,
                             lam=lam,
                             maxiter=maxiter,
                             norm=normalize)
    nn.train(verbose=verbose, save=output)
    nn.accuracy()
Esempio n. 23
0
def runNeuralNet(numTrainValues, numTestValues, pixels, tune, useTrainedWeights, info):
    """
    runNeuralNet() runs the neural net machine learning algorithm on the MNIST
    dataset.
    """
    # TODO: Add the rest of the params to function argument.

    t = time.clock()
    neuralClassifier = neuralnet.NeuralNet(range(10))

    print "Loading Testing Data....\n"
    trainingData, trainingLabels, validationData, validationLabels, features = loadFeatures.loadTrainingData(numTrainValues, pixels, tune)

    print "Loading Testing Data....\n"
    testingData, testingLabels = loadFeatures.loadTestingData(numTestValues, pixels)

    print "Testing Neural Net....\n"
    classifiedData = neuralClassifier.classify(testingData)
    test(classifiedData, testingLabels, info)

    print "Total Time {0}".format(time.clock() - t)
Esempio n. 24
0
 def _initialize_neural_net(self):
     # 新建神经网络
     self.net = neuralnet.NeuralNet(self.min_boundary,
                                    self.max_boundary,
                                    archive_dir=self.archive_dir,
                                    start_datetime=self.start_datetime)
     self.net.init(self.num_params, self.layer_dims,
                   self.train_threshold_ratio, self.batch_size,
                   self.dropout_prob, self.regularisation_coefficient)
     # 随机初始化网络多次,选择在窗口参数上 loss 最小的权重
     best_loss = float('inf')
     for _ in range(self.init_net_weight_num):
         self.net.reset_weights()
         self.net.fit(self.train_params_set, self.train_costs_set,
                      self.max_epoch, self.window_params_set,
                      self.window_costs_set)
         loss = self.net.get_loss(self.window_params_set,
                                  self.window_costs_set)
         if loss < best_loss:
             best_loss = loss
             best_weights = self.net.get_weights()
     self.net.set_weights(best_weights)
Esempio n. 25
0
    def load(self, start_datetime):
        # 加载存档
        load_archive_filename = os.path.join(
            self.archive_dir,
            self.archive_file_prefix + start_datetime + '.txt')
        # 从存档中读取参数
        print("Loading...")
        self._load_archive(load_archive_filename)

        # 读取上次保存的典型参数,获取实验结果
        self.last_iteration += 1
        print("Iteration %d..." % self.last_iteration)
        self.init_costs_set = self.get_experiment_costs(self.init_params_set)

        # 筛选好的参数放入窗口,最多不超过初始参数的一半
        max_size = min(self.window_size, len(self.init_costs_set) // 2)
        indexes = np.argsort(self.init_costs_set)
        self.window_params_set = self.init_params_set[indexes[:max_size]]
        self.window_costs_set = self.init_costs_set[indexes[:max_size]]
        self.train_params_set = self.init_params_set[indexes[max_size:]]
        self.train_costs_set = self.init_costs_set[indexes[max_size:]]
        # 记录典型参数中的最好参数和结果
        self.best_params = self.init_params_set[indexes[0]]
        self.best_cost = self.init_costs_set[indexes[0]]
        # 记入记录列表
        self.best_params_list = np.vstack(
            (self.best_params_list, self.best_params))
        self.best_costs_list = np.hstack(
            (self.best_costs_list, self.best_cost))
        # 更新档案
        self.archive.update({'last_iteration': self.last_iteration})
        # 加载神经网络
        self.net = neuralnet.NeuralNet(self.min_boundary,
                                       self.max_boundary,
                                       archive_dir=self.archive_dir,
                                       start_datetime=self.start_datetime)
        self.net.load(self.archive, self.load_neural_net_archive_filename)
        # 存档
        self._save_archive()
Esempio n. 26
0
def predict(x, labels, params, sizeh, normalize):
    """
    predict an output with a given row. Prints the index of the prediction of the output row.
    
    Arguments:\n
        [x] the file that holds the 1 * n row example that should be predicted  \n
        [labels] the size of the output layer that the parameters were trained on \n
        [params] the file that holds a 1 * n rolled parameter vector \n
    """

    x = np.loadtxt(x, delimiter=",", skiprows=0, dtype="float")
    x = x[np.newaxis]
    nn = neuralnet.NeuralNet(X=None, Y=None, writer=writer, norm=normalize)

    input_size = np.shape(x)[1]
    hidden_size = input_size
    if sizeh:
        hidden_size = sizeh

    nn.set_hidden_size(input_size)
    nn.set_hidden_size(hidden_size)
    nn.set_num_labels(labels)
    nn.set_params(np.loadtxt(params, delimiter=",", skiprows=0, dtype="float"))
    print nn.predict(x)
Esempio n. 27
0
                                imageWidth,
                                imageHeight,
                                fill="white",
                                outline="white")
        draw.rectangle([0, 0, imageWidth, imageHeight],
                       fill=white,
                       outline=white)

    def handleKey(self, event):
        if repr(event.char) == CTRLA:
            self.clearCanvas()


#Load neural network
t = time.time()
print "Loading neural network..."
net = neuralnet.NeuralNet()
net.LoadData(netName)
print "Done!",
print "Loaded in ", time.time() - t, "seconds.."

root = Tk()
root.title("Digit Recognition")
root.resizable(0, 0)

uiFrame = Demo(root)
uiFrame.pack()

root.bind("<Control-Key>", uiFrame.handleKey)
root.mainloop()
Esempio n. 28
0
import neuralnet as n
import numpy as np

net = n.NeuralNet([2, 4, 1])

net.train(np.array([[0, 1]]), np.array([[1]]))
Esempio n. 29
0
def f(x):
    return (0.5 * np.sin(x)) + 0.5, (0.3 * np.cos(x)) + 0.3


# create train samples
xtrain = np.linspace(-7, 7, numTrain).reshape(numTrain, 1)
ytrain = np.zeros([numTrain, 2])
ytrain[:, 0] = f(xtrain)[0].squeeze()
ytrain[:, 1] = f(xtrain)[1].squeeze()

# Instantiate neural network with 2 layers.
# The hidden layer has 5 neurons, with 1 output.
# assumes full connectivity between layer neurons.
# uses sigmoid activation function by default at each layer.
net = nn.NeuralNet([1, 100, 50, 2],
                   actFunc=[act.Sigmoid(),
                            act.Sigmoid(),
                            act.ArcTan()])

# hire a trainer for the network
trainer = nn.Trainer(net, 'SSE', 25, xtrain, ytrain)

# train using BFGS and sum of squares error
#optArgs = {'gtol': 1e-6, 'maxiter': 250}
#trainer.trainBFGS(**optArgs)

optArgs = {
    'bounds': None,
    'm': 1000,
    'factr': 1e7,
    'pgtol': 1e-02,
    'iprint': 1,
if __name__ == '__main__':
	datasets_dict = load_mnist('')

	# print each dataset's shape
	for dataset_type in datasets_dict:
		print(dataset_type, datasets_dict[dataset_type].shape)
	print('\n')

	training_data = datasets_dict['train']
	validation_data = datasets_dict['validation']
	test_data = datasets_dict['test']

	#_display_digits(datasets_dict)
	#_display_digits(datasets_dict, plot_type='same_digit', digit=8)
	#_display_digits_distrib(datasets_dict)

	# transform each dataframe into a numpy.ndarray to be used by the neural net
	training_data_list = _df_to_ndarray(training_data, dataset_type='training')
	test_data_list = _df_to_ndarray(test_data, dataset_type='test')
	validation_data_list = _df_to_ndarray(validation_data, dataset_type='validation')

	nn = neuralnet.NeuralNet([784, 30, 10])
	nn._stochastic_gd(training_data_list, 30, 10, 1.0, validation_data=validation_data_list)

	n_test = len(validation_data)
	test_data_eval = nn.evaluate(test_data_list)
	print('evaluation for the test dataset: {} / {}'.format(test_data_eval, n_test))

	#The values 0.1307 and 0.3081 are the global mean and standard deviation of the MNIST dataset