如何将序列化数据提供给保存的模型?

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

我正在尝试将序列化数据提供给我保存的模型。我怀疑模型未正确导出,或者数据序列化未正确完成。任何提示或建议将不胜感激。

import tensorflow as tf
import numpy as np

train_input_fn = tf.estimator.inputs.numpy_input_fn(
        x={'x': np.array([[1,2,3,4,3], [1,3,4,2,4], [10,2,4,1.3,4], [1,3,5.2,9, 0.3]]).astype(np.float32)},
        y=np.array([0,0,1,1]).astype(np.float32),
        batch_size=2,
        shuffle=True
        )

eval_input_fn = tf.estimator.inputs.numpy_input_fn(
        x={'x': np.array([[1,2,3,1,4], [1,23,4,1,90]]).astype(np.float32)},
        y=np.array([0,1]).astype(np.float32),
        batch_size=2,
        num_epochs=1,
        shuffle=False
        )

def my_model(features, labels, mode, params):
    net = features['x']
    net = tf.layers.dense(net, 32, activation=tf.nn.relu)
    logits = tf.layers.dense(net, 1)
    if mode == tf.estimator.ModeKeys.PREDICT:
        return tf.estimator.EstimatorSpec(
                mode=mode,
                predictions=logits,
                export_outputs=
                    {'logits':tf.estimator.export.PredictOutput(logits)}
                )
    loss = tf.reduce_sum(tf.square(labels-logits))
    if mode == tf.estimator.ModeKeys.TRAIN:
        optimizer = tf.train.AdamOptimizer(learning_rate=0.02)
        train_op = optimizer.minimize(loss=loss,
                global_step=tf.train.get_global_step())
        return tf.estimator.EstimatorSpec(mode=mode, loss=loss, train_op=train_op)
    if mode == tf.estimator.ModeKeys.EVAL:
        return tf.estimator.EstimatorSpec(mode=mode, loss=loss)

train_spec = tf.estimator.TrainSpec(input_fn=train_input_fn, max_steps=5)
eval_spec = tf.estimator.EvalSpec(input_fn=eval_input_fn)

my_estimator = tf.estimator.Estimator(model_fn=my_model)
tf.estimator.train_and_evaluate(my_estimator, train_spec, eval_spec)

saved_model = my_estimator.export_savedmodel('foo', tf.estimator.export.build_parsing_serving_input_receiver_fn({'x': tf.FixedLenFeature([5],tf.float32)}))
predict_fn = tf.contrib.predictor.from_saved_model(saved_model,) 
features=tf.train.Features(feature={'x':tf.train.Feature(float_list=tf.train.FloatList(value=[1,2,3,4,1]))})
ex = tf.train.Example(features=features)
prediction = predict_fn({'examples':ex.SerializeToString()})

这是错误消息:

ValueError: Cannot feed value of shape () for Tensor 'input_example_tensor:0', which has shape '(?,)'
python tensorflow tensorflow-estimator
1个回答
1
投票

原来,必须提供tf.train.Examples.SerializeToString()列表。

在最后一行,prediction = predict_fn({'examples': ex.SerializeToString()})改为prediction = predict_fn({'examples': [ex.SerializeToString()]})

我想通过这个很好的教程:http://shzhangji.com/blog/2018/05/14/serve-tensorflow-estimator-with-savedmodel/

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