def __init__(self, gameDisplay):
     self.gameDisplay = gameDisplay
     self.state = BIRD_ALIVE
     self.img = pygame.image.load(BIRD_FILENAME)
     self.rect = self.img.get_rect()
     self.speed = 0
     self.time_lived = 0
     self.nnet = Nnet(NNET_INPUTS, NNET_HIDDEN, NNET_OUTPUTS)
     self.set_position(BIRD_START_X, BIRD_START_Y)
示例#2
0
 def __init__(self, gameDisplay):  #initialize bird and its features
     self.gameDisplay = gameDisplay
     self.state = BIRD_ALIVE  #Bird starts alive
     self.img = pygame.image.load(BIRD_FILENAME)  #Load image of bird
     self.rect = self.img.get_rect()  #Is used to find the position of bird
     self.speed = 0  #start with 0 speed
     self.time_lived = 0  #start with no time lived (This is for the AI to see if its doing well
     self.nnet = Nnet(NNET_INPUTS, NNET_HIDDEN,
                      NNET_OUTPUTS)  #Initialize different nodes with nnet
     self.set_position(BIRD_START_X, BIRD_START_Y)  #Startposition
示例#3
0
 def _load_nnet(self, cpt_dir):
     # load nnet conf
     nnet_conf = load_json(cpt_dir, "mdl.json")
     nnet = Nnet(**nnet_conf)
     # load checkpoint
     cpt_fname = os.path.join(cpt_dir, "best.pt.tar")
     cpt = th.load(cpt_fname, map_location="cpu")
     nnet.load_state_dict(cpt["model_state_dict"])
     logger.info("Load checkpoint from {}, epoch {:d}".format(
         cpt_fname, cpt["epoch"]))
     return nnet
示例#4
0
def calc_gradient(module_name, layer_sizes):
    W, B = load_WB_data(module_name, layer_sizes)

    net1 = Nnet(layer_sizes, random = False, W=W, B=B)

    outputs = net1.forward_prop(np.asarray(calc_grad_X))
    w_grads, b_grads = net1.back_prop(outputs, np.asarray(calc_grad_T))

    with open(question_2_path + "/dw-" + module_name + ".csv", 'w') as csvfile:
        myWriter = csv.writer(csvfile, lineterminator = '\n')
        for rows in list(w_grads)[::2]:
            myWriter.writerows(rows.tolist())

    with open(question_2_path + "/db-" + module_name + ".csv", 'w') as csvfile:
        myWriter = csv.writer(csvfile, lineterminator = '\n')
        myWriter.writerows(list(b_grads)[::2])
示例#5
0
文件: train.py 项目: zhaoforever/setk
def run(args):
    nnet = Nnet(**nnet_conf)

    trainer = PermutationTrainer(nnet,
                                 gpuid=args.gpu,
                                 checkpoint=args.checkpoint,
                                 **trainer_conf)

    data_conf = {"train_data": train_data, "dev_data": dev_data}
    confs = [nnet_conf, feats_conf, trainer_conf, data_conf]
    names = ["mdl.json", "feats.json", "trainer.json", "data.conf"]

    for conf, fname in zip(confs, names):
        dump_json(conf, args.checkpoint, fname)

    feats_conf["shuf"] = True
    train_loader = make_pitloader(train_data["linear_x"],
                                  feats_conf,
                                  train_data,
                                  batch_size=args.batch_size,
                                  cache_size=args.cache_size)
    feats_conf["shuf"] = False
    dev_loader = make_pitloader(dev_data["linear_x"],
                                feats_conf,
                                dev_data,
                                batch_size=args.batch_size,
                                cache_size=args.cache_size)

    trainer.run(train_loader, dev_loader, num_epochs=args.epochs)
