【问题标题】:How to export saved model in the tutorial of "Recurrent Neural Networks for Drawing Classification"《Recurrent Neural Networks for Drawing Classification》教程中如何导出保存的模型
【发布时间】:2018-08-06 22:20:56
【问题描述】:

我正在浏览 tensorflow 的教程,但遇到了关于如何保存训练模型的问题。

在本教程中,定义并训练了一个循环神经网络来进行绘图分类。这是对应的代码:

estimator = tf.estimator.Estimator(
      model_fn=model_fn,
      model_dir=output_dir,
      config=config,
      params=model_params)
  # Train the model.
  tf.contrib.learn.Experiment(
      estimator=estimator,
      train_input_fn=get_input_fn(
          mode=tf.contrib.learn.ModeKeys.TRAIN,
          tfrecord_pattern=FLAGS.training_data,
          batch_size=FLAGS.batch_size),
      train_steps=FLAGS.steps,
      eval_input_fn=get_input_fn(
          mode=tf.contrib.learn.ModeKeys.EVAL,
          tfrecord_pattern=FLAGS.eval_data,
          batch_size=FLAGS.batch_size),
      min_eval_frequency=1000)

tutorials 没有给出显示如何导出和保存模型的代码。我怎样才能做到这一点?

【问题讨论】:

    标签: tensorflow tensorflow-serving


    【解决方案1】:

    本教程使用Estimator API。模型训练好后,可以调用export_savedmodel()方法保存:

    export_dir = './' # path to store the model
    estimator.export_savedmodel(export_dir, serving_input_fn)
    

    serving_input_fn 是训练期间input_fn 的服务时间等价物。这个函数应该返回一个ServingInputReceiver 对象。该对象的目标是接收服务请求,对其进行解析,并将其发送到模型进行推理。要进行解析,您需要提供一个 feature_spec 字典,告诉解析函数期望什么特性。来自文档:

    feature_spec = {'foo': tf.FixedLenFeature(...),
                    'bar': tf.VarLenFeature(...)}
    

    有关如何从头开始构建它的详细说明,请参阅TF documentation

    在大多数情况下,您可以使用build_parsing_serving_input_receiver_fnbuild_raw_serving_input_receiver_fn 实用程序函数来构建您的serving_input_fn。解析接收器需要如上所示的特征规范,原始接收器需要从字符串到张量的映射,并允许您将“原始”(未序列化)输入数据作为请求传递给模型。例如:

    serving_input_fn = tf.estimator.export.build_parsing_serving_input_receiver_fn(
    feature_spec,
    default_batch_size=None)
    

    【讨论】:

      猜你喜欢
      • 2020-02-10
      • 1970-01-01
      • 2016-05-15
      • 2017-10-20
      • 2020-05-10
      • 1970-01-01
      • 1970-01-01
      • 2023-01-21
      • 2017-03-17
      相关资源
      最近更新 更多