如何保存 Tensorflow LinearClassifier 模型并将其转换为 Tensorflow.js 兼容

How do I save a Tensorflow LinearClassifier model and convert it to Tensorflow.js compatible

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

转换保存的模型

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

下面是我的代码片段。


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

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

评论中的例子对我不起作用,但我按照说明做了类似的事情

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