How to save a prepared model (Appraiser) and load it to test it with data in Tensorflow?

I have this snippet for my model

import pandas as pd import tensorflow as tf from tensorflow.contrib import learn from tensorflow.contrib.learn.python import SKCompat #Assume my dataset is using X['train'] as input and y['train'] as output regressor = SKCompat(learn.Estimator(model_fn=lstm_model(TIMESTEPS, RNN_LAYERS, DENSE_LAYERS),model_dir=LOG_DIR)) validation_monitor = learn.monitors.ValidationMonitor(X['val'], y['val'], every_n_steps=PRINT_STEPS, early_stopping_rounds=1000) regressor.fit(X['train'], y['train'], monitors=[validation_monitor], batch_size=BATCH_SIZE, steps=TRAINING_STEPS) #After training this model I want to save it in a folder, so I can use the trained model for implementing in my algorithm to predict the output #What is the correct format to use here to save my model in a folder called 'saved_model' regressor.export_savedmodel('/saved_model/') #I want to import it later in some other code, How can I import it? #is there any function like import model from file? 

How to keep this rating? I tried to find some examples for tf.contrib.learn.Estimator.export_savedmodel, didn’t I have success? Help evaluate.

+5
source share
1 answer

The export_savedmodel function requires the serve_input_receiver_fn argument, that is, a function with no arguments that defines the input to the model and predictor. Therefore, you should create your own serve_input_receiver_fn , where the input type of the model matches the input of the model in the training script, and the input type of the predictor matches the input of the predictor in the testing script. On the other hand, if you are creating a custom model, you must define export_outputs, defined by the tf.estimator.export.PredictOutput function, which is a dictionary that defines a name that must match the predictor output name in a testing script.

For instance:

SCRIPT TRAINING

 def serving_input_receiver_fn(): serialized_tf_example = tf.placeholder(dtype=tf.string, shape=[None], name='input_tensors') receiver_tensors = {"predictor_inputs": serialized_tf_example} feature_spec = {"words": tf.FixedLenFeature([25],tf.int64)} features = tf.parse_example(serialized_tf_example, feature_spec) return tf.estimator.export.ServingInputReceiver(features, receiver_tensors) def estimator_spec_for_softmax_classification(logits, labels, mode): predicted_classes = tf.argmax(logits, 1) if (mode == tf.estimator.ModeKeys.PREDICT): export_outputs = {'predict_output': tf.estimator.export.PredictOutput({"pred_output_classes": predicted_classes, 'probabilities': tf.nn.softmax(logits)})} return tf.estimator.EstimatorSpec(mode=mode, predictions={'class': predicted_classes, 'prob': tf.nn.softmax(logits)}, export_outputs=export_outputs) # IMPORTANT!!! onehot_labels = tf.one_hot(labels, 31, 1, 0) loss = tf.losses.softmax_cross_entropy(onehot_labels=onehot_labels, logits=logits) if (mode == tf.estimator.ModeKeys.TRAIN): optimizer = tf.train.AdamOptimizer(learning_rate=0.01) train_op = optimizer.minimize(loss, global_step=tf.train.get_global_step()) return tf.estimator.EstimatorSpec(mode, loss=loss, train_op=train_op) eval_metric_ops = {'accuracy': tf.metrics.accuracy(labels=labels, predictions=predicted_classes)} return tf.estimator.EstimatorSpec(mode=mode, loss=loss, eval_metric_ops=eval_metric_ops) def model_custom(features, labels, mode): bow_column = tf.feature_column.categorical_column_with_identity("words", num_buckets=1000) bow_embedding_column = tf.feature_column.embedding_column(bow_column, dimension=50) bow = tf.feature_column.input_layer(features, feature_columns=[bow_embedding_column]) logits = tf.layers.dense(bow, 31, activation=None) return estimator_spec_for_softmax_classification(logits=logits, labels=labels, mode=mode) def main(): # ... # preprocess-> features_train_set and labels_train_set # ... classifier = tf.estimator.Estimator(model_fn = model_custom) train_input_fn = tf.estimator.inputs.numpy_input_fn(x={"words": features_train_set}, y=labels_train_set, batch_size=batch_size_param, num_epochs=None, shuffle=True) classifier.train(input_fn=train_input_fn, steps=100) full_model_dir = classifier.export_savedmodel(export_dir_base="C:/models/directory_base", serving_input_receiver_fn=serving_input_receiver_fn) 

SCRIPT TEST

 def main(): # ... # preprocess-> features_test_set # ... with tf.Session() as sess: tf.saved_model.loader.load(sess, [tf.saved_model.tag_constants.SERVING], full_model_dir) predictor = tf.contrib.predictor.from_saved_model(full_model_dir) model_input = tf.train.Example(features=tf.train.Features( feature={"words": tf.train.Feature(int64_list=tf.train.Int64List(value=features_test_set)) })) model_input = model_input.SerializeToString() output_dict = predictor({"predictor_inputs":[model_input]}) y_predicted = output_dict["pred_output_classes"][0] 

(Code tested in Python 3.6.3, Tensorflow 1.4.0)

+1
source

Source: https://habr.com/ru/post/1266482/


All Articles