def replay(argv): import argparse parser = argparse.ArgumentParser(description='replay state') parser.add_argument('game', help='a game name i.e. checkers') parser.add_argument('history', help='state history') args = parser.parse_args(argv) from util import game State = game.importState(args.game).State history = args.history.split(' ')[1:] state = State() print(state) for action in history: print(action) acs = state.getAction() select_action = None for encoded_action, k in acs: decoded_action = state.actionToString(encoded_action) if action == decoded_action: select_action = encoded_action break assert (select_action != None) state = state.getNextState(select_action) print(state) assert (state.getWinner() != None)
def train(argv): import argparse parser = argparse.ArgumentParser(description='train nn from history file') parser.add_argument('game', help='a game name i.e. checkers') parser.add_argument('history', help='history file') parser.add_argument('input', help='input model file name') parser.add_argument('output', help='output model file name') parser.add_argument('--progress', action='store_true', help='show progress bar') parser.add_argument('--epoch', default=1, type=int, help='training epochs') parser.add_argument('--batch', default=256, type=int, help='batch size') parser.add_argument('--block', default=100000, type=int, help='block size') parser.add_argument('--gpu', type=float, help='gpu memory fraction') args = parser.parse_args(argv) # set gpu memory if args.gpu != None: import tensorflow as tf from keras.backend.tensorflow_backend import set_session config = tf.ConfigProto() config.gpu_options.per_process_gpu_memory_fraction = args.gpu set_session(tf.Session(config=config)) from util import game State = game.importState(args.game).State NN = game.importNn(args.game).NN state = State() nn = NN(args.input) for epoch in range(args.epoch): from util.trainer import train train(state, nn, args.history, { 'batch': args.batch, 'block': args.block, 'progress': args.progress, }) nn.save(args.output)
def loop(argv): import argparse parser = argparse.ArgumentParser(description='loop generate and train') parser.add_argument('game', help='a game name i.e. checkers') parser.add_argument('begin', default=1, type=int, help='begin with generation number') parser.add_argument('count', nargs='?', default=100, type=int, help='number of generation') parser.add_argument('-n', '--number', default=10000, type=int, help='number of generated states') parser.add_argument('-s', '--simulation', default=100, type=int, help='number of simulations per move') parser.add_argument('--hard', default=0, type=int, help='number of random moves') parser.add_argument('--soft', default=1000, type=int, help='number of random moves that depends on visited node count') parser.add_argument('--epoch', default=1, type=int, help='training epochs') parser.add_argument('--batch', default=256, type=int, help='batch size') parser.add_argument('--block', default=100000, type=int, help='block size') parser.add_argument('--gpu', type=float, help='gpu memory fraction') args = parser.parse_args(argv) # set gpu memory if args.gpu != None: import tensorflow as tf from keras.backend.tensorflow_backend import set_session config = tf.ConfigProto() config.gpu_options.per_process_gpu_memory_fraction = args.gpu set_session(tf.Session(config=config)) from util import game State = game.importState(args.game).State NN = game.importNn(args.game).NN state = State() state_no = args.begin nn = NN('{:06}.h5'.format(state_no)) for i in range(args.count): history_filename = '{:06}.txt'.format(state_no) # generate with open(history_filename, 'a') as file: def save_to_file(result): file.write(result) file.write('\n') file.flush() callback = save_to_file from util.generator import generate generate(state, nn, callback, { 'selfplay': args.number, 'simulation': args.simulation, 'hard_random': args.hard, 'soft_random': args.soft, 'progress': True, }) # train for epoch in range(args.epoch): from util.trainer import train train(state, nn, history_filename, { 'batch': args.batch, 'block': args.block, 'progress': True, }) # next generation state_no += 1 nn.save('{:06}.h5'.format(state_no))
parser.add_argument('--gpu', type=float, help='gpu memory fraction') parser.add_argument('--file', help='save to a file') parser.add_argument('--network', help='save to remote server') args = parser.parse_args(sys.argv[1:]) # set gpu memory if args.gpu != None: import tensorflow as tf from keras.backend.tensorflow_backend import set_session config = tf.ConfigProto() config.gpu_options.per_process_gpu_memory_fraction = args.gpu set_session(tf.Session(config=config)) from util import game State = game.importState(args.game).State NN = game.importNn(args.game).NN from async .nn import NN as async_nn state = State() nn = async_nn(NN(args.model), args.task) file = None from tqdm import tqdm pbar = tqdm() import asyncio if args.network != None: import requests loop = asyncio.get_event_loop()
def optimize(argv): import argparse parser = argparse.ArgumentParser(description='optimize end-game state') parser.add_argument('game', help='a game name i.e. checkers') parser.add_argument('player', default='minimax,6', nargs='?', help='optimized by which player') parser.add_argument('history', nargs='?', help='history from command-line') parser.add_argument('--truncate', default=18, type=int, help='truncate last N actions') parser.add_argument('--file', nargs=2, help='file names to load/save history') parser.add_argument('--network', help='remote server url to load/save history') parser.add_argument('--reoptimize', action='store_true', help='re-optimize not successful optimize states') parser.add_argument('--verbose', action='store_true', help='show optimized actions') args = parser.parse_args(argv) from util import game State = game.importState(args.game).State state = State() from util.player import newPlayer player = newPlayer(args.game, args.player) from util import optimizer if args.history != None: player.prepare() history = prepare_history(args.history, args) result = optimizer.optimize(state, history, player, args.verbose) print(result) return if args.file != None: with open(args.file[1], 'w') as outfile: with open(args.file[0], 'r') as infile: for line in infile: player.prepare() history = prepare_history(line, args) result = optimizer.optimize(state, history, player, args.verbose) outfile.write(result) outfile.write('\n') outfile.flush() return if args.network != None: from util import server get = args.network + ('reoptimize.php' if args.reoptimize else 'unoptimize.php') put = args.network + 'optimize.php' while True: result = server.getUnoptimize(get) if result == None: break (model, unoptimized_history) = result player.prepare() history = prepare_history(unoptimized_history, args) result = optimizer.optimize(state, history, player, args.verbose) server.optimize(put, model, unoptimized_history, result) return
def generate(argv): import argparse parser = argparse.ArgumentParser( description='generate game states from mcts+nn') parser.add_argument('game', help='a game name i.e. checkers') parser.add_argument('-m', '--model', default='latest.h5', help='model filename') parser.add_argument('-n', '--number', default=1000000, type=int, help='number of generated states') parser.add_argument('-s', '--simulation', default=100, type=int, help='number of simulations per move') parser.add_argument('--hard', default=0, type=int, help='number of random moves') parser.add_argument( '--soft', default=1000, type=int, help='number of random moves that depends on visited node count') parser.add_argument('--progress', action='store_true', help='show progress bar') parser.add_argument('--gpu', type=float, help='gpu memory fraction') parser.add_argument('--file', help='save to a file') parser.add_argument('--network', help='save to remote server') args = parser.parse_args(argv) # set gpu memory if args.gpu != None: import tensorflow as tf from keras.backend.tensorflow_backend import set_session config = tf.ConfigProto() config.gpu_options.per_process_gpu_memory_fraction = args.gpu set_session(tf.Session(config=config)) from util import game State = game.importState(args.game).State NN = game.importNn(args.game).NN state = State() nn = NN(args.model) file = None if args.network != None: from util.server import submit def submit_to_remote_server(result): submit(args.network, args.model, result) callback = submit_to_remote_server elif args.file != None: file = open(args.file, 'a') def save_to_file(result): file.write(result) file.write('\n') file.flush() callback = save_to_file else: def print_to_stdout(result): print(result) callback = print_to_stdout from util.generator import generate generate( state, nn, callback, { 'selfplay': args.number, 'simulation': args.simulation, 'hard_random': args.hard, 'soft_random': args.soft, 'progress': args.progress, }) if file != None: file.close()