Python utils.logged_timer() Examples
The following are 30
code examples of utils.logged_timer().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
utils
, or try the search function
.
Example #1
Source File: main.py From training_results_v0.5 with Apache License 2.0 | 6 votes |
def evaluate_both( prev_model: 'The path to previous model', cur_model: 'The path to current model', output_dir: 'Where to write the evaluation results'='sgf/evaluate', readouts: 'How many readouts to make per move.'=200, games: 'the number of games to play'=20, verbose: 'How verbose the players should be (see selfplay)' = 1): qmeas.start_time('evaluate') _ensure_dir_exists(output_dir) winners = [] with timer("%d games" % games): winners = evaluation.play_match_many_instance_both( prev_model, cur_model, games, readouts, output_dir, verbose) qmeas.stop_time('evaluate') white_count = 0 for win in winners: if 'W' in win or 'w' in win: white_count += 1 return white_count * 1.0 / (games*2) # qmeas.report_profiler()
Example #2
Source File: controller.py From Python-Reinforcement-Learning-Projects with MIT License | 6 votes |
def train(): model_version, model_name = get_latest_model() logger.info("Training on gathered game data, initializing from {}".format(model_name)) new_model_name = generate(model_version + 1) logger.info("New model will be {}".format(new_model_name)) save_file = os.path.join(PATHS.MODELS_DIR, new_model_name) try: logger.info("Getting tf_records") tf_records = sorted(gfile.Glob(os.path.join(PATHS.TRAINING_CHUNK_DIR, '*.tfrecord.zz'))) tf_records = tf_records[ -1 * (GLOBAL_PARAMETER_STORE.WINDOW_SIZE // GLOBAL_PARAMETER_STORE.EXAMPLES_PER_RECORD):] print("Training from:", tf_records[0], "to", tf_records[-1]) with timer("Training"): network.train(PATHS.ESTIMATOR_WORKING_DIR, tf_records, model_version+1) network.export_latest_checkpoint_model(PATHS.ESTIMATOR_WORKING_DIR, save_file) except: logger.info("Got an error training") logging.exception("Train error")
Example #3
Source File: minigo.py From Gun-Detector with Apache License 2.0 | 6 votes |
def train(trained_models_dir, estimator_model_dir, training_chunk_dir, params): """Train the latest model from gathered data. Args: trained_models_dir: Where to export the completed generation. estimator_model_dir: tf.estimator model directory. training_chunk_dir: Directory where gathered training chunks are. params: An object of hyperparameters for the model. """ model_num, model_name = utils.get_latest_model(trained_models_dir) print('Initializing from model {}'.format(model_name)) new_model_name = utils.generate_model_name(model_num + 1) print('New model will be {}'.format(new_model_name)) save_file = os.path.join(trained_models_dir, new_model_name) tf_records = sorted( tf.gfile.Glob(os.path.join(training_chunk_dir, '*'+_TF_RECORD_SUFFIX))) tf_records = tf_records[ -(params.train_window_size // params.examples_per_chunk):] print('Training from: {} to {}'.format(tf_records[0], tf_records[-1])) with utils.logged_timer('Training'): dualnet.train(estimator_model_dir, tf_records, model_num + 1, params) dualnet.export_model(estimator_model_dir, save_file)
Example #4
Source File: validate.py From training with Apache License 2.0 | 6 votes |
def validate(*tf_records): """Validate a model's performance on a set of holdout data.""" if FLAGS.use_tpu: def _input_fn(params): return preprocessing.get_tpu_input_tensors( params['train_batch_size'], params['input_layout'], tf_records, filter_amount=1.0) else: def _input_fn(): return preprocessing.get_input_tensors( FLAGS.train_batch_size, FLAGS.input_layout, tf_records, filter_amount=1.0, shuffle_examples=False) steps = FLAGS.examples_to_validate // FLAGS.train_batch_size if FLAGS.use_tpu: steps //= FLAGS.num_tpu_cores estimator = dual_net.get_estimator() with utils.logged_timer("Validating"): estimator.evaluate(_input_fn, steps=steps, name=FLAGS.validate_name)
Example #5
Source File: validate.py From training with Apache License 2.0 | 6 votes |
def main(argv): """Validate a model's performance on a set of holdout data.""" _, *validation_paths = argv if FLAGS.expand_validation_dirs: tf_records = [] with utils.logged_timer("Building lists of holdout files"): dirs = validation_paths while dirs: d = dirs.pop() for path, newdirs, files in os.walk(d): tf_records.extend(os.path.join(path, f) for f in files if f.endswith('.zz')) dirs.extend(os.path.join(path, d) for d in newdirs) else: tf_records = validation_paths if not tf_records: print("Validation paths:", validation_paths) print(["{}:\n\t{}".format(p, os.listdir(p)) for p in validation_paths]) raise RuntimeError("Did not find any holdout files for validating!") validate(*tf_records)
Example #6
Source File: main.py From training_results_v0.5 with Apache License 2.0 | 6 votes |
def validate( working_dir: 'tf.estimator working directory', *tf_record_dirs: 'Directories where holdout data are', checkpoint_name: 'Which checkpoint to evaluate (None=latest)'=None, validate_name: 'Name for validation set (i.e., selfplay or human)'=None): qmeas.start_time('validate') tf_records = [] with timer("Building lists of holdout files"): for record_dir in tf_record_dirs: tf_records.extend(gfile.Glob(os.path.join(record_dir, '*.zz'))) first_record = os.path.basename(tf_records[0]) last_record = os.path.basename(tf_records[-1]) with timer("Validating from {} to {}".format(first_record, last_record)): dual_net.validate( working_dir, tf_records, checkpoint_name=checkpoint_name, name=validate_name) qmeas.stop_time('validate')
Example #7
Source File: minigo.py From g-tensorflow-models with Apache License 2.0 | 6 votes |
def train(trained_models_dir, estimator_model_dir, training_chunk_dir, generation, params): """Train the latest model from gathered data. Args: trained_models_dir: Where to export the completed generation. estimator_model_dir: tf.estimator model directory. training_chunk_dir: Directory where gathered training chunks are. generation: Which generation you are training. params: A MiniGoParams instance of hyperparameters for the model. """ new_model_name = utils.generate_model_name(generation) print('New model will be {}'.format(new_model_name)) new_model = os.path.join(trained_models_dir, new_model_name) print('Training on gathered game data...') tf_records = sorted( tf.gfile.Glob(os.path.join(training_chunk_dir, '*'+_TF_RECORD_SUFFIX))) tf_records = tf_records[ -(params.train_window_size // params.examples_per_chunk):] print('Training from: {} to {}'.format(tf_records[0], tf_records[-1])) with utils.logged_timer('Training'): dualnet.train(estimator_model_dir, tf_records, generation, params) dualnet.export_model(estimator_model_dir, new_model)
Example #8
Source File: minigo.py From multilabel-image-classification-tensorflow with MIT License | 6 votes |
def train(trained_models_dir, estimator_model_dir, training_chunk_dir, generation, params): """Train the latest model from gathered data. Args: trained_models_dir: Where to export the completed generation. estimator_model_dir: tf.estimator model directory. training_chunk_dir: Directory where gathered training chunks are. generation: Which generation you are training. params: A MiniGoParams instance of hyperparameters for the model. """ new_model_name = utils.generate_model_name(generation) print('New model will be {}'.format(new_model_name)) new_model = os.path.join(trained_models_dir, new_model_name) print('Training on gathered game data...') tf_records = sorted( tf.gfile.Glob(os.path.join(training_chunk_dir, '*'+_TF_RECORD_SUFFIX))) tf_records = tf_records[ -(params.train_window_size // params.examples_per_chunk):] print('Training from: {} to {}'.format(tf_records[0], tf_records[-1])) with utils.logged_timer('Training'): dualnet.train(estimator_model_dir, tf_records, generation, params) dualnet.export_model(estimator_model_dir, new_model)
Example #9
Source File: main.py From training_results_v0.5 with Apache License 2.0 | 6 votes |
def evaluate_both( prev_model: 'The path to previous model', cur_model: 'The path to current model', output_dir: 'Where to write the evaluation results'='sgf/evaluate', readouts: 'How many readouts to make per move.'=200, games: 'the number of games to play'=20, verbose: 'How verbose the players should be (see selfplay)' = 1): qmeas.start_time('evaluate') _ensure_dir_exists(output_dir) winners = [] with timer("%d games" % games): winners = evaluation.play_match_many_instance_both( prev_model, cur_model, games, readouts, output_dir, verbose) qmeas.stop_time('evaluate') white_count = 0 for win in winners: if 'W' in win or 'w' in win: white_count += 1 return white_count * 1.0 / (games*2) # qmeas.report_profiler()
Example #10
Source File: main.py From training_results_v0.5 with Apache License 2.0 | 6 votes |
def validate( working_dir: 'tf.estimator working directory', *tf_record_dirs: 'Directories where holdout data are', checkpoint_name: 'Which checkpoint to evaluate (None=latest)'=None, validate_name: 'Name for validation set (i.e., selfplay or human)'=None): qmeas.start_time('validate') tf_records = [] with timer("Building lists of holdout files"): for record_dir in tf_record_dirs: tf_records.extend(gfile.Glob(os.path.join(record_dir, '*.zz'))) first_record = os.path.basename(tf_records[0]) last_record = os.path.basename(tf_records[-1]) with timer("Validating from {} to {}".format(first_record, last_record)): dual_net.validate( working_dir, tf_records, checkpoint_name=checkpoint_name, name=validate_name) qmeas.stop_time('validate')
Example #11
Source File: minigo.py From g-tensorflow-models with Apache License 2.0 | 5 votes |
def validate(trained_models_dir, holdout_dir, estimator_model_dir, params): """Validate the latest model on the holdout dataset. Args: trained_models_dir: Directories where the completed generations/models are. holdout_dir: Directories where holdout data are. estimator_model_dir: tf.estimator model directory. params: A MiniGoParams instance of hyperparameters for the model. """ model_num, _ = utils.get_latest_model(trained_models_dir) # Get the holdout game data nums_names = utils.get_models(trained_models_dir) # Model N was trained on games up through model N-1, so the validation set # should only be for models through N-1 as well, thus the (model_num) term. models = [num_name for num_name in nums_names if num_name[0] < model_num] # pair is a tuple of (model_num, model_name), like (13, 000013-modelname) holdout_dirs = [os.path.join(holdout_dir, pair[1]) for pair in models[-params.holdout_generation:]] tf_records = [] with utils.logged_timer('Building lists of holdout files'): for record_dir in holdout_dirs: if os.path.exists(record_dir): # make sure holdout dir exists tf_records.extend( tf.gfile.Glob(os.path.join(record_dir, '*'+_TF_RECORD_SUFFIX))) if not tf_records: print('No holdout dataset for validation! ' 'Please check your holdout directory: {}'.format(holdout_dir)) return print('The length of tf_records is {}.'.format(len(tf_records))) first_tf_record = os.path.basename(tf_records[0]) last_tf_record = os.path.basename(tf_records[-1]) with utils.logged_timer('Validating from {} to {}'.format( first_tf_record, last_tf_record)): dualnet.validate(estimator_model_dir, tf_records, params)
Example #12
Source File: minigo.py From g-tensorflow-models with Apache License 2.0 | 5 votes |
def evaluate(black_model_name, black_net, white_model_name, white_net, evaluate_dir, params): """Evaluate with two models. With two DualNetRunners to play as black and white in a Go match. Two models play several games, and the model that wins by a margin of 55% will be the winner. Args: black_model_name: The name of the model playing black. black_net: The DualNetRunner model for black white_model_name: The name of the model playing white. white_net: The DualNetRunner model for white. evaluate_dir: Where to write the evaluation results. Set as 'base_dir/sgf/evaluate/'. params: A MiniGoParams instance of hyperparameters for the model. Returns: The model name of the winner. Raises: ValueError: if neither `WHITE` or `BLACK` is returned. """ with utils.logged_timer('{} games'.format(params.eval_games)): winner = evaluation.play_match( params, black_net, white_net, params.eval_games, params.eval_readouts, evaluate_dir, params.eval_verbose) if winner != go.WHITE_NAME and winner != go.BLACK_NAME: raise ValueError('Winner should be either White or Black!') return black_model_name if winner == go.BLACK_NAME else white_model_name
Example #13
Source File: main.py From training_results_v0.5 with Apache License 2.0 | 5 votes |
def train( working_dir: 'tf.estimator working directory.', chunk_dir: 'Directory where gathered training chunks are.', model_save_path: 'Where to export the completed generation.', generation_num: 'Which generation you are training.'=0): qmeas.start_time('train') tf_records = sorted(gfile.Glob(os.path.join(chunk_dir, '*.tfrecord.zz'))) tf_records = tf_records[-1 * (WINDOW_SIZE // EXAMPLES_PER_RECORD):] print("Training from:", tf_records[0], "to", tf_records[-1]) with timer("Training"): dual_net.train(working_dir, tf_records, generation_num) dual_net.export_model(working_dir, model_save_path) qmeas.stop_time('train')
Example #14
Source File: train.py From training with Apache License 2.0 | 5 votes |
def main(argv): """Train on examples and export the updated model weights.""" tf_records = argv[1:] logging.info("Training on %s records: %s to %s", len(tf_records), tf_records[0], tf_records[-1]) with utils.logged_timer("Training"): train(*tf_records) if FLAGS.export_path: dual_net.export_model(FLAGS.export_path) if FLAGS.freeze: if FLAGS.use_tpu: dual_net.freeze_graph_tpu(FLAGS.export_path) else: dual_net.freeze_graph(FLAGS.export_path, FLAGS.use_trt, FLAGS.trt_max_batch_size, FLAGS.trt_precision)
Example #15
Source File: selfplay.py From training with Apache License 2.0 | 5 votes |
def run_game(load_file, selfplay_dir=None, holdout_dir=None, sgf_dir=None, holdout_pct=0.05): """Takes a played game and record results and game data.""" if sgf_dir is not None: minimal_sgf_dir = os.path.join(sgf_dir, 'clean') full_sgf_dir = os.path.join(sgf_dir, 'full') utils.ensure_dir_exists(minimal_sgf_dir) utils.ensure_dir_exists(full_sgf_dir) if selfplay_dir is not None: utils.ensure_dir_exists(selfplay_dir) utils.ensure_dir_exists(holdout_dir) with utils.logged_timer("Loading weights from %s ... " % load_file): network = dual_net.DualNetwork(load_file) with utils.logged_timer("Playing game"): player = play(network) output_name = '{}-{}'.format(int(time.time()), socket.gethostname()) game_data = player.extract_data() if sgf_dir is not None: with gfile.GFile(os.path.join(minimal_sgf_dir, '{}.sgf'.format(output_name)), 'w') as f: f.write(player.to_sgf(use_comments=False)) with gfile.GFile(os.path.join(full_sgf_dir, '{}.sgf'.format(output_name)), 'w') as f: f.write(player.to_sgf()) tf_examples = preprocessing.make_dataset_from_selfplay(game_data) if selfplay_dir is not None: # Hold out 5% of games for validation. if random.random() < holdout_pct: fname = os.path.join(holdout_dir, "{}.tfrecord.zz".format(output_name)) else: fname = os.path.join(selfplay_dir, "{}.tfrecord.zz".format(output_name)) preprocessing.write_tf_examples(fname, tf_examples)
Example #16
Source File: minigo.py From multilabel-image-classification-tensorflow with MIT License | 5 votes |
def validate(trained_models_dir, holdout_dir, estimator_model_dir, params): """Validate the latest model on the holdout dataset. Args: trained_models_dir: Directories where the completed generations/models are. holdout_dir: Directories where holdout data are. estimator_model_dir: tf.estimator model directory. params: A MiniGoParams instance of hyperparameters for the model. """ model_num, _ = utils.get_latest_model(trained_models_dir) # Get the holdout game data nums_names = utils.get_models(trained_models_dir) # Model N was trained on games up through model N-1, so the validation set # should only be for models through N-1 as well, thus the (model_num) term. models = [num_name for num_name in nums_names if num_name[0] < model_num] # pair is a tuple of (model_num, model_name), like (13, 000013-modelname) holdout_dirs = [os.path.join(holdout_dir, pair[1]) for pair in models[-params.holdout_generation:]] tf_records = [] with utils.logged_timer('Building lists of holdout files'): for record_dir in holdout_dirs: if os.path.exists(record_dir): # make sure holdout dir exists tf_records.extend( tf.gfile.Glob(os.path.join(record_dir, '*'+_TF_RECORD_SUFFIX))) if not tf_records: print('No holdout dataset for validation! ' 'Please check your holdout directory: {}'.format(holdout_dir)) return print('The length of tf_records is {}.'.format(len(tf_records))) first_tf_record = os.path.basename(tf_records[0]) last_tf_record = os.path.basename(tf_records[-1]) with utils.logged_timer('Validating from {} to {}'.format( first_tf_record, last_tf_record)): dualnet.validate(estimator_model_dir, tf_records, params)
Example #17
Source File: oneoff_utils.py From training with Apache License 2.0 | 5 votes |
def load_player(model_path): print("Loading weights from %s ... " % model_path) with logged_timer("Loading weights from %s ... " % model_path): network = dual_net.DualNetwork(model_path) network.name = os.path.basename(model_path) player = MCTSPlayer(network) return player
Example #18
Source File: controller.py From Python-Reinforcement-Learning-Projects with MIT License | 5 votes |
def evaluate(black_model, white_model): os.makedirs(PATHS.SGF_DIR, exist_ok=True) with timer("Loading weights"): black_net = network.PolicyValueNetwork(black_model) white_net = network.PolicyValueNetwork(white_model) with timer("Playing {} games".format(GLOBAL_PARAMETER_STORE.EVALUATION_GAMES)): play_match(black_net, white_net, GLOBAL_PARAMETER_STORE.EVALUATION_GAMES, GLOBAL_PARAMETER_STORE.EVALUATION_READOUTS, PATHS.SGF_DIR)
Example #19
Source File: controller.py From Python-Reinforcement-Learning-Projects with MIT License | 5 votes |
def validate(model_version=None, validate_name=None): if model_version is None: model_version, model_name = get_latest_model() else: model_version = int(model_version) model_name = get_model(model_version) models = list( filter(lambda num_name: num_name[0] < (model_version - 1), get_models())) if len(models) == 0: logger.info('Not enough models, including model N for validation') models = list( filter(lambda num_name: num_name[0] <= model_version, get_models())) else: logger.info('Validating using data from following models: {}'.format(models)) tf_record_dirs = [os.path.join(PATHS.HOLDOUT_DIR, pair[1]) for pair in models[-5:]] working_dir = PATHS.ESTIMATOR_WORKING_DIR checkpoint_name = os.path.join(PATHS.MODELS_DIR, model_name) tf_records = [] with timer("Building lists of holdout files"): for record_dir in tf_record_dirs: tf_records.extend(gfile.Glob(os.path.join(record_dir, '*.zz'))) with timer("Validating from {} to {}".format(os.path.basename(tf_records[0]), os.path.basename(tf_records[-1]))): network.validate(working_dir, tf_records, checkpoint_path=checkpoint_name, name=validate_name)
Example #20
Source File: minigo.py From multilabel-image-classification-tensorflow with MIT License | 5 votes |
def evaluate(black_model_name, black_net, white_model_name, white_net, evaluate_dir, params): """Evaluate with two models. With two DualNetRunners to play as black and white in a Go match. Two models play several games, and the model that wins by a margin of 55% will be the winner. Args: black_model_name: The name of the model playing black. black_net: The DualNetRunner model for black white_model_name: The name of the model playing white. white_net: The DualNetRunner model for white. evaluate_dir: Where to write the evaluation results. Set as 'base_dir/sgf/evaluate/'. params: A MiniGoParams instance of hyperparameters for the model. Returns: The model name of the winner. Raises: ValueError: if neither `WHITE` or `BLACK` is returned. """ with utils.logged_timer('{} games'.format(params.eval_games)): winner = evaluation.play_match( params, black_net, white_net, params.eval_games, params.eval_readouts, evaluate_dir, params.eval_verbose) if winner != go.WHITE_NAME and winner != go.BLACK_NAME: raise ValueError('Winner should be either White or Black!') return black_model_name if winner == go.BLACK_NAME else white_model_name
Example #21
Source File: training_curve.py From training_results_v0.5 with Apache License 2.0 | 5 votes |
def load_player(model_path): print("Loading weights from %s ... " % model_path) with timer("Loading weights from %s ... " % model_path): network = dual_net.DualNetwork(model_path) network.name = os.path.basename(model_path) player = MCTSPlayer(network, verbosity=2) return player
Example #22
Source File: main.py From training_results_v0.5 with Apache License 2.0 | 5 votes |
def evaluate( black_model: 'The path to the model to play black', white_model: 'The path to the model to play white', output_dir: 'Where to write the evaluation results'='sgf/evaluate', readouts: 'How many readouts to make per move.'=200, games: 'the number of games to play'=20, verbose: 'How verbose the players should be (see selfplay)' = 1): qmeas.start_time('evaluate') _ensure_dir_exists(output_dir) with timer("Loading weights"): black_net = dual_net.DualNetwork(black_model) white_net = dual_net.DualNetwork(white_model) winners = [] with timer("%d games" % games): winners = evaluation.play_match( black_net, white_net, games, readouts, output_dir, verbose) qmeas.stop_time('evaluate') white_count = 0 for win in winners: if 'W' in win or 'w' in win: white_count += 1 return white_count * 1.0 / games # qmeas.report_profiler()
Example #23
Source File: minigo.py From Gun-Detector with Apache License 2.0 | 5 votes |
def validate(trained_models_dir, holdout_dir, estimator_model_dir, params): """Validate the latest model on the holdout dataset. Args: trained_models_dir: Directories where the completed generations/models are. holdout_dir: Directories where holdout data are. estimator_model_dir: tf.estimator model directory. params: An object of hyperparameters for the model. """ model_num, _ = utils.get_latest_model(trained_models_dir) # Get the holdout game data nums_names = utils.get_models(trained_models_dir) # Model N was trained on games up through model N-1, so the validation set # should only be for models through N-1 as well, thus the (model_num) term. models = [num_name for num_name in nums_names if num_name[0] < model_num] # pair is a tuple of (model_num, model_name), like (13, 000013-modelname) holdout_dirs = [os.path.join(holdout_dir, pair[1]) for pair in models[-params.holdout_generation:]] tf_records = [] with utils.logged_timer('Building lists of holdout files'): for record_dir in holdout_dirs: if os.path.exists(record_dir): # make sure holdout dir exists tf_records.extend( tf.gfile.Glob(os.path.join(record_dir, '*'+_TF_RECORD_SUFFIX))) print('The length of tf_records is {}.'.format(len(tf_records))) first_tf_record = os.path.basename(tf_records[0]) last_tf_record = os.path.basename(tf_records[-1]) with utils.logged_timer('Validating from {} to {}'.format( first_tf_record, last_tf_record)): dualnet.validate(estimator_model_dir, tf_records, params)
Example #24
Source File: main.py From training_results_v0.5 with Apache License 2.0 | 5 votes |
def selfplay_cache_model( network: "The path to the network model files", output_dir: "Where to write the games"="data/selfplay", holdout_dir: "Where to write the games"="data/holdout", output_sgf: "Where to write the sgfs"="sgf/", readouts: 'How many simulations to run per move'=100, verbose: '>=2 will print debug info, >=3 will print boards' = 1, resign_threshold: 'absolute value of threshold to resign at' = 0.95, holdout_pct: 'how many games to hold out for validation' = 0.05): qmeas.start_time('selfplay') clean_sgf = os.path.join(output_sgf, 'clean') full_sgf = os.path.join(output_sgf, 'full') _ensure_dir_exists(clean_sgf) _ensure_dir_exists(full_sgf) _ensure_dir_exists(output_dir) _ensure_dir_exists(holdout_dir) with timer("Playing game"): player = selfplay_mcts.play( network, readouts, resign_threshold, verbose) output_name = '{}-{}'.format(int(time.time() * 1000 * 1000), socket.gethostname()) game_data = player.extract_data() with gfile.GFile(os.path.join(clean_sgf, '{}.sgf'.format(output_name)), 'w') as f: f.write(player.to_sgf(use_comments=False)) with gfile.GFile(os.path.join(full_sgf, '{}.sgf'.format(output_name)), 'w') as f: f.write(player.to_sgf()) tf_examples = preprocessing.make_dataset_from_selfplay(game_data) # Hold out 5% of games for evaluation. if random.random() < holdout_pct: fname = os.path.join(holdout_dir, "{}.tfrecord.zz".format(output_name)) else: fname = os.path.join(output_dir, "{}.tfrecord.zz".format(output_name)) preprocessing.write_tf_examples(fname, tf_examples) qmeas.stop_time('selfplay')
Example #25
Source File: main.py From training_results_v0.5 with Apache License 2.0 | 5 votes |
def selfplay_cache_model( network: "The path to the network model files", output_dir: "Where to write the games"="data/selfplay", holdout_dir: "Where to write the games"="data/holdout", output_sgf: "Where to write the sgfs"="sgf/", readouts: 'How many simulations to run per move'=100, verbose: '>=2 will print debug info, >=3 will print boards' = 1, resign_threshold: 'absolute value of threshold to resign at' = 0.95, holdout_pct: 'how many games to hold out for validation' = 0.05): qmeas.start_time('selfplay') clean_sgf = os.path.join(output_sgf, 'clean') full_sgf = os.path.join(output_sgf, 'full') _ensure_dir_exists(clean_sgf) _ensure_dir_exists(full_sgf) _ensure_dir_exists(output_dir) _ensure_dir_exists(holdout_dir) with timer("Playing game"): player = selfplay_mcts.play( network, readouts, resign_threshold, verbose) output_name = '{}-{}'.format(int(time.time() * 1000 * 1000), socket.gethostname()) game_data = player.extract_data() with gfile.GFile(os.path.join(clean_sgf, '{}.sgf'.format(output_name)), 'w') as f: f.write(player.to_sgf(use_comments=False)) with gfile.GFile(os.path.join(full_sgf, '{}.sgf'.format(output_name)), 'w') as f: f.write(player.to_sgf()) tf_examples = preprocessing.make_dataset_from_selfplay(game_data) # Hold out 5% of games for evaluation. if random.random() < holdout_pct: fname = os.path.join(holdout_dir, "{}.tfrecord.zz".format(output_name)) else: fname = os.path.join(output_dir, "{}.tfrecord.zz".format(output_name)) preprocessing.write_tf_examples(fname, tf_examples) qmeas.stop_time('selfplay')
Example #26
Source File: main.py From training_results_v0.5 with Apache License 2.0 | 5 votes |
def selfplay( load_file: "The path to the network model files", output_dir: "Where to write the games"="data/selfplay", holdout_dir: "Where to write the games"="data/holdout", output_sgf: "Where to write the sgfs"="sgf/", readouts: 'How many simulations to run per move'=100, verbose: '>=2 will print debug info, >=3 will print boards' = 1, resign_threshold: 'absolute value of threshold to resign at' = 0.95, holdout_pct: 'how many games to hold out for validation' = 0.05): qmeas.start_time('selfplay') clean_sgf = os.path.join(output_sgf, 'clean') full_sgf = os.path.join(output_sgf, 'full') _ensure_dir_exists(clean_sgf) _ensure_dir_exists(full_sgf) _ensure_dir_exists(output_dir) _ensure_dir_exists(holdout_dir) with timer("Loading weights from %s ... " % load_file): network = dual_net.DualNetwork(load_file) with timer("Playing game"): player = selfplay_mcts.play( network, readouts, resign_threshold, verbose) output_name = '{}-{}'.format(int(time.time() * 1000 * 1000), socket.gethostname()) game_data = player.extract_data() with gfile.GFile(os.path.join(clean_sgf, '{}.sgf'.format(output_name)), 'w') as f: f.write(player.to_sgf(use_comments=False)) with gfile.GFile(os.path.join(full_sgf, '{}.sgf'.format(output_name)), 'w') as f: f.write(player.to_sgf()) tf_examples = preprocessing.make_dataset_from_selfplay(game_data) # Hold out 5% of games for evaluation. if random.random() < holdout_pct: fname = os.path.join(holdout_dir, "{}.tfrecord.zz".format(output_name)) else: fname = os.path.join(output_dir, "{}.tfrecord.zz".format(output_name)) preprocessing.write_tf_examples(fname, tf_examples) qmeas.stop_time('selfplay')
Example #27
Source File: training_curve.py From training_results_v0.5 with Apache License 2.0 | 5 votes |
def load_player(model_path): print("Loading weights from %s ... " % model_path) with timer("Loading weights from %s ... " % model_path): network = dual_net.DualNetwork(model_path) network.name = os.path.basename(model_path) player = MCTSPlayer(network, verbosity=2) return player
Example #28
Source File: main.py From training_results_v0.5 with Apache License 2.0 | 5 votes |
def train( working_dir: 'tf.estimator working directory.', chunk_dir: 'Directory where gathered training chunks are.', model_save_path: 'Where to export the completed generation.', generation_num: 'Which generation you are training.'=0): qmeas.start_time('train') tf_records = sorted(gfile.Glob(os.path.join(chunk_dir, '*.tfrecord.zz'))) tf_records = tf_records[-1 * (WINDOW_SIZE // EXAMPLES_PER_RECORD):] print("Training from:", tf_records[0], "to", tf_records[-1]) with timer("Training"): dual_net.train(working_dir, tf_records, generation_num) dual_net.export_model(working_dir, model_save_path) qmeas.stop_time('train')
Example #29
Source File: minigo.py From g-tensorflow-models with Apache License 2.0 | 4 votes |
def selfplay(selfplay_dirs, selfplay_model, params): """Perform selfplay with a specific model. Args: selfplay_dirs: A dict to specify the directories used in selfplay. selfplay_dirs = { 'output_dir': output_dir, 'holdout_dir': holdout_dir, 'clean_sgf': clean_sgf, 'full_sgf': full_sgf } selfplay_model: The actual Dualnet runner for selfplay. params: A MiniGoParams instance of hyperparameters for the model. """ with utils.logged_timer('Playing game'): player = selfplay_mcts.play( params.board_size, selfplay_model, params.selfplay_readouts, params.selfplay_resign_threshold, params.simultaneous_leaves, params.selfplay_verbose) output_name = '{}-{}'.format(int(time.time()), socket.gethostname()) def _write_sgf_data(dir_sgf, use_comments): with tf.gfile.GFile( os.path.join(dir_sgf, '{}.sgf'.format(output_name)), 'w') as f: f.write(player.to_sgf(use_comments=use_comments)) _write_sgf_data(selfplay_dirs['clean_sgf'], use_comments=False) _write_sgf_data(selfplay_dirs['full_sgf'], use_comments=True) game_data = player.extract_data() tf_examples = preprocessing.make_dataset_from_selfplay(game_data, params) # Hold out 5% of games for evaluation. if random.random() < params.holdout_pct: fname = os.path.join( selfplay_dirs['holdout_dir'], output_name + _TF_RECORD_SUFFIX) else: fname = os.path.join( selfplay_dirs['output_dir'], output_name + _TF_RECORD_SUFFIX) preprocessing.write_tf_examples(fname, tf_examples)
Example #30
Source File: minigo.py From multilabel-image-classification-tensorflow with MIT License | 4 votes |
def gather(selfplay_dir, training_chunk_dir, params): """Gather selfplay data into large training chunk. Args: selfplay_dir: Where to look for games. Set as 'base_dir/data/selfplay/'. training_chunk_dir: where to put collected games. Set as 'base_dir/data/training_chunks/'. params: A MiniGoParams instance of hyperparameters for the model. """ # Check the selfplay data from the most recent 50 models. _ensure_dir_exists(training_chunk_dir) sorted_model_dirs = sorted(tf.gfile.ListDirectory(selfplay_dir)) models = [model_dir.strip('/') for model_dir in sorted_model_dirs[-params.gather_generation:]] with utils.logged_timer('Finding existing tfrecords...'): model_gamedata = { model: tf.gfile.Glob( os.path.join(selfplay_dir, model, '*'+_TF_RECORD_SUFFIX)) for model in models } print('Found {} models'.format(len(models))) for model_name, record_files in sorted(model_gamedata.items()): print(' {}: {} files'.format(model_name, len(record_files))) meta_file = os.path.join(training_chunk_dir, 'meta.txt') try: with tf.gfile.GFile(meta_file, 'r') as f: already_processed = set(f.read().split()) except tf.errors.NotFoundError: already_processed = set() num_already_processed = len(already_processed) for model_name, record_files in sorted(model_gamedata.items()): if set(record_files) <= already_processed: continue print('Gathering files from {}:'.format(model_name)) tf_examples = preprocessing.shuffle_tf_examples( params.shuffle_buffer_size, params.examples_per_chunk, record_files) # tqdm to make the loops show a smart progress meter for i, example_batch in enumerate(tf_examples): output_record = os.path.join( training_chunk_dir, ('{}-{}'+_TF_RECORD_SUFFIX).format(model_name, str(i))) preprocessing.write_tf_examples( output_record, example_batch, serialize=False) already_processed.update(record_files) print('Processed {} new files'.format( len(already_processed) - num_already_processed)) with tf.gfile.GFile(meta_file, 'w') as f: f.write('\n'.join(sorted(already_processed)))