def setUp(self):

        self.red = NeuralNetwork(2, [1, 1])

        self.layer = NeuralLayer(2, 1)

        self.layer2 = NeuralLayer(1, 1)

        self.neuron = Neuron(2)

        self.neuron2 = Neuron(1)

        self.red2 = NeuralNetwork(2, [2, 2])

        self.lay1 = NeuralLayer(2, 2)

        self.lay2 = NeuralLayer(2, 2)

        self.neur1 = Neuron(2)

        self.neur2 = Neuron(2)

        self.neur3 = Neuron(2)

        self.neur4 = Neuron(2)
 def __init__(self, nEpochs=500, nInputs=8, nNeurCapas=[8, 19]):
     self.red = NeuralNetwork(nInputs, nNeurCapas)
     self.lineList = trainReader()
     self.outputList = []
     shuffle(self.lineList)
     self.outlist(self.lineList, self.outputList)
     # self.datatrain(nEpochs)
     print("Funciono Entrenamiento: CheckPoint 1")
     self.dataList = dataReader()
     self.dataOut = []
     self.outlist(self.dataList, self.dataOut)
     self.curveTraining()
Exemplo n.º 3
0
    def learn_network(self):
        count_neurons1 = self.count_neurons_l1_spinbox.value()
        count_neurons2 = self.count_neurons_l2_spinbox.value()

        count_inputs_n1 = self.neuron_l1_count_inputs_spinbox.value()
        count_component_input_n1 = self.neuron_l1_count_input_components_spinbox.value(
        )
        count_inputs_n2 = self.neuron_l2_count_inputs_spinbox.value()

        self.ours = FileReader.read_matrix_files(self.ours_paths,
                                                 count_inputs_n1,
                                                 count_component_input_n1)
        self.aliens = FileReader.read_matrix_files(self.aliens_paths,
                                                   count_inputs_n1,
                                                   count_component_input_n1)

        self.network = nn.NeuralNetwork(h=count_inputs_n1,
                                        g=count_inputs_n2,
                                        components=count_component_input_n1,
                                        n1=count_neurons1,
                                        n2=count_neurons2)

        self.network.learn(self.k1, self.k2, self.ours, self.aliens)

        QMessageBox.about(None, "Learn neural network", "Done!")
Exemplo n.º 4
0
 def __init__(self, nTrain=1000, nInputs=2, nNeurCapas=(2, 3, 1)):
     self.Network = NeuralNetwork(nInputs, nNeurCapas)
     self.Inputs = []
     self.Outputs = []
     I1 = [0, 0]
     I2 = [0, 1]
     I3 = [1, 0]
     I4 = [1, 1]
     for i in range(nTrain):
         self.Inputs.append(I1)
         self.Inputs.append(I2)
         self.Inputs.append(I3)
         self.Inputs.append(I4)
         self.Outputs.append([xor(I1[0], I1[1])])
         self.Outputs.append([xor(I2[0], I2[1])])
         self.Outputs.append([xor(I3[0], I3[1])])
         self.Outputs.append([xor(I4[0], I4[1])])
     self.Network.realTraining(self.Inputs, self.Outputs)
     self.Network.feed([0, 0])
     print(self.Network.getOutput())
Exemplo n.º 5
0
 def __init__(self, name, map):
     super(Animal, self).__init__(name)
     self.map = map
     self.brain = NeuralNetwork(10, 9, 5)
     self.x_pos = random.randint(0, self.map.MAPWIDTH - 1)
     self.y_pos = random.randint(0, self.map.MAPHEIGHT - 1)
     self.hunger = 0
     self.tiredness = 0
     self.distance_x = 0
     self.distance_y = 0
     self.fitness = 0
     self.rect = self.image.get_rect()
Exemplo n.º 6
0
    def load_network(self):
        fileName = self.__openFileNameDialog("Network config file (*.json)")

        if fileName:
            with open(fileName, 'r') as file_in:
                file_text = file_in.read()
                json_obj = json.loads(file_text)
                json_obj['mu'] = np.array(json_obj['mu'])
                json_obj['W'] = np.array(json_obj['W'])

                self.network = nn.NeuralNetwork()
                self.network.__dict__ = json_obj

            QMessageBox.about(None, "Loading neural network", "Done!")
Exemplo n.º 7
0
from Network import NeuralNetwork
import random
import numpy as np
import matplotlib.pyplot as plt
import cv2

input_nodes = 9
hidden_nodes = 5
output_nodes = 2
learning_rate = 0.2

n = NeuralNetwork(input_nodes, hidden_nodes, output_nodes, learning_rate)

ppi = np.array([[0.3, 0.3]])

epochs = 1111

