Beispiel #1
0
    return np.tanh(x);

def act_prime(x):
    return 1-np.tanh(x)**2;

# loss function and its derivative
def loss(y_true, y_pred):
    return np.mean(np.power(y_true-y_pred, 2));

def loss_prime(y_true, y_pred):
    return 2*(y_pred-y_true)/y_true.size;

# training data
x_train = np.array([[[0,0]], [[0,1]], [[1,0]], [[1,1]]]);
y_train = np.array([[[0]], [[1]], [[1]], [[0]]]);

# network
net = Network();
net.add(FCLayer((1,2), (1,3)));
net.add(ActivationLayer((1,3), act, act_prime));
net.add(FCLayer((1,3), (1,1)));
net.add(ActivationLayer((1,1), act, act_prime));

# train
net.use(loss, loss_prime);
net.fit(x_train, y_train, epochs=1000, learning_rate=0.1);

# test
out = net.predict(x_train);
print(out);
Beispiel #2
0
class Individual:
    def __init__(self):
        self.NN = Network(5, 1)
        self.alive = True
        self.reached_pilars = 0
        self.score = 0
        self.rect = pygame.Rect((int(WIDTH / 2), int(HEIGHT / 2)), (40, 40))
        self.movement_y = 0
        self.bool = True
        self.bool2 = True

    def draw(self, display):
        pygame.draw.rect(display, (0, 255, 0), self.rect, 1)

    def update(self):
        if self.alive:
            self.score+=1
            self.predict()
            self.movement_y += gravity
            self.rect.centery += self.movement_y
            self.check()
            VARIABLES.best_round_score = self.reached_pilars

    def check(self):
        global pipe_rect_list
        if self.rect.collidelist(pipe_rect_list) != -1:
            self.alive = False

        if pipe_rect_list[0].centerx - 10 < self.rect.centerx < pipe_rect_list[0].centerx + 10 and self.bool:
            self.bool = False
            self.reached_pilars += 1
        elif not pipe_rect_list[0].centerx - 10 < self.rect.centerx < pipe_rect_list[0].centerx + 10:
            self.bool = True

        if pipe_rect_list[2].centerx - 10 < self.rect.centerx < pipe_rect_list[1].centerx + 10 and self.bool2:
            self.bool2 = False
            self.reached_pilars += 1
        elif not pipe_rect_list[2].centerx - 10 < self.rect.centerx < pipe_rect_list[1].centerx + 10:
            self.bool2 = True

        if self.rect.bottom>HEIGHT or self.rect.top < 0:
            self.alive = False

    def jump(self):
        self.movement_y = -8

    def get_state(self):
        global pipe_list
        if pipe_list[0].rect_upper.right < self.rect.left and pipe_list[0].rect_upper.right < pipe_list[1].rect_upper.right:
            pipe = pipe_list[1]
        elif pipe_list[1].rect_upper.right < self.rect.left and pipe_list[1].rect_upper.right < pipe_list[0].rect_upper.right:
            pipe = pipe_list[0]
        else:
            pipe = pipe_list[0]
        return [self.rect.centery/HEIGHT, pipe.rect_upper.bottom/HEIGHT,
                pipe.rect_lower.top/HEIGHT, pipe.rect_lower.centerx/WIDTH,
                self.movement_y/20]

    def pair(self, other):
        new_weight1 = self.NN.get_flatted()
        new_weight2 = other.NN.get_flatted()
        gene = random.randint(0, len(new_weight1) - 1)
        new_weight1[gene:], new_weight2[:gene] = new_weight2[gene:], new_weight1[:gene]

        return new_weight1, new_weight2

    def predict(self):
        output = self.NN.predict(self.get_state())
        if output < 0.5:
            self.jump()

    def reset(self):
        self.rect.centery = int(HEIGHT / 2)
        self.alive = True
        self.reached_pilars = 0
        self.score = 0