Python tensorflow.contrib.learn.python.learn.learn_runner.run() Examples

The following are 30 code examples of tensorflow.contrib.learn.python.learn.learn_runner.run(). 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 tensorflow.contrib.learn.python.learn.learn_runner , or try the search function .
Example #1
Source File: task.py    From cloudml-samples with Apache License 2.0 6 votes vote down vote up
def main(argv=None):
  """Run a Tensorflow model on the Movielens dataset."""
  env = json.loads(os.environ.get('TF_CONFIG', '{}'))
  # First find out if there's a task value on the environment variable.
  # If there is none or it is empty define a default one.
  task_data = env.get('task') or {'type': 'master', 'index': 0}
  argv = sys.argv if argv is None else argv
  args = create_parser().parse_args(args=argv[1:])

  trial = task_data.get('trial')
  if trial is not None:
    output_path = os.path.join(args.output_path, trial)
  else:
    output_path = args.output_path

  learn_runner.run(experiment_fn=make_experiment_fn(args),
                   output_dir=output_path) 
Example #2
Source File: task.py    From pydatalab with Apache License 2.0 6 votes vote down vote up
def local_analysis(args):
  if args.analysis:
    # Already analyzed.
    return

  if not args.schema or not args.features:
    raise ValueError('Either --analysis, or both --schema and --features are provided.')

  tf_config = json.loads(os.environ.get('TF_CONFIG', '{}'))
  cluster_spec = tf_config.get('cluster', {})
  if len(cluster_spec.get('worker', [])) > 0:
    raise ValueError('If "schema" and "features" are provided, local analysis will run and ' +
                     'only BASIC scale-tier (no workers node) is supported.')

  if cluster_spec and not (args.schema.startswith('gs://') and args.features.startswith('gs://')):
    raise ValueError('Cloud trainer requires GCS paths for --schema and --features.')

  print('Running analysis.')
  schema = json.loads(file_io.read_file_to_string(args.schema).decode())
  features = json.loads(file_io.read_file_to_string(args.features).decode())
  args.analysis = os.path.join(args.job_dir, 'analysis')
  args.transform = True
  file_io.recursive_create_dir(args.analysis)
  feature_analysis.run_local_analysis(args.analysis, args.train, schema, features)
  print('Analysis done.') 
Example #3
Source File: task.py    From pydatalab with Apache License 2.0 6 votes vote down vote up
def local_analysis(args):
  if args.analysis:
    # Already analyzed.
    return

  if not args.schema or not args.features:
    raise ValueError('Either --analysis, or both --schema and --features are provided.')

  tf_config = json.loads(os.environ.get('TF_CONFIG', '{}'))
  cluster_spec = tf_config.get('cluster', {})
  if len(cluster_spec.get('worker', [])) > 0:
    raise ValueError('If "schema" and "features" are provided, local analysis will run and ' +
                     'only BASIC scale-tier (no workers node) is supported.')

  if cluster_spec and not (args.schema.startswith('gs://') and args.features.startswith('gs://')):
    raise ValueError('Cloud trainer requires GCS paths for --schema and --features.')

  print('Running analysis.')
  schema = json.loads(file_io.read_file_to_string(args.schema).decode())
  features = json.loads(file_io.read_file_to_string(args.features).decode())
  args.analysis = os.path.join(args.job_dir, 'analysis')
  args.transform = True
  file_io.recursive_create_dir(args.analysis)
  feature_analysis.run_local_analysis(args.analysis, args.train, schema, features)
  print('Analysis done.') 
Example #4
Source File: variants_inference.py    From cloudml-examples with Apache License 2.0 6 votes vote down vote up
def main(unused_argv):
  if not FLAGS.input_dir:
    raise ValueError("Input dir should be specified.")

  if FLAGS.eval_dir:
    train_file_pattern = os.path.join(FLAGS.input_dir, "examples*")
    eval_file_pattern = os.path.join(FLAGS.eval_dir, "examples*")
  else:
    train_file_pattern = os.path.join(FLAGS.input_dir, "examples*[0-7]-of-*")
    eval_file_pattern = os.path.join(FLAGS.input_dir, "examples*[89]-of-*")

  if not FLAGS.num_classes:
    raise ValueError("Number of classes should be specified.")

  if not FLAGS.sparse_features:
    raise ValueError("Name of the sparse features should be specified.")

  learn_runner.run(
      experiment_fn=_def_experiment(
          train_file_pattern,
          eval_file_pattern,
          FLAGS.batch_size),
      output_dir=FLAGS.export_dir) 
