简体   繁体   中英

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

I have recently started using tensorflow, and I have just a simple LinearClassifier model. And I want to save it and then convert the saved model using tfjs-converter

But the problem is that I can't seem to complete the first step of saving the model without errors.

Below is a snippet of my code.


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

Any help on getting unstuck is greatly appreciated!

The example in the comment didn't work for me, but I followed the instructions and settled upon something like this

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's example for making a basic linear estimator crashed when I tried to run it. You don't have to use Estimators to do machine learning — it seems like they're still in development currently.

If you're just getting started with Tensorflow, you might want to start with keras.models like here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM