示例#1
0
    def __init__(self, position):
        self.position = position
        self.rotation = random.random() * 2 * math.pi
        self.bearing = Vector(math.cos(self.rotation), math.sin(self.rotation))

        self.neural_net = NeuralNet(4, 1, 10, 3)
        self.fitness = 0
示例#2
0
    def __init__(self, position, usage, speed, angle):
        self.position = position
        self.rotation = angle
        self.bearing = Vector(math.cos(self.rotation), math.sin(self.rotation))

        self.neural_net = NeuralNet(5, 1, 10, 1)

        self.usage = usage
        self.speed = speed

        self.reset()
示例#3
0
 def toggle(self):
     if self.toggle_btn.config('relief')[-1] == SUNKEN:
         self.toggle_btn.config(text="Learning", relief=RAISED)
         self.neural_net = None
     else:
         self.toggle_btn.config(text="Testing", relief=SUNKEN)
         self.neural_net = NeuralNet(
             "data.txt",
             algorithm=self.controller.learning_algorithm,
             layer_arch=self.controller.architecture,
             learning_rate=self.controller.learning_rate,
             max_iter=self.controller.max_iter)
         self.neural_net.learn()
示例#4
0
    def __init__(self, position):
        self.position = position
        self.rotation = random.random() * 2 * math.pi
        self.bearing = Vector(math.cos(self.rotation), math.sin(self.rotation))

        self.neural_net = NeuralNet(4,1,10,3)
        self.fitness = 0
示例#5
0
def the_method():
    if request.headers['Content-Type'] == 'text/plain':
        return "Text Message: " + request.data

    elif request.headers['Content-Type'] == 'application/json':
        return "JSON Message: " + json.dumps(request.json)

    elif request.headers['Content-Type'] == 'application/octet-stream':
        print(request.data)
        f = open('./binary', 'wb')
        f.write(request.data)
        f.close()
        return "Binary message written!"
    else:
        f = request.files['binarydata']
        f.seek(0)
        my_bytes = f.read()

        index = 0
        matrix = []
        row = []
        for byte in my_bytes:
            normalized = (float(byte) - 0) / (255 - 0)
            row.append(normalized)
            if (index % row_size == (row_size - 1)):
                matrix.append(row)
                row = []
            index = index + 1
        np_array = np.array([matrix], dtype=np.float64)

        print(np_array[0].shape)
        #print("np_array[0]. Type : {} | Value : {}".format(type(np_array[0][0][0]),np_array[0][0][0]))

        neural_network = NeuralNet(row_size, row_size)
        neural_network.load_model('StoredModel.modelconfig')

        arg_max, max_val = neural_network.predict_element(np_array)
        lb = labels_map[arg_max]
        print('ArgMax : {} | MaxVal : {} |Label : {}'.format(
            arg_max, max_val, lb))

        result = {
            'Confidence': max_val.item(),
            'Predicted': arg_max.item(),
            'Label': lb
        }
        return json.dumps(result)
示例#6
0
def train():
    neural_network = NeuralNet(row_size, row_size)
    neural_network.build_layers()

    (training_data, training_labels,
     label_mapper) = load_from_dataset('DS', row_size, row_size)
    #(training_data , training_labels ) = prepare_data('Datasets/DatasetSample0/Images',labels_dictionary,row_size,row_size)

    neural_network.fit_data(training_data, training_labels, 10, 0.998)
    neural_network.save_model('StoredModel')

    return "Finished Fitting/Saving Model"
示例#7
0
def make_population(size,
                    input_size,
                    output_size,
                    hidden_layer_size=3,
                    numof_hidden_layers=1):
    population = []
    for i in range(size):
        population.append(
            NeuralNet(input_size, output_size, hidden_layer_size,
                      numof_hidden_layers))
    return population
