def simple_dnn_regressor(export_path, eval_export_path): """Trains and exports a simple DNN regressor.""" feature_spec = tf.feature_column.make_parse_example_spec( feature_columns=util.dnn_columns(True)) regressor = tf.estimator.DNNRegressor( hidden_units=[4], feature_columns=util.dnn_columns(False), loss_reduction=tf.losses.Reduction.SUM) regressor = tf.estimator.add_metrics(regressor, util.regressor_extra_metrics) regressor.train(input_fn=util.make_regressor_input_fn(feature_spec), steps=3000) return util.export_model_and_eval_model( estimator=regressor, serving_input_receiver_fn=( tf.estimator.export.build_parsing_serving_input_receiver_fn( tf.feature_column.make_parse_example_spec( util.dnn_columns(False)))), eval_input_receiver_fn=export.build_parsing_eval_input_receiver_fn( tf.feature_column.make_parse_example_spec(util.dnn_columns(True)), label_key='label'), export_path=export_path, eval_export_path=eval_export_path)
def simple_linear_regressor(export_path, eval_export_path): """Trains and exports a simple linear regressor.""" feature_spec = tf.feature_column.make_parse_example_spec( util.linear_columns(False)) eval_feature_spec = tf.feature_column.make_parse_example_spec( util.linear_columns(True) + [ tf.feature_column.categorical_column_with_hash_bucket( 'slice_key', 100) ]) regressor = tf.estimator.LinearRegressor( feature_columns=util.linear_columns(), loss_reduction=tf.compat.v1.losses.Reduction.SUM) regressor = tf.estimator.add_metrics(regressor, util.regressor_extra_metrics) regressor.train(input_fn=util.make_regressor_input_fn( tf.feature_column.make_parse_example_spec(util.linear_columns(True))), steps=3000) return util.export_model_and_eval_model( estimator=regressor, serving_input_receiver_fn=( tf.estimator.export.build_parsing_serving_input_receiver_fn( feature_spec)), eval_input_receiver_fn=export.build_parsing_eval_input_receiver_fn( eval_feature_spec, label_key='label'), export_path=export_path, eval_export_path=eval_export_path)