Search code examples
tensorflowtensorflow-estimator

How to save and load model of tf.estimator.BoostedTreesRegressor in the Tensorflow version='2.0.0'


I am New to the tf.estimator.BoostedTreesRegressor.. Here is the sample code that I have used to built the model.

n_batches = 20

est = tf.estimator.BoostedTreesRegressor(feature_columns,
                                           n_batches_per_layer=n_batches , learning_rate=0.001, n_trees=700,
                                            max_depth=13, 
model_dir = "model", tf.config.threading.set_intra_op_parallelism_threads(60))

est.train(train_input_fn, max_steps=10)

I want to save the model.. and load latter the model to predict the sales.

Can you please help me with how to do this in the TensorFlow version 2.. as I was not able to find...

Thanks


Solution

  • Your model should be saved in the model_dir path according to the official documentation. Please specify a real directory path to model_dir while instantiating BoostedTreesRegressor.

    Furthermore, you can save model using export_saved_model method.

    # Saving estimator model
    serving_input_fn = tf.estimator.export.build_parsing_serving_input_receiver_fn(
      tf.feature_column.make_parse_example_spec(feature_columns))
    export_path = estimator.export_saved_model("/dir/path/", serving_input_fn)
    

    For loading saved model you can use saved_model.load function as follows:

    #loading saved model
    imported = tf.saved_model.load(export_path)