def training(self): self.compile_model() total_steps = self.config.trainer.start_total_steps bef_files = [] last_file = None while True: files = get_game_data_filenames(self.config.resource) if (len(files) * self.config.play_data.nb_game_in_file < self.config.trainer.min_games_to_begin_learn \ or ((last_file is not None) and files.index(last_file) + 20 > len(files))): if last_file is not None: logger.info('Waiting for enough data 600s, ' + str((len(files) - files.index(last_file)) * self.config.play_data.nb_game_in_file) \ +' vs '+ str(self.config.trainer.min_games_to_begin_learn)+' games') else: logger.info('Waiting for enough data 600s, ' + str(len(files) * self.config.play_data.nb_game_in_file) \ +' vs '+ str(self.config.trainer.min_games_to_begin_learn)+' games') time.sleep(600) continue else: last_file = files[-1] bef_files = files if (len(files) > 100): files = files[-100:] self.filenames = deque(files) shuffle(self.filenames) self.fill_queue() if len(self.dataset[0]) > self.config.trainer.batch_size: steps = self.train_epoch(self.config.trainer.epoch_to_checkpoint) total_steps += steps self.save_current_model() a, b, c = self.dataset a.clear() b.clear() c.clear() self.update_learning_rate(total_steps)
def remove_play_data(self): files = get_game_data_filenames(self.config.resource) if len(files) < self.config.play_data.max_file_num: return try: for i in range(len(files) - self.config.play_data.max_file_num): os.remove(files[i]) except: pass
def remove_play_data(self,all=False): files = get_game_data_filenames(self.config.resource) if (all): for path in files: os.remove(path) else: while len(files) > self.config.play_data.max_file_num: os.remove(files[0]) del files[0]
def remove_play_data(self): files = get_game_data_filenames(self.config.resource) if len(files) < self.config.play_data.max_file_num: return try: for i in range(len(files) - self.config.play_data.max_file_num): os.remove(files[i]) except: pass
def fixbug(): from cchess_alphazero.config import Config from cchess_alphazero.lib.data_helper import get_game_data_filenames, read_game_data_from_file, write_game_data_to_file import cchess_alphazero.environment.static_env as senv c = Config('distribute') files = get_game_data_filenames(c.resource) cnt = 0 fix = 0 draw_cnt = 0 for filename in files: try: data = read_game_data_from_file(filename) except: print(f"error: {filename}") os.remove(filename) continue state = data[0] real_data = [state] need_fix = True draw = False action = None value = None is_red_turn = True for item in data[1:]: action = item[0] value = -item[1] if value == 0: need_fix = False draw = True draw_cnt += 1 break state = senv.step(state, action) is_red_turn = not is_red_turn real_data.append([action, value]) if not draw: game_over, v, final_move = senv.done(state) if final_move: v = -v is_red_turn = not is_red_turn if not is_red_turn: v = -v if not game_over: v = 1 # print(game_over, v, final_move, state) if v == data[1][1]: need_fix = False else: need_fix = True if need_fix: write_game_data_to_file(filename, real_data) # print(filename) fix += 1 cnt += 1 if cnt % 1000 == 0: print(cnt, fix, draw_cnt) print(f"all {cnt}, fix {fix}, draw {draw_cnt}")
def training(self): self.compile_model() total_steps = self.config.trainer.start_total_steps bef_files = [] last_file = None while True: files = get_game_data_filenames(self.config.resource) offset = self.config.trainer.min_games_to_begin_learn if (len(files) < self.config.trainer.min_games_to_begin_learn \ or ((last_file is not None and last_file in files) and files.index(last_file) + 1 + offset > len(files))): # if last_file is not None: # logger.info('Waiting for enough data 300s, ' + str((len(files) - files.index(last_file)) * self.config.play_data.nb_game_in_file) \ # +' vs '+ str(self.config.trainer.min_games_to_begin_learn)+' games') # else: # logger.info('Waiting for enough data 300s, ' + str(len(files) * self.config.play_data.nb_game_in_file) \ # +' vs '+ str(self.config.trainer.min_games_to_begin_learn)+' games') # time.sleep(300) if last_file is not None: self.save_current_model(send=True) break else: if last_file is not None and last_file in files: idx = files.index(last_file) + 1 if len(files) - idx > self.config.trainer.load_step: files = files[idx:idx + self.config.trainer.load_step] else: files = files[idx:] elif len(files) > self.config.trainer.load_step: files = files[0:self.config.trainer.load_step] last_file = files[-1] logger.info(f"Last file = {last_file}") logger.debug(f"files = {files[0:-1:2000]}") self.filenames = deque(files) logger.debug(f"Start training {len(self.filenames)} files") shuffle(self.filenames) self.fill_queue() self.update_learning_rate(total_steps) if len(self.dataset[0]) > self.config.trainer.batch_size: steps = self.train_epoch( self.config.trainer.epoch_to_checkpoint) total_steps += steps self.save_current_model(send=False) self.update_learning_rate(total_steps) self.count += 1 a, b, c = self.dataset a.clear() b.clear() c.clear() del self.dataset, a, b, c gc.collect() self.dataset = deque(), deque(), deque() self.backup_play_data(files)
def training(self): self.compile_model() total_steps = self.config.trainer.start_total_steps bef_files = [] last_file = None while True: files = get_game_data_filenames(self.config.resource) offset = self.config.trainer.min_games_to_begin_learn // self.config.play_data.nb_game_in_file if (len(files) * self.config.play_data.nb_game_in_file < self.config.trainer.min_games_to_begin_learn \ or ((last_file is not None) and files.index(last_file) + offset > len(files))): if last_file is not None: logger.info('Waiting for enough data 300s, ' + str((len(files) - files.index(last_file)) * self.config.play_data.nb_game_in_file) \ +' vs '+ str(self.config.trainer.min_games_to_begin_learn)+' games') else: logger.info('Waiting for enough data 300s, ' + str(len(files) * self.config.play_data.nb_game_in_file) \ +' vs '+ str(self.config.trainer.min_games_to_begin_learn)+' games') time.sleep(300) continue else: bef_files = files if last_file is not None and len(files) > offset: files = files[-offset:] last_file = files[-1] self.filenames = deque(files) logger.debug(f"Start training {len(self.filenames)} files") shuffle(self.filenames) self.fill_queue() if len(self.dataset[0]) > self.config.trainer.batch_size: steps = self.train_epoch(self.config.trainer.epoch_to_checkpoint) total_steps += steps self.save_current_model() a, b, c = self.dataset a.clear() b.clear() c.clear() self.update_learning_rate(total_steps) self.remove_play_data()