Prediction using the TF contrib predictor in Tensor Flow

Viewed 709

While training a python model for census dataset using the code below: https://gist.github.com/gaganmalhotra/8c40e7650f27cf3f894bad092fbe01ab

I was successfully able to train/save and load the model back in python.

But for making predictions using the predictor it creates error like:

ValueError: Got unexpected keys in input_dict: set(['workclass', 'gender', 'marital_status', 'race', 'native_country', 'education', 'occupation'])

When the data passed to the model only contains these tensors, the code for that could be found in the gist i have shared above. Also find the snippet below-

from tensorflow.contrib import predictor
export_dir = "/Users/Documents/SampleTF_projects/tempppp/1510877466/"
predict_fn = predictor.from_saved_model(export_dir, signature_def_key=None)

K_CATEGORICAL_COLUMNS = ["gender", "native_country", "education", "occupation", "workclass", "marital_status", "race"]

def test_ip(df):
  categorical_cols = {k: tf.SparseTensor(
      indices=[[i, 0] for i in range(df[k].size)],
      values=df[k].values,
      dense_shape=[df[k].size, 1])
                      for k in K_CATEGORICAL_COLUMNS}
  return categorical_cols

# Get a sample from training dataframe
input_test = df_train[2:3]

# Passing through the input function will return the dict of corresponding tensors
dict_test = test_ip(input_test)

#Now making the prediction
predictions = predict_fn(dict_test)   #<<<<<<Error caused at this line
print(predictions['probabilities'])

Im not sure as only these features were used for training the data and now we are using same features for prediction.

@ash

0 Answers
Related