for i in range(epochs):
    a = np.random.permutation(18)
    for pro in a:
        number = pro
        # number = random.randint(0, 17)
        if number == 1 or number == 2 or number == 3:
            rightAnswers = np.array([[1, 0]])
        elif number == 4 or number == 5 or number == 6:
            rightAnswers = np.array([[0, 1]])
        elif 6 < number < 16 or number == 17:
            rightAnswers = np.array([[1, 1]])
        elif number == 0 or number == 16:
            rightAnswers = np.array([[0, 0]])
class dataSet:
    def __init__(self, nEpochs=500, nInputs=8, nNeurCapas=[8, 19]):
        self.red = NeuralNetwork(nInputs, nNeurCapas)
        self.lineList = trainReader()
        self.outputList = []
        shuffle(self.lineList)
        self.outlist(self.lineList, self.outputList)
        # self.datatrain(nEpochs)
        print("Funciono Entrenamiento: CheckPoint 1")
        self.dataList = dataReader()
        self.dataOut = []
        self.outlist(self.dataList, self.dataOut)
        self.curveTraining()

    def outlist(self, lineList, outList):
        for i in lineList:
            zerolist = [0] * 19
            zerolist[i.pop(0)] = 1
            outList.append(zerolist)

    def datatrain(self, nEpoch):
        for j in range(nEpoch):
            #start = time.time()
            self.red.realTraining(self.lineList, self.outputList)
            #end = time.time()
            #print(end - start)

    def curveTraining(self):
        totallineas = len(self.dataList)
        contpres = 0.0
        error = 0
        xaprendizaje = []
        yaprendizaje = []
        xerror = []
        yerror = []
        for k in range(7):
            for l in range(len(self.dataList)):
                self.red.feed(self.dataList[l])
                for m in range(len(self.red.output)):
                    absdif = abs(self.dataOut[l][m] - self.red.output[m])
                    if absdif < 0.5:
                        contpres += 1
                    error += absdif**2
                contpres /= 19
            contpres /= totallineas
            xaprendizaje.append(k)
            yaprendizaje.append(contpres)
            contpres = 0
            xerror.append(k)
            yerror.append(error)
            error = 0
            self.datatrain(k * 100 + 100)
        plt.figure(1)
        plt.subplot(211)
        plt.xlabel("Numero de Epochs")
        plt.ylabel("Cura de Aprendizaje")
        plt.plot(xaprendizaje, yaprendizaje, 'b-')
        plt.subplot(212)
        plt.xlabel("Numero de Epochs")
        plt.ylabel("Error")
        plt.plot(xerror, yerror, 'r-')
        plt.show()
class NetworkTests(unittest.TestCase):
    def setUp(self):

        self.red = NeuralNetwork(2, [1, 1])

        self.layer = NeuralLayer(2, 1)

        self.layer2 = NeuralLayer(1, 1)

        self.neuron = Neuron(2)

        self.neuron2 = Neuron(1)

        self.red2 = NeuralNetwork(2, [2, 2])

        self.lay1 = NeuralLayer(2, 2)

        self.lay2 = NeuralLayer(2, 2)

        self.neur1 = Neuron(2)

        self.neur2 = Neuron(2)

        self.neur3 = Neuron(2)

        self.neur4 = Neuron(2)

    # testcase entregado en ucursos

    def testCase1(self):
        self.neuron.custom([0.4, 0.3], 0.5)

        self.neuron2.custom([0.3], 0.4)

        self.layer.custom([self.neuron])

        self.layer2.custom([self.neuron2])

        self.red.custom([self.layer, self.layer2])

        self.red.train([1, 1], [1])

        self.assertEqual(round(self.neuron.weight[0], 15), 0.402101508999489)

        self.assertEqual(round(self.neuron.weight[1], 15), 0.302101508999489)

        self.assertEqual(round(self.neuron2.weight[0], 15), 0.330262548639919)

        self.assertEqual(round(self.neuron.bias, 15), 0.502101508999489)

        self.assertEqual(round(self.neuron2.bias, 15), 0.439377453127974)

    def testCase2(self):
        self.neur1.custom([0.7, 0.3], 0.5)

        self.neur2.custom([0.3, 0.7], 0.4)

        self.neur3.custom([0.2, 0.3], 0.3)

        self.neur4.custom([0.4, 0.2], 0.6)

        self.lay1.custom([self.neur1, self.neur2])

        self.lay2.custom([self.neur3, self.neur4])

        self.red2.custom([self.lay1, self.lay2])

        self.red2.train([1, 1], [1, 1])

        self.assertEqual(round(self.neur1.weight[0], 15), 0.702510448549328)

        self.assertEqual(round(self.neur1.weight[1], 15), 0.302510448549328)

        self.assertEqual(round(self.neur2.weight[0], 15), 0.302498011357483)

        self.assertEqual(round(self.neur2.weight[1], 15), 0.702498011357483)

        self.assertEqual(round(self.neur3.weight[0], 15), 0.229947378819557)

        self.assertEqual(round(self.neur3.weight[1], 15), 0.329383628639501)

        self.assertEqual(round(self.neur4.weight[0], 15), 0.419430056526462)

        self.assertEqual(round(self.neur4.weight[1], 15), 0.219064291698386)

        self.assertEqual(round(self.neur1.bias, 15), 0.502510448549328)

        self.assertEqual(round(self.neur2.bias, 15), 0.402498011357483)

        self.assertEqual(round(self.neur3.bias, 15), 0.336629542251590)

        self.assertEqual(round(self.neur4.bias, 15), 0.623765488150905)
