Exemple #1
0
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)
Exemple #2
0
    def load_model(self):
        filename = get_latest_model()
        if filename is None: return False

        self.saver = tf.train.import_meta_graph(filename)
        self.saver.restore(self.sess,
                           tf.train.latest_checkpoint('saved_models/'))
        graph = tf.get_default_graph()
        variables = graph.get_collection('trainable_variables')

        #loading weights
        self.W = []
        self.B = []
        i = 0
        while i < len(variables):
            self.W.append(variables[i])
            i += 1
            self.B.append(variables[i])
            i += 1

        #loading tensors
        self.X = graph.get_tensor_by_name("X:0")
        self.Y_ = graph.get_tensor_by_name("Y_:0")
        self.step = graph.get_tensor_by_name("step:0")
        self.pkeep = graph.get_tensor_by_name("pkeep:0")
        self.Y = graph.get_tensor_by_name("Y:0")
        self.loss = graph.get_tensor_by_name("loss:0") * 100
        self.accuracy = graph.get_tensor_by_name("accuracy:0")
        self.minimize = graph.get_operation_by_name("minimize")

        return True
Exemple #3
0
def predict(model_name='lgb',
            pred_start_date='2018-09-01',
            pred_end_date='2018-09-30',
            scaler='',
            use_log=False,
            num_processes=17,
            is_train=False):
    logger.info("开始预测, 当前使用模型:%s" % (model_name))
    test_fe_df = feature_engineering(pred_start_date=pred_start_date,
                                     pred_end_date=pred_end_date,
                                     is_train=False,
                                     num_processes=num_processes)
    logger.info("预测样本数:%s, 分类阈值: %s, 分类每日截断个数:%s" %
                (test_fe_df.shape[0], CLS_RANKING, NUM_SUBMISSION))
    model_save_path = get_latest_model(conf.TRAINED_MODEL_DIR,
                                       '%s.model' % model_name)
    if model_name == 'lgb':
        submission_df = inference_pipeline_ensemble_tree(
            test_fe_df,
            use_log,
            model_save_path=model_save_path,
            model_name=model_name)
    elif model_type == 'stacking':
        # TODO:增加stacking部分inference pipeline
        raise NotImplementedError('%s has not been implemented' % model_name)
    else:
        # TODO:增加神经网络部分
        raise NotImplementedError('%s was not been implemented' % model_name)

    logger.info("%s预测完成!" % model_name)
    return submission_df
Exemple #4
0
    def __init__(self):
        model_path = utils.get_latest_model("..", "voxnet")
        print(model_path)

        self.model = load_model(model_path)
        self.model.summary()
        self.graph = tf.get_default_graph()

        self.voxel_size_meters = 0.1
Exemple #5
0
 def test_get_latest_model(self):
     with tempfile.TemporaryDirectory() as models_dir:
         model1 = '000013-model.meta'
         model2 = '000017-model.meta'
         f1 = open(os.path.join(models_dir, model1), 'w')
         f1.close()
         f2 = open(os.path.join(models_dir, model2), 'w')
         f2.close()
         latest_model = utils.get_latest_model(models_dir)
         self.assertEqual(latest_model, (17, '000017-model'))
Exemple #6
0
 def test_get_latest_model(self):
   with tempfile.TemporaryDirectory() as models_dir:
     model1 = '000013-model.meta'
     model2 = '000017-model.meta'
     f1 = open(os.path.join(models_dir, model1), 'w')
     f1.close()
     f2 = open(os.path.join(models_dir, model2), 'w')
     f2.close()
     latest_model = utils.get_latest_model(models_dir)
     self.assertEqual(latest_model, (17, '000017-model'))
Exemple #7
0
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)
Exemple #8
0
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)
def main(_):
  """Run the reinforcement learning loop."""
  tf.logging.set_verbosity(tf.logging.INFO)

  params = _set_params(FLAGS)

  # A dummy model for debug/testing purpose with fewer games and iterations
  if FLAGS.test:
    params = model_params.DummyMiniGoParams()
    base_dir = FLAGS.base_dir + str(FLAGS.board_size) + '_size_dummy/'
  else:
    # Set directories for models and datasets
    base_dir = FLAGS.base_dir + str(FLAGS.board_size) + '_size/'

  dirs = utils.MiniGoDirectory(base_dir)

  # Run selfplay only if user specifies the argument.
  if FLAGS.selfplay:
    selfplay_model_name = FLAGS.selfplay_model_name or utils.get_latest_model(
        dirs.trained_models_dir)[1]
    max_games = FLAGS.selfplay_max_games or params.max_games_per_generation
    run_selfplay(selfplay_model_name, max_games, dirs, params)
    return

  # Run the RL pipeline
  # if no models have been trained, start from bootstrap model

  if not os.path.isdir(dirs.trained_models_dir):
    print('No trained model exists! Starting from Bootstrap...')
    print('Creating random initial weights...')
    bootstrap(dirs.estimator_model_dir, dirs.trained_models_dir, params)
  else:
    print('A MiniGo base directory has been found! ')
    print('Start from the last checkpoint...')

  _, best_model_so_far = utils.get_latest_model(dirs.trained_models_dir)
  for rl_iter in range(params.max_iters_per_pipeline):
    print('RL_iteration: {}'.format(rl_iter))
    # Self-play with the best model to generate training data
    run_selfplay(
        best_model_so_far, params.max_games_per_generation, dirs, params)

    # gather selfplay data for training
    print('Gathering game output...')
    gather(dirs.selfplay_dir, dirs.training_chunk_dir, params)

    # train the next generation model
    model_num, _ = utils.get_latest_model(dirs.trained_models_dir)
    print('Training on gathered game data...')
    train(dirs.trained_models_dir, dirs.estimator_model_dir,
          dirs.training_chunk_dir, model_num + 1, params)

    # validate the latest model if needed
    if FLAGS.validation:
      print('Validating on the holdout game data...')
      validate(dirs.trained_models_dir, dirs.holdout_dir,
               dirs.estimator_model_dir, params)

    _, current_model = utils.get_latest_model(dirs.trained_models_dir)

    if FLAGS.evaluation:  # Perform evaluation if needed
      print('Evaluate models between {} and {}'.format(
          best_model_so_far, current_model))
      black_model = os.path.join(dirs.trained_models_dir, best_model_so_far)
      white_model = os.path.join(dirs.trained_models_dir, current_model)
      _ensure_dir_exists(dirs.evaluate_dir)
      with utils.logged_timer('Loading weights'):
        black_net = dualnet.DualNetRunner(black_model, params)
        white_net = dualnet.DualNetRunner(white_model, params)

      best_model_so_far = evaluate(
          best_model_so_far, black_net, current_model, white_net,
          dirs.evaluate_dir, params)
      print('Winner of evaluation: {}!'.format(best_model_so_far))
    else:
      best_model_so_far = current_model
Exemple #10
0
def main(_):
  """Run the reinforcement learning loop."""
  tf.logging.set_verbosity(tf.logging.INFO)

  params = _set_params(FLAGS)

  # A dummy model for debug/testing purpose with fewer games and iterations
  if FLAGS.test:
    params = model_params.DummyMiniGoParams()
    base_dir = FLAGS.base_dir + str(FLAGS.board_size) + '_size_dummy/'
  else:
    # Set directories for models and datasets
    base_dir = FLAGS.base_dir + str(FLAGS.board_size) + '_size/'

  dirs = utils.MiniGoDirectory(base_dir)

  # Run selfplay only if user specifies the argument.
  if FLAGS.selfplay:
    selfplay_model_name = FLAGS.selfplay_model_name or utils.get_latest_model(
        dirs.trained_models_dir)[1]
    max_games = FLAGS.selfplay_max_games or params.max_games_per_generation
    run_selfplay(selfplay_model_name, max_games, dirs, params)
    return

  # Run the RL pipeline
  # if no models have been trained, start from bootstrap model

  if not os.path.isdir(dirs.trained_models_dir):
    print('No trained model exists! Starting from Bootstrap...')
    print('Creating random initial weights...')
    bootstrap(dirs.estimator_model_dir, dirs.trained_models_dir, params)
  else:
    print('A MiniGo base directory has been found! ')
    print('Start from the last checkpoint...')

  _, best_model_so_far = utils.get_latest_model(dirs.trained_models_dir)
  for rl_iter in range(params.max_iters_per_pipeline):
    print('RL_iteration: {}'.format(rl_iter))
    # Self-play with the best model to generate training data
    run_selfplay(
        best_model_so_far, params.max_games_per_generation, dirs, params)

    # gather selfplay data for training
    print('Gathering game output...')
    gather(dirs.selfplay_dir, dirs.training_chunk_dir, params)

    # train the next generation model
    model_num, _ = utils.get_latest_model(dirs.trained_models_dir)
    print('Training on gathered game data...')
    train(dirs.trained_models_dir, dirs.estimator_model_dir,
          dirs.training_chunk_dir, model_num + 1, params)

    # validate the latest model if needed
    if FLAGS.validation:
      print('Validating on the holdout game data...')
      validate(dirs.trained_models_dir, dirs.holdout_dir,
               dirs.estimator_model_dir, params)

    _, current_model = utils.get_latest_model(dirs.trained_models_dir)

    if FLAGS.evaluation:  # Perform evaluation if needed
      print('Evaluate models between {} and {}'.format(
          best_model_so_far, current_model))
      black_model = os.path.join(dirs.trained_models_dir, best_model_so_far)
      white_model = os.path.join(dirs.trained_models_dir, current_model)
      _ensure_dir_exists(dirs.evaluate_dir)
      with utils.logged_timer('Loading weights'):
        black_net = dualnet.DualNetRunner(black_model, params)
        white_net = dualnet.DualNetRunner(white_model, params)

      best_model_so_far = evaluate(
          best_model_so_far, black_net, current_model, white_net,
          dirs.evaluate_dir, params)
      print('Winner of evaluation: {}!'.format(best_model_so_far))
    else:
      best_model_so_far = current_model
