ValueError while predicting from tf saved_model
ValueError while predicting from tf saved_model
I have saved a DNNestimator, and now I am trying to use that model to predict on some data.
Model training:
feature_columns = [tf.contrib.layers.real_valued_column("x", dimension=500)]
classifier = tf.estimator.DNNClassifier(
feature_columns=feature_columns,
hidden_units=[500],
optimizer=tf.train.AdamOptimizer(1e-4),
n_classes=18,
dropout=0,
model_dir=None)
train_input_fn = tf.estimator.inputs.numpy_input_fn(
x='x': train_vec.values,
y=train.code.astype(np.int32),
num_epochs=None,
batch_size=50,
shuffle=True)
classifier.train(input_fn=train_input_fn, steps=1000)
feature_spec = 'x':tf.FixedLenFeature(shape= [500],dtype=np.float32)
serving_fn = tf.estimator.export.build_parsing_serving_input_receiver_fn( feature_spec)
export_path = "path/to/export"
classifier.export_savedmodel(export_path,serving_fn)
I am trying to predict here:
a=np.expand_dims(test_vec.iloc[0].values,axis=0)
predict_fn = tf.contrib.predictor.from_saved_model(export_path_folder)
predictions = predict_fn("inputs":a)
Train_vec and test_vec are dataframes with 500 columns (features). I get the following error while predicting:
ValueError: Cannot feed value of shape (1, 500) for Tensor u'input_example_tensor:0', which has shape '(?,)'
Following is my saved_model_cli:
The given SavedModel SignatureDef contains the following input(s):
inputs['inputs'] tensor_info:
dtype: DT_STRING
shape: (-1)
name: input_example_tensor:0
The given SavedModel SignatureDef contains the following output(s):
outputs['classes'] tensor_info:
dtype: DT_STRING
shape: (-1, 18)
name: dnn/head/Tile:0
outputs['scores'] tensor_info:
dtype: DT_FLOAT
shape: (-1, 18)
name: dnn/head/predictions/probabilities:0
Method name is: tensorflow/serving/classify
Very new to tensorflow, any help or direction would be valuable.
Thanks!
1 Answer
1
Work around:
Could not solve the above error, but a DNNClassifier "warm start" worked.
classifier = tf.estimator.DNNClassifier(
feature_columns=feature_columns,
hidden_units=[500],
optimizer=tf.train.AdamOptimizer(1e-4),
n_classes=18,
dropout=0,
warm_start_from=export_path_folder)
And then use classifier.predict
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.