Run prediction from saved model in tensorflow 2.0

∥☆過路亽.° 提交于 2020-04-11 09:56:07

问题


I have a saved model (a directory with model.pd and variables) and wanted to run predictions on a pandas dataframe.

I've unsuccessfully tried a few ways to do this:

Attempt 1: Restore the estimator from the saved model

estimator = tf.estimator.LinearClassifier(
    feature_columns=create_feature_cols(),
    model_dir=path,
    warm_start_from=path)

Where path is the directory that has a model.pd and variables folder. I got an error

ValueError: Tensor linear/linear_model/dummy_feature1/weights is not found in 
gs://bucket/Trainer/output/2013/20191008T170504.583379-63adee0eaee0/serving_model_dir/export/1570554483/variables/variables 
checkpoint {'linear/linear_model/dummy_feature1/weights': [1, 1], 'linear/linear_model/dummy_feature2/weights': [1, 1]
}

Attempt 2: Run prediction directly from the saved model by running

imported = tf.saved_model.load(path)  # path is the directory that has a `model.pd` and variables folder
imported.signatures["predict"](example)

But has not successfully passed the argument - looks like the function is looking for a tf.example and I am not sure how to convert a dataframe to tf.example. My attempt to convert is below but got an error that df[f] is not a tensor:

for f in features:
    example.features.feature[f].float_list.value.extend(df[f])

I've seen solutions on stackoverflow but they are all tensorflow 1.14. Greatly appreciate if someone can help with tensorflow 2.0.

来源:https://stackoverflow.com/questions/58308258/run-prediction-from-saved-model-in-tensorflow-2-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!