Exemplo n.º 10
0
class Animal(Sprite):



    def __init__(self, name, map):
        super(Animal, self).__init__(name)
        self.map = map
        self.brain = NeuralNetwork(10, 9, 5)
        self.x_pos = random.randint(0, self.map.MAPWIDTH - 1)
        self.y_pos = random.randint(0, self.map.MAPHEIGHT - 1)
        self.hunger = 0
        self.tiredness = 0
        self.distance_x = 0
        self.distance_y = 0
        self.fitness = 0
        self.rect = self.image.get_rect()


# Think Outputs
#--------------------------------------------------------
    def move_up(self):
        if self.valid_move("up"):
            self.y_pos -= 1
        self.eat()

    def move_down(self):
        if self.valid_move("down"):
            self.y_pos += 1
        self.eat()

    def move_right(self):
        if self.valid_move("right"):
            self.x_pos += 1
        self.eat()

    def move_left(self):
        if self.valid_move("left"):
            self.x_pos -= 1
        self.eat()

    def sleep(self):
        self.tiredness -= 10
        self.hunger += 5

    def valid_move(self, dir):
        if self.y_pos >= 0 and self.y_pos < (self.map.MAPHEIGHT - 1) \
            and self.x_pos >= 0 and self.x_pos < (self.map.MAPWIDTH - 1):
            if (dir == "left" and self.map.Tiles[self.y_pos][self.x_pos - 1].__str__() == 'GRASS')\
                or (dir == "right" and self.map.Tiles[self.y_pos][self.x_pos + 1].__str__() == 'GRASS')\
                or (dir == "down" and self.map.Tiles[self.y_pos + 1][self.x_pos].__str__() == 'GRASS')\
                or (dir == "up" and self.map.Tiles[self.y_pos - 1][self.x_pos].__str__() == 'GRASS'):
                return True

        return False

    def eat(self):
        if self.map.Tiles[self.y_pos][self.x_pos].is_occupied:
            self.hunger -= 20
            self.fitness += 25
            p = self.map.plants.sprites()
            for eaten in p:
                if eaten.x_pos == self.x_pos and eaten.y_pos == self.y_pos:
                    self.map.plants.remove(eaten)

# Think & Helper Functions
# --------------------------------------------------------------------
    def think(self):
        self.tiredness += 5
        self.hunger += 5

        self.update_fitness()
        self.find_food()

        inputs = self.make_inputs()
        output = self.brain.feedForward(inputs)
        decision = output.index(max(output))
        if decision == 0:
            self.move_up()
        elif decision == 1:
            self.move_down()
        elif decision == 2:
            self.move_right()
        elif decision == 3:
            self.move_left()
        elif decision == 4:
            self.sleep()


    def make_inputs(self):

        inputs = [self.x_pos, self.y_pos, self.hunger, self.tiredness, self.distance_x, self.distance_y]

        if self.y_pos >= 0 and self.y_pos < (self.map.MAPHEIGHT - 1) \
            and self.x_pos >= 0 and self.x_pos < (self.map.MAPWIDTH - 1):
            obstruction_left = (0 if self.map.Tiles[self.y_pos][self.x_pos - 1].__str__() == 'GRASS' else 1)
            obstruction_right = (0 if self.map.Tiles[self.y_pos][self.x_pos + 1].__str__() == 'GRASS' else 1)
            obstruction_up = (0 if self.map.Tiles[self.y_pos + 1][self.x_pos].__str__() == 'GRASS' else 1)
            obstruction_down = (0 if self.map.Tiles[self.y_pos - 1][self.x_pos].__str__() == 'GRASS' else 1)



        else:
            obstruction_left = (1 if self.x_pos == 0 else 0)
            obstruction_right = (1 if self.x_pos == self.map.MAPWIDTH else 0)
            obstruction_up = (1 if self.y_pos == 0 else 0)
            obstruction_down = (1 if self.x_pos == self.map.MAPHEIGHT else 0)

        inputs.append(obstruction_left)
        inputs.append(obstruction_right)
        inputs.append(obstruction_up)
        inputs.append(obstruction_down)
        return inputs

    def update_fitness(self):
        if self.hunger < 25:
            self.fitness += 2
        elif self.hunger < 50:
            self.fitness += 1
        if self.tiredness < 25:
            self.fitness += 2
        elif self.tiredness < 50:
            self.fitness += 1
        if self.hunger & self.tiredness < 25:
            self.fitness += 4
        elif self.hunger & self.tiredness < 50:
            self.fitness += 2

        print("fitness:" + self.fitness.__str__())

    def find_food(self):
        current_distance = - 1
        closest_x = -1
        closest_y = -1
        for plant in self.map.plants:
            plant_x = plant.x_pos
            plant_y = plant.y_pos

            x_dist = plant_x - self.x_pos
            y_dist = plant_y - self.y_pos

            distance = math.sqrt((x_dist * x_dist) + (y_dist * y_dist))

            if distance < current_distance or current_distance == -1:
                closest_x = plant_x
                closest_y = plant_y

        self.distance_x = closest_x
        self.distance_y = closest_y


    def valid_fauna(self):
        if self.map.Tiles[self.y_pos][self.x_pos].__str__() == 'GRASS'\
        and self.map.Tiles[self.y_pos][self.x_pos].is_occupied == False:
            return True
        else:
            return False
