tensorflow估计器保存模型

问题描述 投票:0回答:1

我正在尝试用文字分类法对character_rnn(https:/github.comtensorflowtensorflowblob671baf080238025da9698ea980cd9504005f727ctensorflowexampleslearntext_classification_character_rnn.py。).

我如何为它写一个serving_input_fn?我想保存和恢复这个模型

扩展代码保存,但得到的错误,请帮助。

from tensorflow.contrib.learn.python.learn.utils import input_fn_utils
feature_spec = {"feature":tf.FixedLenFeature([100],tf.int64)}
serving_input_fn = input_fn_utils.build_parsing_serving_input_fn(feature_spec)

然后

classifier.export_savedmodel(export_dir_base='model', serving_input_receiver_fn=serving_input_fn)

并得到以下错误信息

类型错误。转换类型对象为Tensor失败。内容: {'feature': }. 考虑将元素转为支持的类型。

请帮助我。

tensorflow tensorflow-serving tensorflow-estimator
1个回答
0
投票

serving_fn 需要的功能是一个 tensor.

请看下面的示例。

def serving_fn():
    day_of_month      = tf.Variable([], dtype=tf.int64, name='DAY_OF_MONTH')
    day_of_week       = tf.Variable([], dtype=tf.int64, name='DAY_OF_WEEK')
    tail_num          = tf.Variable([], dtype=tf.string,name='TAIL_NUM')
    op_carrier_fl_num = tf.Variable([], dtype=tf.int64, name='OP_CARRIER_FL_NUM')
    origin_airport_id = tf.Variable([], dtype=tf.int64, name='ORIGIN_AIRPORT_ID')
    dest_airport_id   = tf.Variable([], dtype=tf.int64, name='DEST_AIRPORT_ID')
    dep_time_blk      = tf.Variable([], dtype=tf.string,name='DEP_TIME_BLK')

    reqd_inputs =  {'DAY_OF_MONTH':day_of_month,
                    'DAY_OF_WEEK':day_of_week,
                    'TAIL_NUM':tail_num,
                    'OP_CARRIER_FL_NUM':op_carrier_fl_num,
                    'ORIGIN_AIRPORT_ID':origin_airport_id,
                    'DEST_AIRPORT_ID':dest_airport_id,
                    'DEP_TIME_BLK':dep_time_blk}

    fn = tf.estimator.export.build_raw_serving_input_receiver_fn(reqd_inputs)
    return fn

根据你的特征和数据类型,你需要将它们转换为相应的张力。

如果有帮助的话,上面提到的示例可以作为Kaggle笔记本的一部分,网址是。https:/www.kaggle.comjintolonappangbm-tf2-boostedtreesclassifier-export-to-serve

© www.soinside.com 2019 - 2024. All rights reserved.