def main(): tf.logging.set_verbosity(FLAGS.verbosity) train_spec = tf.estimator.TrainSpec( input_fn=data.get_input_fn( FLAGS.train, shuffle=True, batch_size=FLAGS.batch_size ), max_steps=FLAGS.max_steps, ) exporter = tf.estimator.FinalExporter( "estimator", data.json_serving_input_fn, as_text=False, # change to true if you want to export the model as readable text ) eval_spec = tf.estimator.EvalSpec( input_fn=data.get_input_fn(FLAGS.eval), steps=None, throttle_secs=FLAGS.throttle_secs, exporters=[exporter], ) estimator = model.create_estimator() tf.logging.log( tf.logging.INFO, "About to start training and evaulating. To see results type\ntensorboard --logdir={}\nin another window.".format( FLAGS.job_dir ), ) tf.estimator.train_and_evaluate(estimator, train_spec, eval_spec) tf.logging.log( tf.logging.INFO, "Finished training and evaulating. To see results type\ntensorboard --logdir={}\nin another window.".format( FLAGS.job_dir ), )
def _experiment_fn(run_config, hparams): train_input = lambda: input.generate_text_input_fn( hparams.train_files, mode=tf.contrib.learn.ModeKeys.TRAIN, num_epochs=hparams.num_epochs, batch_size=hparams.train_batch_size) eval_input = lambda: input.generate_text_input_fn( hparams.eval_files, mode=tf.contrib.learn.ModeKeys.EVAL, batch_size=hparams.eval_batch_size) if metadata.TASK_TYPE == "classification": estimator = model.create_classifier(config=run_config) elif metadata.TASK_TYPE == "regression": estimator = model.create_regressor(config=run_config) else: estimator = model.create_estimator(config=run_config) return tf.contrib.learn.Experiment(estimator, train_input_fn=train_input, eval_input_fn=eval_input, eval_metrics=get_eval_metrics(), **experiment_args)
def run_experiment(run_config): """Train, evaluate, and export the model using tf.estimator.train_and_evaluate API""" train_input_fn = input.generate_input_fn( file_names_pattern=HYPER_PARAMS.train_files, mode=tf.estimator.ModeKeys.TRAIN, num_epochs=HYPER_PARAMS.num_epochs, batch_size=HYPER_PARAMS.train_batch_size) eval_input_fn = input.generate_input_fn( file_names_pattern=HYPER_PARAMS.eval_files, mode=tf.estimator.ModeKeys.EVAL, batch_size=HYPER_PARAMS.eval_batch_size) exporter = tf.estimator.FinalExporter( 'estimator', input.SERVING_FUNCTIONS[HYPER_PARAMS.export_format], as_text= False # change to true if you want to export the model as readable text ) # compute the number of training steps based on num_epoch, train_size, and train_batch_size if HYPER_PARAMS.train_size is not None and HYPER_PARAMS.num_epochs is not None: train_steps = (HYPER_PARAMS.train_size / HYPER_PARAMS.train_batch_size) * \ HYPER_PARAMS.num_epochs else: train_steps = HYPER_PARAMS.train_steps train_spec = tf.estimator.TrainSpec(train_input_fn, max_steps=int(train_steps)) eval_spec = tf.estimator.EvalSpec( eval_input_fn, steps=HYPER_PARAMS.eval_steps, exporters=[exporter], name='estimator-eval', throttle_secs=HYPER_PARAMS.eval_every_secs, ) print("* experiment configurations") print("===========================") print("Train size: {}".format(HYPER_PARAMS.train_size)) print("Epoch count: {}".format(HYPER_PARAMS.num_epochs)) print("Train batch size: {}".format(HYPER_PARAMS.train_batch_size)) print("Training steps: {} ({})".format( int(train_steps), "supplied" if HYPER_PARAMS.train_size is None else "computed")) print("Evaluate every: {} seconds".format(HYPER_PARAMS.eval_every_secs)) print("===========================") if metadata.TASK_TYPE == "classification": estimator = model.create_classifier(config=run_config) elif metadata.TASK_TYPE == "regression": estimator = model.create_regressor(config=run_config) else: estimator = model.create_estimator(config=run_config) # train and evaluate tf.estimator.train_and_evaluate(estimator, train_spec, eval_spec)
def test_create_estimator(self): """Tests that Tensorfow estimator is created correctly.""" # Hyperparameters to create the Estimator hparams = tf.contrib.training.HParams( job_dir='test_dir', save_checkpoints_steps=1, keep_checkpoint_max=1, num_layers=2, dnn_dropout=0.7, dnn_optimizer='test_optimizer', linear_optimizer='test_optimizer', first_layer_size=10) estimator = model.create_estimator(hparams) self.assertIsInstance(estimator, tf.estimator.Estimator)
def main(unused_argv): train_words, int_to_vocab, vocab_to_int, n_vocab = read_data_from_file( 'data/text8') estimator = create_estimator(n_vocab) if FLAGS.mode == 'train': train(train_words, estimator, int_to_vocab, vocab_to_int) elif FLAGS.mode == 'predict': if FLAGS.test_word is not None: test_words = [vocab_to_int[FLAGS.test_word]] predict(test_words, estimator, int_to_vocab, vocab_to_int) else: valid_words = sample_eval_data() predict(valid_words, estimator, int_to_vocab, vocab_to_int)
def run_experiment(run_config): """Train, evaluate, and export the model using tf.estimator.train_and_evaluate API""" train_input_fn = input.generate_input_fn( file_names_pattern=HYPER_PARAMS.train_files, mode=tf.estimator.ModeKeys.TRAIN, num_epochs=HYPER_PARAMS.num_epochs, batch_size=HYPER_PARAMS.train_batch_size) eval_input_fn = input.generate_input_fn( file_names_pattern=HYPER_PARAMS.eval_files, mode=tf.estimator.ModeKeys.EVAL, batch_size=HYPER_PARAMS.eval_batch_size) exporter = tf.estimator.FinalExporter( 'estimator', input.SERVING_FUNCTIONS[HYPER_PARAMS.export_format], as_text= False # change to true if you want to export the model as readable text ) # compute the number of training steps based on num_epoch, train_size, and train_batch_size if HYPER_PARAMS.train_size is not None and HYPER_PARAMS.num_epochs is not None: train_steps = (HYPER_PARAMS.train_size / HYPER_PARAMS.train_batch_size) * \ HYPER_PARAMS.num_epochs else: train_steps = HYPER_PARAMS.train_steps train_spec = tf.estimator.TrainSpec(train_input_fn, max_steps=int(train_steps)) eval_spec = tf.estimator.EvalSpec( eval_input_fn, steps=HYPER_PARAMS.eval_steps, exporters=[exporter], throttle_secs=HYPER_PARAMS.eval_every_secs, ) print("* experiment configurations") print("===========================") print("Train size: {}".format(HYPER_PARAMS.train_size)) print("Epoch count: {}".format(HYPER_PARAMS.num_epochs)) print("Train batch size: {}".format(HYPER_PARAMS.train_batch_size)) print("Training steps: {} ({})".format( int(train_steps), "supplied" if HYPER_PARAMS.train_size is None else "computed")) print("Evaluate every {} seconds".format(HYPER_PARAMS.eval_every_secs)) print("===========================") if metadata.TASK_TYPE == "classification": estimator = model.create_classifier(config=run_config) elif metadata.TASK_TYPE == "regression": estimator = model.create_regressor(config=run_config) else: estimator = model.create_estimator(config=run_config) # train and evaluate tf.estimator.train_and_evaluate(estimator, train_spec, eval_spec) # This is the export for the Tensorflow Model Analysis tool. if HYPER_PARAMS.export_format in ['CSV', 'EXAMPLE']: eval_receiver_fn = input.TFMA_SERVING_FUNCTIONS[ HYPER_PARAMS.export_format] tfma_export.export_eval_savedmodel( estimator=estimator, export_dir_base=os.path.join(estimator.model_dir, "tfma_export"), eval_input_receiver_fn=eval_receiver_fn) else: tf.logging.info("TFMA doesn't yet support a JSON input receiver. " "The TFMA export will not be created.")
def run_experiment(run_config): """Train, evaluate, and export the model using tf.estimator.train_and_evaluate API""" train_input_fn = generate_input_fn( file_names=HYPER_PARAMS.train_files, mode=tf.estimator.ModeKeys.TRAIN, num_epochs=HYPER_PARAMS.num_epochs, batch_size=HYPER_PARAMS.train_batch_size) eval_input_fn = generate_input_fn( file_names=HYPER_PARAMS.eval_files, mode=tf.estimator.ModeKeys.EVAL, batch_size=HYPER_PARAMS.eval_batch_size, ) exporter = tf.estimator.FinalExporter( 'estimator', serving_input, as_text= False # change to true if you want to export the model as readable text ) # compute the number of training steps based on num_epoch, train_size, and train_batch_size if HYPER_PARAMS.train_size is not None and HYPER_PARAMS.num_epochs is not None: train_steps = (HYPER_PARAMS.train_size / HYPER_PARAMS.train_batch_size) * \ HYPER_PARAMS.num_epochs else: train_steps = HYPER_PARAMS.train_steps train_spec = tf.estimator.TrainSpec(train_input_fn, max_steps=int(train_steps)) print(int(train_steps)) eval_spec = tf.estimator.EvalSpec( eval_input_fn, steps=HYPER_PARAMS.eval_steps, exporters=[exporter], throttle_secs=HYPER_PARAMS.eval_every_secs, ) print(HYPER_PARAMS.eval_steps) def metric_fn(labels, predictions): metrics = { 'mse': tf.metrics.mean_squared_error(labels, predictions['predictions']) } return metrics print("* experiment configurations") print("===========================") print("Train size: {}".format(HYPER_PARAMS.train_size)) print("Epoch count: {}".format(HYPER_PARAMS.num_epochs)) print("Train batch size: {}".format(HYPER_PARAMS.train_batch_size)) print("Training steps: {} ({})".format( int(train_steps), "supplied" if HYPER_PARAMS.train_size is None else "computed")) print("Evaluate every {} seconds".format(HYPER_PARAMS.eval_every_secs)) print("===========================") estimator = model.create_estimator(config=run_config) estimator = tf.contrib.estimator.add_metrics(estimator, metric_fn) # train and evaluate tf.estimator.train_and_evaluate(estimator, train_spec, eval_spec)