Example #5
Source File: task.py    From cloudml-samples with Apache License 2.0 6 votes vote down vote up
def main(argv=None):
  """Run a Tensorflow model on the Reddit dataset."""
  env = json.loads(os.environ.get('TF_CONFIG', '{}'))
  # First find out if there's a task value on the environment variable.
  # If there is none or it is empty define a default one.
  task_data = env.get('task') or {'type': 'master', 'index': 0}
  argv = sys.argv if argv is None else argv
  args = create_parser().parse_args(args=argv[1:])

  trial = task_data.get('trial')
  if trial is not None:
    output_dir = os.path.join(args.output_path, trial)
  else:
    output_dir = args.output_path

  learn_runner.run(experiment_fn=get_experiment_fn(args),
                   output_dir=output_dir) 
Example #6
Source File: model.py    From Elphas with Apache License 2.0 5 votes vote down vote up
def main(unused_argv):
    tf.flags.mark_flag_as_required('model_dir')
    tf.flags.mark_flag_as_required('pipeline_config_path')
    config = tf.contrib.learn.RunConfig(model_dir=FLAGS.model_dir)
    learn_runner.run(
        experiment_fn=_build_experiment_fn(FLAGS.num_train_steps,
                                           FLAGS.num_eval_steps),
        run_config=config,
        hparams=model_hparams.create_hparams()) 
Example #7
Source File: train_eval.py    From conv-ensemble-str with Apache License 2.0 5 votes vote down vote up
def main(_argv):
  """Main function
  """
  schedules = ['train', 'evaluate', 'continuous_eval']
  assert FLAGS.schedule in schedules,\
                      "Only schedules: %s supported!"%(','.join(schedules))

  learn_runner.run(
      experiment_fn=_create_experiment,
      output_dir=FLAGS.output_dir,
      schedule=FLAGS.schedule) 
Example #8
Source File: learn_runner_test.py    From deep_image_model with Apache License 2.0 5 votes vote down vote up
def test_fail_schedule_from_config_with_no_job_name(self):
    config = run_config.RunConfig(
        job_name=None, cluster_spec=build_distributed_cluster_spec())
    self.assertRaisesRegexp(
        ValueError,
        "Must specify a schedule",
        learn_runner.run,
        lambda output_dir: TestExperiment(config=config),
        output_dir="/tmp") 
Example #9
Source File: learn_runner_test.py    From deep_image_model with Apache License 2.0 5 votes vote down vote up
def test_fail_non_callable_task(self):
    self.assertRaisesRegexp(TypeError,
                            "Schedule references non-callable member",
                            learn_runner.run, build_experiment, "/tmp",
                            "default") 
Example #10
Source File: learn_runner_test.py    From deep_image_model with Apache License 2.0 5 votes vote down vote up
def test_fail_non_existent_task(self):
    self.assertRaisesRegexp(ValueError, "Schedule references non-existent task",
                            learn_runner.run, build_experiment, "/tmp",
                            "mirage") 
Example #11
Source File: learn_runner_test.py    From deep_image_model with Apache License 2.0 5 votes vote down vote up
def test_fail_not_experiment(self):
    self.assertRaisesRegexp(TypeError,
                            "Experiment builder did not return an Experiment",
                            learn_runner.run, build_non_experiment, "/tmp",
                            "simple_test") 
Example #12
Source File: learn_runner_test.py    From deep_image_model with Apache License 2.0 5 votes vote down vote up
def test_fail_non_callable(self):
    self.assertRaisesRegexp(TypeError, "Experiment builder .* is not callable",
                            learn_runner.run, "not callable", "/tmp",
                            "simple_test") 
Example #13
Source File: learn_runner_test.py    From deep_image_model with Apache License 2.0 5 votes vote down vote up
def test_schedule_from_manually_specified_job_name(self):
    config = run_config.RunConfig(
        job_name="worker", cluster_spec=build_distributed_cluster_spec())
    self.assertEqual(
        "train",
        learn_runner.run(lambda output_dir: TestExperiment(config=config),
                         output_dir="/tmp")) 
Example #14
Source File: learn_runner_test.py    From deep_image_model with Apache License 2.0 5 votes vote down vote up
def test_no_schedule_and_non_distributed_runs_train_and_evaluate(self):
    config = run_config.RunConfig(
        cluster_spec=build_non_distributed_cluster_spec())
    self.assertEqual(
        "train_and_evaluate",
        learn_runner.run(lambda output_dir: TestExperiment(config=config),
                         output_dir="/tmp")) 
Example #15
Source File: learn_runner_test.py    From deep_image_model with Apache License 2.0 5 votes vote down vote up
def test_no_schedule_and_no_config_runs_train_and_evaluate(self):
    self.assertEqual(
        "train_and_evaluate",
        learn_runner.run(build_experiment,
                         output_dir="/tmp")) 
Example #16
Source File: learn_runner_test.py    From deep_image_model with Apache License 2.0 5 votes vote down vote up
def test_fail_no_output_dir(self):
    self.assertRaisesRegexp(ValueError, "Must specify an output directory",
                            learn_runner.run, build_experiment, "",
                            "simple_task") 