示例#8
0
    def __init__(self, rows, cols, brain = None):
        self.rows, self.cols = rows, cols

        self.board = np.zeros((rows, cols), dtype = np.uint8)
        self.board[:, 0] = self.board[:, -1] = 1
        self.board[0, :] = self.board[-1, :] = 1

        self.board[rows // 2, cols // 2] = 2
        self.snake = [Vector(cols // 2, rows // 2)]

        self.add_food()

        self.dir = Vector(1, 0)
        self.score = 0
        self.age = 0
        self.health = 100

        if brain is None:
            self.brain = NeuralNet(rows * cols + 1, rows * cols // 2, 4)
        else:
            self.brain = brain.copy()
示例#9
0
class Tank(object):
    def __init__(self, position):
        self.position = position
        self.rotation = random.random() * 2 * math.pi
        self.bearing = Vector(math.cos(self.rotation), math.sin(self.rotation))

        self.neural_net = NeuralNet(4, 1, 10, 3)
        self.fitness = 0

    def update(self, mines):

        mines = sorted(mines, key=lambda m: m.position.distance(self.position))

        # closest mine
        closest_mine = mines[0]

        # look_at vector
        look_at = Vector(self.position.x - closest_mine.position.x,
                         self.position.y - closest_mine.position.y)
        look_at.normalize()

        outputs = self.neural_net.update(
            [look_at.x, look_at.y, self.bearing.x, self.bearing.y])

        # rotation
        self.rotation += (outputs[0] - outputs[1]) * 0.01

        # speed
        self.speed = outputs[2]

        # calcuate new bearing
        degrees = self.rotation * 180 / math.pi
        self.bearing.x = -math.sin(degrees)
        self.bearing.y = math.cos(degrees)

        # calculate new position
        self.position.x += self.bearing.x * self.speed
        self.position.y += self.bearing.y * self.speed

        if self.position.x > WIDTH: self.position.x = 0
        if self.position.x < 0: self.position.x = WIDTH

        if self.position.y > HEIGHT: self.position.y = 0
        if self.position.y < 0: self.position.y = HEIGHT

        return closest_mine

    def __repr__(self):
        return 'Tank(position=[%s], bearing=[%s], fitness=%s)'\
                % (self.position, self.bearing, self.fitness)
示例#10
0
class Tank(object):
    def __init__(self, position):
        self.position = position
        self.rotation = random.random() * 2 * math.pi
        self.bearing = Vector(math.cos(self.rotation), math.sin(self.rotation))

        self.neural_net = NeuralNet(4,1,10,3)
        self.fitness = 0

    def update(self, mines):

        mines = sorted(mines, key = lambda m: m.position.distance(self.position))

        # closest mine
        closest_mine = mines[0]

        # look_at vector
        look_at = Vector(self.position.x - closest_mine.position.x,
                         self.position.y - closest_mine.position.y)
        look_at.normalize()

        outputs = self.neural_net.update([look_at.x, look_at.y,
                                          self.bearing.x, self.bearing.y])

        # rotation
        self.rotation += (outputs[0] - outputs[1]) * 0.01

        # speed
        self.speed = outputs[2]

        # calcuate new bearing
        degrees = self.rotation * 180 / math.pi
        self.bearing.x = -math.sin(degrees)
        self.bearing.y = math.cos(degrees)

        # calculate new position
        self.position.x += self.bearing.x * self.speed
        self.position.y += self.bearing.y * self.speed

        if self.position.x > WIDTH: self.position.x = 0
        if self.position.x < 0: self.position.x = WIDTH

        if self.position.y > HEIGHT: self.position.y = 0
        if self.position.y < 0: self.position.y = HEIGHT

        return closest_mine

    def __repr__(self):
        return 'Tank(position=[%s], bearing=[%s], fitness=%s)'\
                % (self.position, self.bearing, self.fitness)
示例#11
0
class Board:
    def __init__(self, rows, cols, brain = None):
        self.rows, self.cols = rows, cols

        self.board = np.zeros((rows, cols), dtype = np.uint8)
        self.board[:, 0] = self.board[:, -1] = 1
        self.board[0, :] = self.board[-1, :] = 1

        self.board[rows // 2, cols // 2] = 2
        self.snake = [Vector(cols // 2, rows // 2)]

        self.add_food()

        self.dir = Vector(1, 0)
        self.score = 0
        self.age = 0
        self.health = 100

        if brain is None:
            self.brain = NeuralNet(rows * cols + 1, rows * cols // 2, 4)
        else:
            self.brain = brain.copy()

    @property
    def fitness(self):
        fitness = self.age * self.score * self.score
        if len(self.snake) < 10:
            return int(fitness * np.exp2(len(self.snake)))
        return int(fitness * np.exp2(10) * (len(self.snake) - 9))

    @property
    def alive(self):
        return self.health > 0
    
    def copy(self):
        return Board(self.rows, self.cols, brain = self.brain)
    
    def crossover(self, partner):
        brain = self.brain.crossover(partner.brain)
        return Board(self.rows, self.cols, brain = brain)
    
    def mutate(self, mutation_rate):
        self.brain.mutate(mutation_rate)
        return self

    def add_food(self):
        row = int(np.random.random() * self.rows)
        col = int(np.random.random() * self.cols)
        if self.board[row, col] == 0:
            self.board[row, col] = 3
        else:
            self.add_food()

    def move(self, dir):
        self.age += 1
        self.health -= 1

        curr, next = self.snake[-1], self.snake[-1] + dir
        next_val = self.board[int(next.y), int(next.x)]
        # Empty
        if next_val == 0:
            pos = self.snake.pop(0)
            self.board[int(pos.y), int(pos.x)] = 0
        # Wall
        elif next_val == 1:
            self.health = 0
            return
        # Snake
        elif next_val == 2:
            self.health = 0
            return
        # Food
        elif next_val == 3:
            self.score += 1
            self.add_food()
            self.health += self.health // 3

        self.snake.append(next)
        self.board[int(next.y), int(next.x)] = 2

    def update(self):
        inputs = (self.board.astype(float) / 3.).flatten()
        inputs = np.append(inputs, [self.dir.heading()])
        decision = self.brain.output(inputs)
        decision = np.argmax(decision)
        # Up
        if decision == 0:
            self.dir = Vector(0, -1)
        # Down
        elif decision == 1:
            self.dir = Vector(0, 1)
        # Up
        elif decision == 2:
            self.dir = Vector(-1, 0)
        # Down
        elif decision == 3:
            self.dir = Vector(1, 0)

        self.move(self.dir)

    def show(self, size = Vector(500, 500), pos = Vector(300, 0)):
        _size = size // [self.cols, self.rows]

        engine.stroke = 0
        for index, value in np.ndenumerate(self.board):
            # Empty
            if value == 0:
                engine.fill = None
            # Wall
            elif value == 1:
                engine.fill = 128
            # Snake
            elif value == 2:
                engine.fill = 255
            # Food
            elif value == 3:
                engine.fill = 255, 0, 0

            _pos = pos + _size * index[::-1]
            engine.rect(_pos, _size)
示例#12
0
class Plane(object):
    def __init__(self, position, usage, speed, angle):
        self.position = position
        self.rotation = angle
        self.bearing = Vector(math.cos(self.rotation), math.sin(self.rotation))

        self.neural_net = NeuralNet(5, 1, 10, 1)

        self.usage = usage
        self.speed = speed

        self.reset()

    def reset(self):
        self.fitness = 0
        self.beam = None
        self.th = 0
        self.action = 0

    def update(self, beams):

        beams = sorted([
            b for b in beams
            if ((self.beam and b.load < self.beam.load) or self.beam == None)
            and b != self.beam
            and b.position.distance(self.position) <= b.radius
        ],
                       key=lambda b: b.load)

        # other beam
        next_beam = beams[0] if beams else None

        if self.beam and abs(self.beam.position.distance(
                self.position)) >= self.beam.radius:
            self.beam.load -= self.usage * 1.0 / self.beam.capacity
            self.beam = None
            if next_beam:
                self.th = 0

        sigmoid = lambda x, x0, a, b: a - a / (1 + np.exp(-b * (x - x0)))
        perf = lambda x: np.round(sigmoid(x, 0.9, 1.0, 3.0), 2)

        self.lc = self.beam.load if self.beam else 1

        self.ln = next_beam.load if next_beam else 1

        self.ld = perf(self.ln) - perf(self.lc)

        # current beam
        self.fc = 1 if self.beam else -1

        # next beam
        self.fn = 1 if next_beam else -1

        # time since handover
        if self.beam:
            self.th += 1
        max_th = 10
        self.th = min([self.th, max_th])

        state = [self.ld, self.fc, self.fn, self.th, self.speed]
        outputs = self.neural_net.update(state)
        self.action = outputs[0]

        if self.action < 0.5:
            # handover
            if self.beam:
                self.beam.load -= self.usage * 1.0 / self.beam.capacity
                if next_beam:
                    # minimize handovers
                    penalty = 2 * max([0, (max_th - self.th + 1)])
                    if next_beam.load < self.beam.load:
                        penalty *= 0.1
                    self.fitness -= penalty

            self.th = 0
            if next_beam:
                self.beam = next_beam
                self.beam.load += self.usage * 1.0 / self.beam.capacity
            else:
                if self.beam:
                    self.beam = None
                    self.fitness -= 50
                else:
                    # no current beam
                    pass
        else:
            if self.beam and next_beam:
                if self.beam.load > next_beam:
                    self.fitness -= 0.5 * self.th

        if self.beam:
            fitness = 100 * np.power(perf(self.beam.load), 1)
            self.fitness += fitness

        # calculate new position
        self.position.x += self.bearing.x * self.speed
        self.position.y += self.bearing.y * self.speed

        if self.position.x > WIDTH: self.position.x = 0
        if self.position.x < 0: self.position.x = WIDTH

        if self.position.y > HEIGHT: self.position.y = 0
        if self.position.y < 0: self.position.y = HEIGHT

        return self.beam

    def __repr__(self):
        return 'Beam(position=[%s], bearing=[%s], fitness=%s)'\
                % (self.position, self.bearing, self.fitness)
示例#13
0
from neural import NeuralNet as NN
import pipe
from paths import *

data = pipe.read_data(p2_wdbc, ['B', 'M'])
data.select(int(data.count * 0.6))
nnet = NN(30, 2)
nnet.train(data)
nnet.test(data)
示例#14
0
def main():
    f = "/Users/gabriel/Desktop/instances.txt"
    base_code = ord("A")
    with open(f) as f:
        lines = f.readlines()
    s = int(lines[0]) * int(lines[1])
    n = int(lines[2])
    l = 4

    instances = list()

    while n > 0:
        n -= 1
        pattern = np.zeros((1, s))
        pattern_class = np.zeros((1, int(lines[2])))
        pt_index = 0
        inst = Instance()
        for i in range(int(lines[1])):
            for c in lines[l].strip():
                pattern[0][pt_index] = 1 if c == "#" else 0
                pt_index += 1
            l += 1
        pattern_class[0][ord(lines[l].strip()) - base_code] = 1
        l += 1
        inst.attributes = pattern
        inst.output_values = pattern_class
        inst.normalize()
        instances.append(inst)

    ann = NeuralNet([35, 35, 26], True)
    ann.learning_rate = 0.5
    ann.momentum = 0.4
    ann.instances(instances)
    ann.train(55)

    correct = 0
    for i in instances:
        ann.instance(i)
        ann.feed_forward()
        f = chr(65 + np.argmax(i.output_values))
        print "F={}".format(f)
        f_hat = chr(65 + np.argmax(ann.output))
        print "F'={}".format(f_hat)

        if f == f_hat:
            correct += 1
        else:
            print "ERROR!"
        print
    print str(correct / float(len(instances)) * 100) + " %"
示例#15
0
import numpy as np
import sys
from neural import NeuralNet
from dataset import get_all_categories_shuffled, numpy_array_from_file, prepare_data

labels_dictionary = {'Circle': 0, 'L': 1, 'RightArrow': 2}
labels_map = ['Circle', 'L', 'RightArrow']

#Replace with your root folder that holds the dataset of images
all_bytes, all_labels = prepare_data("Datasets/DatasetSample0/Images",
                                     labels_dictionary, 56, 56)

nn = NeuralNet(56, 56)
nn.build_layers()
nn.fit_data(training_data=all_bytes,
            training_labels=all_labels,
            epochs=10,
            accuracy=0.999)
nn.save_model('easter_egg_ahlabikyafraise_')
#nn.load_model('easter_egg_ahlabikyafraise_')

image_path = sys.argv[1]
test_image = numpy_array_from_file(image_path)
test_image = test_image / 255

arg_max, prediction_level = nn.predict_element(test_image)
print('arg_max : {} which is {} with prediction : {}'.format(
    arg_max, labels_map[arg_max], prediction_level))
    ([1, 0, 1, 1, 0, 0], [1]),
    ([1, 0, 1, 0, 1, 0], [1]),
    ([1, 1, 0, 0, 1, 1], [1]),
    ([1, 1, 1, 1, 0, 0], [1]),
    ([1, 0, 0, 0, 1, 1], [1]),
    ([1, 0, 0, 0, 1, 0], [0]),
    ([0, 1, 1, 1, 0, 1], [1]),
    ([0, 1, 1, 0, 1, 1], [0]),
    ([0, 0, 0, 1, 1, 0], [0]),
    ([0, 1, 0, 1, 0, 1], [0]),
    ([0, 0, 0, 1, 0, 1], [0]),
    ([0, 1, 1, 0, 1, 1], [0]),
    ([0, 1, 1, 1, 0, 0], [0]),
]

nn = NeuralNet(6, 1, 1)
nn.train(rn_training_data)

print(nn.get_ih_weights())
print()
print(nn.get_ho_weights())

print(nn.evaluate([1, 1, 1, 1, 1, 1]))
print(nn.evaluate([0, 0, 0, 0, 0, 0]))
print()
print(nn.evaluate([1, 0, 0, 0, 0, 0]))  # 1
print(nn.evaluate([0, 1, 0, 0, 0, 0]))  # 0
print(nn.evaluate([0, 0, 1, 0, 0, 0]))  # 0
print(nn.evaluate([0, 0, 0, 1, 0, 0]))  # 0
print(nn.evaluate([0, 0, 0, 0, 1, 0]))  # 0
print(nn.evaluate([0, 0, 0, 0, 0, 1]))  # 0
    out = np.zeros((1, 26))
    out[0, ord(letter) - 65] = 1

    element = np.delete(element, 0)
    element = element.astype(np.float)
    element = element.reshape((1, element.shape[0]))

    inst = Instance()
    inst.attributes = element
    inst.output_values = out
    inst.normalize()

    instances.append(inst)
print "...Ready!"
print "Instantiate neuralnet..."
ann = NeuralNet([16, 32, 26], True)
ann.learning_rate = 0.09
ann.momentum = 0.44
ann.instances(instances)
print "...Ready!"
print "Generate model..."
ann.train(50, True)
print "...Ready!"

print ann

correct = 0
for i in instances:
    ann.instance(i)
    ann.feed_forward()
    f = chr(65 + np.argmax(i.output_values))
示例#18
0
class MainPage(Frame):
    def __init__(self, parent, controller):
        super(MainPage, self).__init__(parent)

        self.controller = controller
        self.neural_net = None
        self.current_gesture = []

        self.canvas = Canvas(self, width=800, height=500)
        self.canvas.pack(expand=YES, fill=BOTH)

        self.canvas.bind("<B1-Motion>", self.paint)
        self.canvas.bind("<ButtonRelease-1>", self._on_release)

        self.class_label = Label(self, text="Class:")
        self.class_label.pack(padx=15)
        self.slider = Scale(self,
                            from_=1,
                            to=5,
                            tickinterval=1,
                            orient=HORIZONTAL)
        self.slider.pack(padx=15, pady=5)

        self.toggle_btn = Button(self,
                                 text="Learning",
                                 relief=RAISED,
                                 command=self.toggle)
        self.toggle_btn.pack(padx=15, pady=5)

        self.button_options = Button(
            self,
            text="Options",
            command=lambda: controller.show_frame(OptionsPage))
        self.button_options.pack(padx=15, pady=5)

        self.button_clear_file = Button(self,
                                        text="Clear file",
                                        command=self.clear_file)
        self.button_clear_file.pack(padx=15, pady=5)

        self.button_quit = Button(self, text="Quit", command=parent.quit)
        self.button_quit.pack(padx=15, pady=5)

    def toggle(self):
        if self.toggle_btn.config('relief')[-1] == SUNKEN:
            self.toggle_btn.config(text="Learning", relief=RAISED)
            self.neural_net = None
        else:
            self.toggle_btn.config(text="Testing", relief=SUNKEN)
            self.neural_net = NeuralNet(
                "data.txt",
                algorithm=self.controller.learning_algorithm,
                layer_arch=self.controller.architecture,
                learning_rate=self.controller.learning_rate,
                max_iter=self.controller.max_iter)
            self.neural_net.learn()

    def paint(self, event):
        x1, y1 = event.x + 1, event.y + 1
        x2, y2 = event.x - 1, event.y - 1
        self.canvas.create_oval(x1, y1, x2, y2)
        self.current_gesture.append([event.x, event.y])

    def _on_release(self, event):
        self.canvas.delete("all")

        data = self.transform_data()
        total_distance = 0
        # print(data)

        for first, second in zip(data, data[1:]):
            total_distance += np.linalg.norm(first - second)
        # print(total_distance)

        representatives = [data[0]]
        dist = 0
        k = 1
        for i in range(1, len(data)):
            dist += np.linalg.norm(data[i] - data[i - 1])
            if dist >= k * total_distance / (self.controller.M - 1):
                representatives.append(data[i])
                k += 1
        while len(representatives) < self.controller.M:
            representatives.append(data[len(data) - 1])

        if self.neural_net is None:
            self.store_data(representatives)

        else:
            inputs = []
            for pair in representatives:
                for elem in pair:
                    inputs.append(elem)

            index_class, alfabet_class = self.neural_net.get_class(inputs)
            self.slider.set(index_class + 1)
            print(alfabet_class)

        self.current_gesture = []

    def transform_data(self):
        vector = np.array(self.current_gesture)
        centroid = np.mean(vector, axis=0)
        translated = vector - centroid
        transformer = np.max(np.abs(translated), axis=1)
        scaled = translated / transformer.reshape(-1, 1)
        return scaled

    def store_data(self, representatives):
        classes = np.zeros(5)
        data_class = self.slider.get()
        classes[data_class - 1] = 1
        data = np.array(representatives)
        with open("data.txt", "a") as f:
            for point in data:
                for coordinate in point:
                    f.write(str(coordinate) + ' ')
            for c in classes:
                f.write(str(c) + ' ')
            f.write("\n")

    def clear_file(self):
        with open("data.txt", "w") as f:
            f.write("")