【问题标题】:How do I save a Tensorflow LinearClassifier model and convert it to Tensorflow.js compatible如何保存 Tensorflow LinearClassifier 模型并将其转换为兼容 Tensorflow.js
【发布时间】:2020-11-20 18:47:16
【问题描述】:

我最近开始使用 tensorflow,我只有一个简单的 LinearClassifier 模型。我想保存它,然后使用tfjs-converter转换保存的模型

但问题是我似乎无法无误地完成保存模型的第一步。

下面是我的代码的 sn-p。


linear_est = tf.estimator.LinearClassifier(feature_columns=feature_columns)

linear_est.train(train_input_fn)  # train

linear_est.save('saved_model/my_model') # This gives an error

非常感谢任何关于摆脱困境的帮助!

【问题讨论】:

标签: python tensorflow tensorflow2.0 tensorflow.js


【解决方案1】:

评论中的示例对我不起作用,但我按照说明进行操作并确定了类似的内容

import tensorflow as tf
import tensorflow.keras as keras
import tensorflowjs as tfjs

input_column = tf.feature_column.numeric_column("x")
a = tf.estimator.LinearClassifier(input_column)

serving_input_fn = tf.estimator.export.build_parsing_serving_input_receiver_fn(
  tf.feature_column.make_parse_example_spec([input_column]))

# Save Estimator as a tf model
a.export_saved_model("modelFromEstimator/", serving_input_fn)

# Import model as keras model
model = keras.models.load_model("modelFromEstimator/")

# Save as tfjs model
tfjs.converters.save_keras_model(model, "tfjsmodel")

Tensorflow 的用于制作基本线性估计器的示例在我尝试运行它时崩溃了。您不必使用 Estimator 来进行机器学习——它们目前似乎仍在开发中。

如果您刚刚开始使用 Tensorflow,您可能希望从 keras.models 开始,例如 here。

【讨论】:

  • 在创建serving_input_fn 时遇到问题。我有多个输入列,并将它们附加到一个列表中。当我尝试使用该列表创建 serving_input_fn 时,我收到一条错误消息“ValueError: All feature_columns must be FeatureColumn instances.”
  • 不要使用列表中的列名,而是先尝试使用tf.feature_column.numeric_column("feature") 将它们转换为 FeatureColumns,然后再将它们全部附加到列表中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-14
  • 1970-01-01
  • 2021-11-28
  • 1970-01-01
  • 2018-10-27
  • 2020-03-09
相关资源
最近更新 更多