Example #17
Source File: learn_runner_test.py    From deep_image_model with Apache License 2.0 5 votes vote down vote up
def test_schedule_from_config_runs_train_on_worker(self):
    config = run_config.RunConfig(
        job_name="worker", cluster_spec=build_distributed_cluster_spec())
    self.assertEqual(
        "train",
        learn_runner.run(lambda output_dir: TestExperiment(config=config),
                         output_dir="/tmp")) 
Example #18
Source File: learn_runner_test.py    From deep_image_model with Apache License 2.0 5 votes vote down vote up
def test_schedule_from_config_runs_serve_on_ps(self):
    config = run_config.RunConfig(
        job_name="ps", cluster_spec=build_distributed_cluster_spec())
    self.assertEqual(
        "run_std_server",
        learn_runner.run(lambda output_dir: TestExperiment(config=config),
                         output_dir="/tmp")) 
Example #19
Source File: learn_runner_test.py    From deep_image_model with Apache License 2.0 5 votes vote down vote up
def test_schedule_from_tf_config(self):
    os.environ["TF_CONFIG"] = json.dumps(
        {"cluster": build_distributed_cluster_spec().as_dict(),
         "task": {"type": "worker"}})
    # RunConfig constructor will set job_name from TF_CONFIG.
    config = run_config.RunConfig()
    self.assertEqual(
        "train",
        learn_runner.run(lambda output_dir: TestExperiment(config=config),
                         output_dir="/tmp")) 
Example #20
Source File: model.py    From ros_people_object_detection_tensorflow with Apache License 2.0 5 votes vote down vote up
def main(unused_argv):
  tf.flags.mark_flag_as_required('model_dir')
  tf.flags.mark_flag_as_required('pipeline_config_path')
  config = tf.contrib.learn.RunConfig(model_dir=FLAGS.model_dir)
  learn_runner.run(
      experiment_fn=build_experiment_fn(FLAGS.num_train_steps,
                                        FLAGS.num_eval_steps),
      run_config=config,
      hparams=model_hparams.create_hparams()) 
Example #21
Source File: model.py    From Traffic-Rule-Violation-Detection-System with MIT License 5 votes vote down vote up
def main(unused_argv):
  tf.flags.mark_flag_as_required('model_dir')
  tf.flags.mark_flag_as_required('pipeline_config_path')
  config = tf.contrib.learn.RunConfig(model_dir=FLAGS.model_dir)
  learn_runner.run(
      experiment_fn=_build_experiment_fn(FLAGS.num_train_steps,
                                         FLAGS.num_eval_steps),
      run_config=config,
      hparams=model_hparams.create_hparams()) 
Example #22
Source File: g2p_trainer_utils.py    From NETransliteration-COLING2018 with MIT License 5 votes vote down vote up
def run(params, problem_instance, train_preprocess_file_path,
        dev_preprocess_file_path):
  """Runs an Estimator locally or distributed.

  Args:
    data_dir: The directory the data can be found in.
    model_name: The name of the model to use.
    output_dir: The directory to store outputs in.
    train_steps: The number of steps to run training for.
    eval_steps: The number of steps to run evaluation for.
    schedule: (str) The schedule to run. The value here must
      be the name of one of Experiment's methods.
  """
  train_preprocess_file_path = os.path.abspath(train_preprocess_file_path)
  dev_preprocess_file_path = os.path.abspath(dev_preprocess_file_path)
  print("train preprocess", train_preprocess_file_path, "dev preprocess", dev_preprocess_file_path)
  exp_fn = make_experiment_fn(
      params,
      problem_instance,
      train_preprocess_file_path=train_preprocess_file_path,
      dev_preprocess_file_path=dev_preprocess_file_path)

  # Create hparams and run_config
  #run_config = trainer_utils.create_run_config(params.model_dir)
  run_config = create_run_config(params.model_dir)
  hparams = trainer_utils.create_hparams(
      params.hparams_set,
      params.data_dir,
      passed_hparams=params.hparams)

  if trainer_utils.is_chief():
    trainer_utils.save_metadata(params.model_dir, hparams)

  learn_runner.run(
      experiment_fn=exp_fn,
      schedule=params.schedule,
      run_config=run_config,
      hparams=hparams) 
Example #23
Source File: task.py    From pydatalab with Apache License 2.0 5 votes vote down vote up
def main(argv=None):
  """Run a Tensorflow model on the Iris dataset."""
  args = parse_arguments(sys.argv if argv is None else argv)

  tf.logging.set_verbosity(tf.logging.INFO)
  learn_runner.run(
      experiment_fn=get_experiment_fn(args),
      output_dir=args.job_dir) 