Exemple #11
0
def main(_):
    """Run the reinforcement learning loop."""
    tf.logging.set_verbosity(tf.logging.INFO)

    params = _set_params_from_board_size(FLAGS.board_size)

    # A dummy model for debug/testing purpose with fewer games and iterations
    if FLAGS.debug:
        params = model_params.DummyMiniGoParams()

    # Set directories for models and datasets
    base_dir = FLAGS.base_dir + str(FLAGS.board_size) + '_board_size/'
    dirs = utils.MiniGoDirectory(base_dir)
    # if no models have been trained, start from bootstrap model
    if os.path.isdir(base_dir) is False:
        print('No trained model exists! Starting from Bootstrap...')
        print('Creating random initial weights...')
        bootstrap(dirs.estimator_model_dir, dirs.trained_models_dir, params)
    else:
        print('A MiniGo base directory has been found! ')
        print('Start from the last checkpoint...')

    _, best_model_so_far = utils.get_latest_model(dirs.trained_models_dir)

    for rl_iter in range(params.max_iters_per_pipeline):
        print('RL_iteration: {}'.format(rl_iter))

        # Self-play to generate at least params.max_games_per_generation games
        selfplay(best_model_so_far, dirs.trained_models_dir, dirs.selfplay_dir,
                 dirs.holdout_dir, dirs.sgf_dir, params)
        games = tf.gfile.Glob(
            os.path.join(dirs.selfplay_dir, best_model_so_far, '*.zz'))
        while len(games) < params.max_games_per_generation:
            selfplay(best_model_so_far, dirs.trained_models_dir,
                     dirs.selfplay_dir, dirs.holdout_dir, dirs.sgf_dir, params)
            if FLAGS.validation:
                params = model_params.DummyValidationParams()
                selfplay(best_model_so_far, dirs.trained_models_dir,
                         dirs.selfplay_dir, dirs.holdout_dir, dirs.sgf_dir,
                         params)
            games = tf.gfile.Glob(
                os.path.join(dirs.selfplay_dir, best_model_so_far, '*.zz'))

        print('Gathering game output...')
        gather(dirs.selfplay_dir, dirs.training_chunk_dir, params)

        print('Training on gathered game data...')
        train(dirs.trained_models_dir, dirs.estimator_model_dir,
              dirs.training_chunk_dir, params)

        if FLAGS.validation:
            print('Validating on the holdout game data...')
            validate(dirs.trained_models_dir, dirs.holdout_dir,
                     dirs.estimator_model_dir, params)

        _, current_model = utils.get_latest_model(dirs.trained_models_dir)
        if FLAGS.evaluation:  # Perform evaluation if needed
            print('Evaluating the latest model...')
            best_model_so_far = evaluate(dirs.trained_models_dir,
                                         best_model_so_far, current_model,
                                         dirs.evaluate_dir, params)
            print('Winner: {}!'.format(best_model_so_far))
        else:
            best_model_so_far = current_model