服务输入fn的训练过的张量流模型

时间:2019-12-12 02:27:03

标签: python tensorflow export tensorflow-serving tensorflow-estimator

我想导出模型以进行推断。我已经做了,但是问题是我在导出它时做错了。当它真正开始预测事物时,它将起作用,但是结果却完全不准确。

transform(df, Z = ifelse(grepl("^[A-Z]+$", x), x, y))
#    x   y   Z
#1 ABC jkl ABC
#2 def MNO MNO
#3 GHI pqr GHI

完成模型训练后,我想导出模型以进行推断。但是,下面的代码有问题,因为它根本无法进行推理,而为预测提供了错误的值。我还从某个地方复制了此代码,所以我不知道如何为fn和相关工作提供服务,并且文档非常混乱。如果我向其中添加“ Tensorflow_sucks”,即使我的训练功能中从未使用过“ Tensorflow_sucks”,它也不会返回错误?

CATEGORICAL_COLUMNS = ['Path']
NUMERIC_COLUMNS = ['IntersectionId','EntryHeading','ExitHeading','City','Latitude','Longitude','Hour','Month','Weekend']

def one_hot_cat_column(feature_name, vocab):
  return tf.feature_column.indicator_column(
      tf.feature_column.categorical_column_with_vocabulary_list(feature_name,
                                                 vocab))
feature_columns = []
for feature_name in CATEGORICAL_COLUMNS:
  # Need to one-hot encode categorical features.
  vocabulary = train_df[feature_name].unique()
  feature_columns.append(one_hot_cat_column(feature_name, vocabulary))

for feature_name in NUMERIC_COLUMNS:
  feature_columns.append(tf.feature_column.numeric_column(feature_name,
                                           dtype=tf.float32))

def make_input_fn(X, y, n_epochs=None, shuffle=True):
  def input_fn():
    dataset = tf.data.Dataset.from_tensor_slices((dict(X), y))
    if shuffle:
      dataset = dataset.shuffle(NUM_EXAMPLES)
    # For training, cycle thru dataset as many times as need (n_epochs=None).
    dataset = dataset.repeat(n_epochs)
    # In memory training doesn't use batching.
    dataset = dataset.batch(NUM_EXAMPLES)
    return dataset
  return input_fn


train_input_fn = make_input_fn(train_df[CATEGORICAL_COLUMNS+NUMERIC_COLUMNS], current_train)


n_batches = 1
est = tf.estimator.BoostedTreesRegressor(feature_columns,
                                            n_batches_per_layer=n_batches,max_depth=20)
est.train(train_input_fn)


如何正确地将模型导出为SavedModel进行推断?

0 个答案:

没有答案