导出估算器模型以进行评估

时间:2020-07-04 09:30:48

标签: python tensorflow tensorflow2.0 tensorflow-estimator

我设法导出了模型

EXPORT = True
if EXPORT:
  serving_input_fn = tf.estimator.export.build_parsing_serving_input_receiver_fn(
    tf.feature_column.make_parse_example_spec(feature_columns))
  # Save the model
  estimator_path = classifier.export_saved_model("/content/drive/My Drive/Colab Notebooks/Research_project/", serving_input_fn)

并在以后用于预测

# Load the DNN models
ddos_classifier_1 = tf.saved_model.load(PATH_TO_MODEL_1)

def predict(df):
  """
  returns the predicted label given a dataframe of features
  """
  feature = {k: tf.train.Feature(float_list=tf.train.FloatList(value=[v])) for k, v in dict(df).items()}
  example = tf.train.Example(features=tf.train.Features(feature=feature))
  prediction = ddos_classifier_1.signatures["predict"](examples=tf.constant([example.SerializeToString()]))
  return prediction["classes"].numpy()[0][0].decode("utf-8")

我想做同样的事情,但是要评估模型。为此,我要添加documentation所提示的experimental_mode参数。

EXPORT = True
if EXPORT:
  serving_input_fn = tf.estimator.export.build_parsing_serving_input_receiver_fn(
    tf.feature_column.make_parse_example_spec(feature_columns))
  # Save the model
  estimator_path = classifier.export_saved_model(
  "/content/drive/My Drive/Colab Notebooks/Research_project/",
  serving_input_fn,
  experimental_mode=tf.estimator.ModeKeys.EVAL)

问题在于,现在serving_input_receiver_fn不再有效,我无法使其正常工作

ValueError: You must provide a labels Tensor. Given: None. Suggested troubleshooting steps: Check that your data contains your label feature. Check that your input_fn properly parses and returns labels.

应如何修改serving_input_fn以允许评估?

0 个答案:

没有答案