Run prediction from saved model in tensorflow 2.0

2020-07-11 05:09发布

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.

0条回答
登录 后发表回答