def run(args):
    parse_str = lambda s: tuple(map(int, s.split(",")))
    nnet = Nnet(**nnet_conf)

    trainer = GE2ETrainer(
        nnet,
        gpuid=parse_str(args.gpu),
        checkpoint=args.checkpoint,
        resume=args.resume,
        **trainer_conf)

    loader_conf = {
        "M": args.M,
        "N": args.N,
        "chunk_size": parse_str(args.chunk_size)
    }
    for conf, fname in zip([nnet_conf, trainer_conf, loader_conf],
                           ["mdl.json", "trainer.json", "loader.json"]):
        dump_json(conf, args.checkpoint, fname)

    train_loader = SpeakerLoader(
        train_dir, **loader_conf, num_steps=args.train_steps)
    dev_loader = SpeakerLoader(
        dev_dir, **loader_conf, num_steps=args.dev_steps)

    trainer.run(train_loader, dev_loader, num_epochs=args.epochs)
示例#7
0
def train_plot_net(module_name):
    layer_sizes = modules[module_name]
    X_train, T_train, X_validation, T_validation, X_test, T_test = load_training_data(
    )

    # W_raw = read_file('weight.csv')
    # B_raw = read_file('bias.csv')
    # W, B = [], []
    #
    # cur = 0
    # for i in range(len(layer_sizes) - 1):
    #     W.append(np.asarray(W_raw[cur:cur + layer_sizes[i]], dtype=np.float32))
    #     B.append(np.asarray(B_raw[i], dtype=np.float32))
    #     cur += layer_sizes[i]
    #
    # net1 = Nnet(layer_sizes, random = False, W=W, B=B)
    net1 = Nnet(layer_sizes)
    net1.set_train_param(batch_size=40,
                         max_nb_of_epochs=800,
                         learning_rate=0.05,
                         momentum=0.1,
                         lrate_drop=0.75,
                         epochs_drop=20)
    res = net1.train(X_train, T_train, X_validation, T_validation, X_test,
                     T_test)
    net1.save_wb()

    plot.plot_accuracys(module_name, res["nb_of_epochs"], res["train_acc"],
                        res["val_acc"], res["test_acc"])
    plot.plot_costs(module_name, res["nb_of_epochs"], res["train_costs"],
                    res["val_costs"], res["test_costs"])
    plot.plot_lrate(module_name, res["nb_of_epochs"], res["lrate"])
class Bird():
    def __init__(self, gameDisplay):
        self.gameDisplay = gameDisplay
        self.state = BIRD_ALIVE
        self.img = pygame.image.load(BIRD_FILENAME)
        self.rect = self.img.get_rect()
        self.speed = 0
        self.time_lived = 0
        self.nnet = Nnet(NNET_INPUTS, NNET_HIDDEN, NNET_OUTPUTS)
        self.set_position(BIRD_START_X, BIRD_START_Y)

    def set_position(self, x, y):
        self.rect.centerx = x
        self.rect.centery = y

    def move(self, dt):

        distance = 0
        new_speed = 0

        distance = (self.speed * dt) + (0.5 * GRAVITY * dt * dt)
        new_speed = self.speed + (GRAVITY * dt)

        self.rect.centery += distance
        self.speed = new_speed

        if self.rect.top < 0:
            self.rect.top = 0
            self.speed = 0

    def jump(self, pipes):
        inputs = self.get_inputs(pipes)
        val = self.nnet.get_max_value(inputs)
        if val > JUMP_CHANCE:
            self.speed = BIRD_START_SPEED

    def draw(self):
        self.gameDisplay.blit(self.img, self.rect)

    def check_status(self, pipes):
        if self.rect.bottom > DISPLAY_H:
            self.state = BIRD_DEAD
        else:
            self.check_hits(pipes)

    def check_hits(self, pipes):
        for p in pipes:
            if p.rect.colliderect(self.rect):
                self.state = BIRD_DEAD
                break

    def update(self, dt, pipes):
        if self.state == BIRD_ALIVE:
            self.time_lived += dt
            self.move(dt)
            self.jump(pipes)
            self.draw()
            self.check_status(pipes)

    def get_inputs(self, pipes):

        closest = DISPLAY_W * 2
        bottom_y = 0
        for p in pipes:
            if p.pipe_type == PIPE_UPPER and p.rect.right < closest and p.rect.right > self.rect.left:
                closest = p.rect.right
                bottom_y = p.rect.bottom

        horizontal_distance = closest - self.rect.centerx
        vertical_distance = (self.rect.centery) - (bottom_y +
                                                   PIPE_GAP_SIZE / 2)

        inputs = [((horizontal_distance / DISPLAY_W) * 0.99) + 0.01,
                  (((vertical_distance + Y_SHIFT) / NORMALIZER) * 0.99) + 0.01]

        return inputs