Exemplo n.º 11
0
def main():
    # Creating NN
    nn = NeuralNetwork([784, 64, 3])
    # Creating training data
    createTraining(cats_training, "cat")
    createTraining(trains_training, "train")
    createTraining(rainbows_training, "rainbow")
    # Creating test data
    createTest(cats_testing, "cat")
    createTest(trains_testing, "train")
    createTest(rainbows_testing, "rainbow")
    # make one array from all the training and test data
    training_data = cats_training + trains_training + rainbows_training
    testing_data = cats_testing + trains_testing + rainbows_testing
    # Normalize the training data and testing data, (0-1)
    normalizeValues(trains_training)
    normalizeValues(testing_data)
    # arrange the data in specific format that will fit to the NN library i created.
    for j in range(2400):
        for i in range(785):
            training_data[j][i] = [training_data[j][i]]
    for j in range(600):
        for i in range(785):
            testing_data[j][i] = [testing_data[j][i]]
    '''
    training the model with the training data (30 times),
    shuffle the training data every iteration.
    specific target to every label.
    '''
    for i in range(35):
        random.shuffle(training_data)
        for j in range(2400):
            target = [[0], [0], [0]]
            if (training_data[j][784] == ['cat']):
                target[0] = [1]
                nn.train(training_data[j][:-1], target, 1)
            if (training_data[j][784] == ['train']):
                target[1] = [1]
                nn.train(training_data[j][:-1], target, 1)
            if (training_data[j][784] == ['rainbow']):
                target[2] = [1]
                nn.train(training_data[j][:-1], target, 1)
    '''
    Calculate the acc. should be more various of training data.
    currently : 800 cats, 800 rainbows, 800 trains.
    '''
    acc = 0
    trains = 0
    rainbows = 0
    cats = 0
    for i in range(len(testing_data)):
        label = testing_data[i][784]
        guess = nn.feed_forword(testing_data[i][:-1])
        if max(guess) == guess[0] and label == ['cat']:
            acc += 1
            cats += 1
        if max(guess) == guess[1] and label == ['train']:
            acc += 1
            trains += 1
        if max(guess) == guess[2] and label == ['rainbow']:
            acc += 1
            rainbows += 1
    print("Acc : " + str(acc))
    print("length of testing data : " + str(len(testing_data)))
    print("Cats : " + str(cats))
    print("Rainbows : " + str(rainbows))
    print("trains : " + str(trains))
Exemplo n.º 12
0
from Network import NeuralNetwork
import cv2
import numpy as np
import matplotlib.pyplot as plt

n = NeuralNetwork(9, 5, 2, 0.3)

n.synaptic_weight1 = np.load('./weights/example_1.npy')
n.synaptic_weight2 = np.load('./weights/example_2.npy')


image = cv2.imread("dataset/test/test2.0.png", 0)

inputs = image.flatten()
inputs = np.asfarray(inputs / 255.0 * 0.99) + 0.01
answers = n.query(inputs)
print(str(answers[0]) + str(answers[1]))

plt.imshow(image)
plt.xlabel('test2.0.png')
plt.title(str(answers[0]) + '  --  ' + str(answers[1]))
plt.show()


for i in range(18):
    image = cv2.imread("dataset/test/" + str(i) + ".png", 0)

    inputs = image.flatten()
    inputs = np.asfarray(inputs / 255.0 * 0.99) + 0.01
    answers = n.query(inputs)
    print(str(answers[0]) + str(answers[1]))