【问题标题】:How to save a trained model (Estimator) and Load it back to test it with data in Tensorflow?如何保存经过训练的模型(估计器)并将其加载回来以使用 Tensorflow 中的数据对其进行测试?
【发布时间】:2017-04-07 21:25:30
【问题描述】:

我的模型有这个 sn-p

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?

如何保存此估算器?我尝试为 tf.contrib.learn.Estimator.export_savedmodel 找到一些示例,但没有成功?帮助表示赞赏。

【问题讨论】:

    标签: python machine-learning tensorflow save pre-trained-model


    【解决方案1】:

    export_savedmodel 函数需要参数 serving_input_receiver_fn,这是一个没有参数的函数,它定义了模型和预测器的输入。因此,您必须创建自己的 serving_input_receiver_fn,其中模型输入类型与训练脚本中的模型输入匹配,预测器输入类型与测试脚本中的预测器输入匹配。 另一方面,如果您创建自定义模型,则必须定义 export_outputs,由函数 tf.estimator.export.PredictOutput 定义,该输入是定义必须匹配的名称的字典带有测试脚本中预测器输出的名称。

    例如:

    培训脚本

    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)
    

    测试脚本

    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]
    

    (在 Python 3.6.3、Tensorflow 1.4.0 中测试的代码)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-18
      • 1970-01-01
      • 2019-02-19
      • 1970-01-01
      • 2017-07-01
      • 1970-01-01
      相关资源
      最近更新 更多