示例#9
0
class Bird():
    def __init__(self, gameDisplay):
        self.gameDisplay = gameDisplay
        self.state = BIRD_ALIVE
        self.img = pygame.image.load(BIRD_FILENAME)
        self.rect = self.img.get_rect()
        self.speed = 0
        self.fitness = 0
        self.time_lived = 0
        self.nnet = Nnet(NNET_INPUTS, NNET_HIDDEN, NNET_OUTPUTS)
        self.set_position(BIRD_START_X, BIRD_START_Y)

    def reset(self):
        self.state = BIRD_ALIVE
        self.speed = 0
        self.fitness = 0
        self.time_lived = 0
        self.set_position(BIRD_START_X, BIRD_START_Y)

    def set_position(self, x, y):
        self.rect.centerx = x
        self.rect.centery = y

    def move(self, dt):
        distance = 0
        new_speed = 0

        distance = (self.speed * dt) + (
            0.5 * GRAVITY * dt * dt
        )  # this is using the real-life distanced travelled equation (s = ut + 0.5at^2)
        new_speed = self.speed + (GRAVITY * dt)

        self.rect.centery += distance
        self.speed = new_speed

        if self.rect.top < 0:
            self.rect.top = 0
            self.speed = 0

    def jump(self, pipes):
        inputs = self.get_inputs(pipes)
        val = self.nnet.get_max_value(inputs)
        if val >= JUMP_CUTOFF:
            self.speed = BIRD_START_SPEED

    def draw(self):
        self.gameDisplay.blit(self.img, self.rect)

    def check_status(self, pipes):
        if self.rect.bottom > DISPLAY_H:
            self.state = BIRD_DEAD
        else:
            self.check_hits(pipes)

    def assign_collision_fitness(self, p):
        gap_y = 0
        if p.pipe_type == PIPE_UPPER:
            gap_y = p.rect.bottom + PIPE_GAP_SIZE / 2
        else:
            gap_y = p.rect.top - PIPE_GAP_SIZE / 2

        self.fitness = -(abs(self.rect.centery - gap_y))

    def check_hits(self, pipes):
        for p in pipes:
            if p.rect.colliderect(self.rect):
                self.state = BIRD_DEAD
                self.assign_collision_fitness(p)
                break

    def update(self, dt, pipes):
        if self.state == BIRD_ALIVE:
            self.time_lived += dt
            self.move(dt)
            self.jump(pipes)
            self.draw()
            self.check_status(pipes)

    def get_inputs(self, pipes):
        upcoming = DISPLAY_W * 2
        bottom_y = 0
        for p in pipes:
            if p.pipe_type == PIPE_UPPER and p.rect.right < upcoming and p.rect.right > self.rect.left:
                upcoming = p.rect.right
                bottom_y = p.rect.bottom

        horizontal_distance = upcoming - self.rect.centerx
        vertical_distance = (self.rect.centery) - (bottom_y +
                                                   PIPE_GAP_SIZE / 2)

        inputs = [((horizontal_distance / DISPLAY_W) * 0.99) + 0.01,
                  (((vertical_distance + Y_SHIFT) / NORMALIZER) * 0.99) + 0.01]

        return inputs

    def create_offspring(p1, p2, gameDisplay):
        new_bird = Bird(gameDisplay)
        new_bird.nnet.create_mixed_weights(p1.nnet, p2.nnet)
        return new_bird
