【问题标题】:tensorflow estimator save model张量流估计器保存模型
【发布时间】:2020-05-07 11:48:52
【问题描述】:

我正在试验 character_rnn (https://github.com/tensorflow/tensorflow/blob/671baf080238025da9698ea980cd9504005f727c/tensorflow/examples/learn/text_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)

得到这个错误

TypeError:无法将类型对象转换为张量。内容:{'功能':}。考虑将元素转换为支持的类型。

请帮帮我。

【问题讨论】:

标签: tensorflow tensorflow-serving tensorflow-estimator


【解决方案1】:

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.com/jintolonappan/gbm-tf2-boostedtreesclassifier-export-to-serve 获得

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-09
    • 1970-01-01
    • 1970-01-01
    • 2016-10-26
    • 1970-01-01
    • 1970-01-01
    • 2018-07-10
    相关资源
    最近更新 更多