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 run_experiment(run_config, parameters): """Runs TensorFlow experiment. Creates the model, trains it, and evaluates it. Args: run_config: Configuration for experiment. parameters: Parameters passed to the job. """ estimator = model.create_regressor(config=run_config, parameters=parameters) train_spec = inputs.get_train_spec(parameters.training_path, parameters.batch_size, parameters.max_steps) eval_spec = inputs.get_eval_spec(parameters.validation_path, parameters.eval_batch_size) tf.estimator.train_and_evaluate(estimator, train_spec, eval_spec)
def run_experiment(run_config, parameters): """ This funtion runs the tensorflow model. """ estimator = model.create_regressor( config = run_config, parameters=parameters) train_spec = inputs.train_spec( parameters.training_path, parameters.batch_size, parameters.max_steps) eval_spec = inputs.eval_spec( parameters.validation_path, parameters.eval_batch_size) tf.estimator.train_and_evaluate( estimator, train_spec, eval_spec )
def main(): """ This main function executes the job. """ """ Sets parameters """ args_parser = argparse.ArgumentParser() args_parser.add_argument( "-p", "--predict", required=True, help="Is it a prediction?") args_parser.add_argument( '--predict_file_name', help='Path to the prediction file', required=False ) args_parser.add_argument( '--output_folder', help='GCS location to write checkpoints and export models.', default='gs://taxi_exps_4/' ) args_parser.add_argument( '--job_dir', help='Where the latest model sits.', default='gs://taxi_exps_4/2020-05-24_04-16-25' ) args_parser.add_argument( '--training_path', help='Location to training data.', default='gs://taxi_fare_pp1/data/csv/train.csv' ) args_parser.add_argument( '--validation_path', help='Location to validation data.', default='gs://taxi_fare_pp1/data/csv/eval.csv' ) args_parser.add_argument( '--pred_path', help='Location to prediction data.', default='gs://taxi_fare_pp1/data/csv/pred.csv' ) args_parser.add_argument( '--first_layer_size', help='First layer size.', default=256, type=int ) args_parser.add_argument( '--number_layers', help='Number of hidden layers.', default=3, type=int ) args_parser.add_argument( '--layer_reduction_fraction', help='Fraction to reduce layers in network.', default=0.5, type=float ) args_parser.add_argument( '--learning_rate', help='Learning rate.', default=0.000001, type=float ) args_parser.add_argument( '--batch_size', help='Training batch size.', default=64, type=int ) args_parser.add_argument( '--eval_batch_size', help='Evaluation batch size.', default=168, type=int ) args_parser.add_argument( '--max_steps', help='Maximum steps for training.', default=25000, type=int ) args_parser.add_argument( '--nbuckets', help='Number of buckets for bucketing latitude and longitude.', default=20, type=int ) parameters = args_parser.parse_args() tf.logging.set_verbosity(tf.logging.INFO) folder_timestamp = datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S') if parameters.predict=="True": print ('--------Predictions----------') data = pd.read_csv(parameters.pred_path) data = data.head() x_test = data[['hour', 'weekday', 'pickup_latitude', 'pickup_longitude', 'dropoff_latitude', 'dropoff_longitude', 'k2', 'is_luxury']] y = data['fare_dollars'] run_config = tf.estimator.RunConfig() model_dir=parameters.job_dir run_config = run_config.replace(model_dir=model_dir) estimator = model.create_regressor( config = run_config, parameters=parameters) pred_input_func = tf.estimator.inputs.pandas_input_fn(x = x_test, shuffle = False) predictions = estimator.predict(pred_input_func) for result in predictions: print (result) elif parameters.predict=="False": print ('--------Training---------') model_dir = os.path.join(parameters.output_folder, folder_timestamp, json.loads(os.environ.get('TF_CONFIG', '{}')).get('task', {}).get('trial', '')) run_config = tf.estimator.RunConfig( log_step_count_steps=1000, save_checkpoints_secs=120, keep_checkpoint_max=3, model_dir=model_dir ) run_experiment(run_config, parameters)
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.")