示例#10
0
class Bird():
    def __init__(self, gameDisplay):  #initialize bird and its features
        self.gameDisplay = gameDisplay
        self.state = BIRD_ALIVE  #Bird starts alive
        self.img = pygame.image.load(BIRD_FILENAME)  #Load image of bird
        self.rect = self.img.get_rect()  #Is used to find the position of bird
        self.speed = 0  #start with 0 speed
        self.fitness = 0
        self.time_lived = 0  #start with no time lived (This is for the AI to see if its doing well
        self.nnet = Nnet(NNET_INPUTS, NNET_HIDDEN,
                         NNET_OUTPUTS)  #Initialize different nodes with nnet
        self.set_position(BIRD_START_X, BIRD_START_Y)  #Startposition

    def reset(self):  #Reset all changed values
        self.state = BIRD_ALIVE
        self.speed = 0
        self.fitness = 0
        self.time_lived = 0
        self.set_position(BIRD_START_X, BIRD_START_Y)

    def set_position(self, x, y):
        self.rect.centerx = x
        self.rect.centery = y

    def move(self, dt):  #When moving
        distance = 0  #empty distance
        new_speed = 0  #empty speed

        distance = (self.speed * dt) + (0.5 * GRAVITY * dt * dt
                                        )  #s = ut + 0.5at^2
        new_speed = self.speed + (GRAVITY * dt)  #v = u + at

        self.rect.centery += distance  #Set the position of the bird on the y axis
        self.speed = new_speed  #Set speed of the bird

        if self.rect.top < 0:  #If the bird goes above the screen
            self.rect.top = 0  #stay in screen
            self.speed = 0  #set speed to 0

    def jump(self, pipes):
        inputs = self.get_inputs(pipes)
        val = self.nnet.get_max_value(inputs)
        if val > JUMP_CHANCE:
            self.speed = BIRD_START_SPEED  #If jump is used, set speed again

    def draw(self):
        self.gameDisplay.blit(self.img, self.rect)  #Draw every frame

    def check_status(self, pipes):  #Check if the bird still lives
        if self.rect.bottom > DISPLAY_H:  #If it is below the screen
            self.state = BIRD_DEAD
        else:  #if not
            self.check_hits(pipes)  #check if it hit with the pipes

    def assign_collision_fitness(
            self, p
    ):  #Set fitness value to how close it was to the pipe when it died
        gap_y = 0
        if p.pipe_type == PIPE_UPPER:  #If collided into upper pipe
            gap_y = p.rect.bottom + PIPE_GAP_SIZE / 2  #Distance to gap from upper pipe
        else:
            gap_y = p.rect.top - PIPE_GAP_SIZE / 2  #Distance to gap from bottom pipe

        self.fitness = -(abs(self.rect.centery - gap_y))

    def check_hits(self, pipes):
        for p in pipes:  #for every pipe in the list
            if p.rect.colliderect(
                    self.rect):  #When the bird collides with a pipe
                self.state = BIRD_DEAD
                self.assign_collision_fitness(p)
                break

    def update(self, dt, pipes):
        if self.state == BIRD_ALIVE:  #if alive, call all the actions below
            self.time_lived += dt
            self.move(dt)
            self.jump(pipes)
            self.draw()
            self.check_status(pipes)

    def get_inputs(self, pipes):
        closest = DISPLAY_W * 2
        bottom_y = 0
        for p in pipes:  #Which pipe is closest?
            if p.pipe_type == PIPE_UPPER and p.rect.right < closest and p.rect.right > self.rect.left:
                closest = p.rect.right
                bottom_y = p.rect.bottom

        horizontal_distance = closest - self.rect.centerx  #how far the closest pipe is horizontally
        vertical_distance = (self.rect.centery) - (bottom_y + PIPE_GAP_SIZE / 2
                                                   )  #and vertically

        inputs = [((horizontal_distance / DISPLAY_W) * 0.99) + 0.01,
                  (((vertical_distance + Y_SHIFT) / NORMALIZER) * 0.99) + 0.01]

        return inputs

    def create_offspring(
        p1, p2, gameDisplay
    ):  #from two other birds, one new bird can be created based on their neural net weights
        new_bird = Bird(gameDisplay)
        new_bird.nnet.create_mixed_weights(p1.nnet, p2.nnet)
        return new_bird