Example #24
Source File: task.py    From pydatalab with Apache License 2.0 5 votes vote down vote up
def __init__(self, epilog=None, datalab_epilog=None):
    self.full_parser = argparse.ArgumentParser(epilog=epilog)
    self.datalab_help = []
    self.datalab_epilog = datalab_epilog

    # Datalab help string
    self.full_parser.add_argument(
        '--datalab-help', action=self.make_datalab_help_action(),
        help='Show a smaller help message for DataLab only and exit')

    # The arguments added here are required to exist by Datalab's "%%ml train" magics.
    self.full_parser.add_argument(
        '--train', type=str, required=True, action='append', metavar='FILE')
    self.full_parser.add_argument(
        '--eval', type=str, required=True, action='append', metavar='FILE')
    self.full_parser.add_argument('--job-dir', type=str, required=True)
    self.full_parser.add_argument(
        '--analysis', type=str,
        metavar='ANALYSIS_OUTPUT_DIR',
        help=('Output folder of analysis. Should contain the schema, stats, and '
              'vocab files. Path must be on GCS if running cloud training. ' +
              'If absent, --schema and --features must be provided and ' +
              'the master trainer will do analysis locally.'))
    self.full_parser.add_argument(
        '--transform', action='store_true', default=False,
        help='If used, input data is raw csv that needs transformation. If analysis ' +
             'is required to run in trainerm this is automatically set to true.')
    self.full_parser.add_argument(
        '--schema', type=str,
        help='Schema of the training csv file. Only needed if analysis is required.')
    self.full_parser.add_argument(
        '--features', type=str,
        help='Feature transform config. Only needed if analysis is required.') 
Example #25
Source File: task.py    From pydatalab with Apache License 2.0 5 votes vote down vote up
def main(argv=None):
  args = parse_arguments(sys.argv if argv is None else argv)
  local_analysis(args)
  set_logging_level(args)
  # Supress TensorFlow Debugging info.
  os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'

  learn_runner.run(
      experiment_fn=get_experiment_fn(args),
      output_dir=args.job_dir) 
Example #26
Source File: task.py    From pydatalab with Apache License 2.0 5 votes vote down vote up
def main(argv=None):
  args = parse_arguments(sys.argv if argv is None else argv)
  local_analysis(args)
  set_logging_level(args)
  # Supress TensorFlow Debugging info.
  os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'

  learn_runner.run(
      experiment_fn=get_experiment_fn(args),
      output_dir=args.job_dir) 
Example #27
Source File: census_widendeep.py    From deep_image_model with Apache License 2.0 5 votes vote down vote up
def main(unused_argv):
  print("Worker index: %d" % FLAGS.worker_index)
  learn_runner.run(experiment_fn=_create_experiment_fn,
                   output_dir=FLAGS.output_dir,
                   schedule=FLAGS.schedule) 
Example #28
Source File: learn_runner_test.py    From deep_image_model with Apache License 2.0 5 votes vote down vote up
def test_run_with_custom_schedule(self):
    self.assertEqual(
        "simple_task, default=None.",
        learn_runner.run(build_experiment,
                         output_dir="/tmp",
                         schedule="simple_task")) 
Example #29
Source File: learn_runner_test.py    From deep_image_model with Apache License 2.0 5 votes vote down vote up
def test_run_with_explicit_local_run(self):
    self.assertEqual(
        "local_run",
        learn_runner.run(build_experiment,
                         output_dir="/tmp",
                         schedule="local_run")) 
Example #30
Source File: task.py    From cloudml-samples with Apache License 2.0 5 votes vote down vote up
def main(argv=None):
  """Run a Tensorflow model on the Criteo dataset."""
  env = json.loads(os.environ.get('TF_CONFIG', '{}'))
  # First find out if there's a task value on the environment variable.
  # If there is none or it is empty define a default one.
  task_data = env.get('task') or {'type': 'master', 'index': 0}
  argv = sys.argv if argv is None else argv
  args = create_parser().parse_args(args=argv[1:])

  trial = task_data.get('trial')
  if trial is not None:
    output_dir = os.path.join(args.output_path, trial)
  else:
    output_dir = args.output_path

  # Do only evaluation if instructed so, or call Experiment's run.
  if args.eval_only_summary_filename:
    experiment = get_experiment_fn(args)(output_dir)
    # Note that evaluation here will appear as 'one_pass' in tensorboard.
    results = experiment.evaluate(delay_secs=0)
    # Converts numpy types to native types for json dumps.
    json_out = json.dumps(
        {key: value.tolist() for key, value in results.iteritems()})
    with tf.Session():
      tf.write_file(args.eval_only_summary_filename, json_out).run()
  else:
    learn_runner.run(experiment_fn=get_experiment_fn(args),
                     output_dir=output_dir)