def get_simple_dnn_classifier_and_metadata(n_classes=2, label_vocabulary=None): """Returns metadata for creating simple DNN classifier.""" if label_vocabulary: feature_spec = tf.feature_column.make_parse_example_spec( feature_columns=util.dnn_columns(False, n_classes=n_classes)) feature_spec['label'] = tf.io.FixedLenFeature(shape=[1], dtype=tf.string) else: feature_spec = tf.feature_column.make_parse_example_spec( feature_columns=util.dnn_columns(True, n_classes=n_classes)) classifier = tf_estimator.DNNClassifier( hidden_units=[4], feature_columns=util.dnn_columns(False), n_classes=n_classes, label_vocabulary=label_vocabulary, loss_reduction=tf.losses.Reduction.SUM) classifier = tf_estimator.add_metrics(classifier, util.classifier_extra_metrics) return { 'estimator': classifier, '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': build_parsing_eval_input_receiver_fn(feature_spec, label_key='label'), 'train_input_fn': util.make_classifier_input_fn(feature_spec, n_classes, label_vocabulary=label_vocabulary), }
def simple_linear_classifier(export_path, eval_export_path): """Trains and exports a simple linear classifier.""" 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) ]) classifier = tf.estimator.LinearClassifier( feature_columns=util.linear_columns(), loss_reduction=tf.compat.v1.losses.Reduction.SUM) classifier = tf.estimator.add_metrics(classifier, util.classifier_extra_metrics) classifier.train(input_fn=util.make_classifier_input_fn( tf.feature_column.make_parse_example_spec(util.linear_columns(True))), steps=1000) return util.export_model_and_eval_model( estimator=classifier, 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)
def get_simple_dnn_classifier_and_metadata(n_classes=2): """Returns metadata for creating simple DNN classifier.""" feature_spec = tf.feature_column.make_parse_example_spec( feature_columns=util.dnn_columns(True, n_classes=n_classes)) classifier = tf.estimator.DNNClassifier( hidden_units=[4], feature_columns=util.dnn_columns(False), n_classes=n_classes, loss_reduction=tf.compat.v1.losses.Reduction.SUM) classifier = tf.estimator.add_metrics(classifier, util.classifier_extra_metrics) return { 'estimator': classifier, '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': build_parsing_eval_input_receiver_fn( tf.feature_column.make_parse_example_spec( util.dnn_columns(True, n_classes=n_classes)), label_key='label'), 'train_input_fn': util.make_classifier_input_fn(feature_spec, n_classes), }
def simple_dnn_classifier(export_path, eval_export_path, n_classes=2): """Trains and exports a simple DNN classifier.""" feature_spec = tf.feature_column.make_parse_example_spec( feature_columns=util.dnn_columns(True, n_classes=n_classes)) classifier = tf.estimator.DNNClassifier( hidden_units=[4], feature_columns=util.dnn_columns(False), n_classes=n_classes) classifier = tf.contrib.estimator.add_metrics( classifier, util.classifier_extra_metrics) classifier.train(input_fn=util.make_classifier_input_fn( feature_spec, n_classes), steps=1000) return util.export_model_and_eval_model( estimator=classifier, 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=build_parsing_eval_input_receiver_fn( tf.feature_column.make_parse_example_spec( util.dnn_columns(True, n_classes=n_classes)), label_key='label'), export_path=export_path, eval_export_path=eval_export_path)
def get_simple_dnn_classifier_and_metadata(n_classes=2): feature_spec = tf.feature_column.make_parse_example_spec( feature_columns=util.dnn_columns(True, n_classes=n_classes)) classifier = tf.estimator.DNNClassifier( hidden_units=[4], feature_columns=util.dnn_columns(False), n_classes=n_classes) classifier = tf.contrib.estimator.add_metrics( classifier, util.classifier_extra_metrics) return { 'estimator': classifier, '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': build_parsing_eval_input_receiver_fn( tf.feature_column.make_parse_example_spec( util.dnn_columns(True, n_classes=n_classes)), label_key='label'), 'train_input_fn': util.make_classifier_input_fn(feature_spec, n_classes), }