Example #1
0
    def __init__(self, args):
        self.tasks = [self.train_step]

        self.args = args
        self.device = args.device
        self.model = Model().to(self.device)
        self.optimizer = optim.Adam(self.model.parameters(),
                                    lr=args.lr,
                                    amsgrad=True)
        self.writer = SummaryWriter()
        self.scores = deque(maxlen=250)
        self.saved_model_score = -1e10
        self._scores = deque(maxlen=10000)
        self.n_errors = 0

        self.env = Environment(args)
        self.batch_buffer = list()

        self.frames = 0
        self.running = True
        self.debug_mode = False

        backup_dir(src=str(Path(__file__).parent),
                   dst=str(Path(self.writer.log_dir) / 'backup.zip'))
        cprint('READY', 'green')
Example #2
0
    def __init__(self, args):

        self.tasks = [self.train_step]
        self.args = args
        self.device = args.device
        self.model = Model().to(self.device)

        # 만약 model.pt가 있다면, model load
        # 이전에 학습된 모델이라고 가정한다.
        model_path = Path(__file__).parent / 'model.pt'
        if os.path.isfile(model_path):
            # checkpoint = torch.load(model_path, map_location=torch.device('cuda')) # gpu
            checkpoint = torch.load(model_path, map_location=torch.device('cpu'))  # cpu
            # checkpoint = torch.load(model_path)  # gpu environment
            self.model.load_state_dict(checkpoint['model_state_dict'])
            # gpu
            # self.model.to(self.device)

        # parameter for model save
        # 현재 score가 일정 수치를 넘어 model save가 되면 False로 바뀐다.
        # 다시 score가 일정 수치 이하로 내려가면 True로 바뀐다.
        self.possible_model_save = True

        # 만약 model.pt가 있다면, optimizer load
        self.optimizer = optim.Adam(self.model.parameters(), lr=args.lr, amsgrad=True)
        if os.path.isfile(model_path):
            # checkpoint = torch.load(model_path, map_location=torch.device('cuda')) # gpu
            checkpoint = torch.load(model_path, map_location=torch.device('cpu'))  # cpu
            # checkpoint = torch.load(model_path)
            self.optimizer.load_state_dict(checkpoint['optimizer_state_dict'])

        self.writer = SummaryWriter()
        self.scores = deque(maxlen=250)
        self.saved_model_score = -1e10
        self._scores = deque(maxlen=10000)
        self.n_errors = 0

        # for pool
        self.mean_score = -1
        self.mybot_version = None
        self.bot_cache_path = Path(__file__).parent / ('bot_cache.txt')

        # version 파일이 있다면 내용을 가져오기.
        if os.path.isfile(self.bot_cache_path):
            with open(self.bot_cache_path, 'r') as cache_reader:
                Info_cache = cache_reader.read()
                # 비어 있지 않을 때 내용을 가져와 mybot_version에 넣는다.
                if Info_cache != '':
                    splited_Info = Info_cache.split()
                    self.mybot_version = int(splited_Info[0])
                    self.saved_model_score = float(splited_Info[1])
                else:
                    self.mybot_version = 1
                    # 혹시 모르는 cache 파일은 없고 모델만 생겼을 때를 대비.
                    for i in range(1, 5):
                        model_path = Path(__file__).parent / ('model' + str(i) + '.pt')
                        if os.path.isfile(model_path):
                            self.mybot_version = (self.mybot_version % 4) + 1
        else:
            self.mybot_version = 1
            # 혹시 모르는 cache 파일은 없고 모델만 생겼을 때를 대비.
            for i in range(1, 5):
                model_path = Path(__file__).parent / ('model' + str(i) + '.pt')
                if os.path.isfile(model_path):
                    self.mybot_version = (self.mybot_version % 4) + 1

        self.env = Environment(args)
        self.batch_buffer = list()

        self.frames = 0
        self.running = True
        self.debug_mode = False

        backup_dir(
            src=str(Path(__file__).parent), 
            dst=str(Path(self.writer.log_dir) / 'backup.zip')
        )
        cprint('READY', 'green')