Warlax56
Warlax56

Reputation: 1202

Simple way to evaluate input with a TensorFlow model?

Here I have a boosted decision tree being trained with generated data, and saved as est:

from sklearn.datasets import make_blobs
import pandas as pd
import tensorflow as tf

#creates an input function for a tf model
def make_input_fn(X, Y, n_epochs=None, shuffle=True, verbose=False):
    batch_len = len(Y)
    def input_fn():
        dataset = tf.data.Dataset.from_tensor_slices((dict(X), Y))
        if shuffle:
            dataset = dataset.shuffle(batch_len)
        # For training, cycle thru dataset as many times as need (n_epochs=None).
        dataset = dataset.repeat(n_epochs)
        #dividing data into batches
        dataset = dataset.batch(batch_len)
        return dataset
    return input_fn

#making data
trainX, trainY = make_blobs(n_samples=10, centers=2, n_features=3, random_state=0)

#xVals
trainX = pd.DataFrame(trainX)
trainX.columns = ['feature{}'.format(num) for num in trainX.columns]

#yVals
trainY = pd.DataFrame(trainY)
trainY.columns = ['flag']

# Defining input function
train_input_fn = make_input_fn(trainX, trainY)

#defining tf feature columns
feature_columns=[]
for feature_name in list(trainX.columns):
    feature_columns.append(tf.feature_column.numeric_column(feature_name,dtype=tf.float32))
    
#creating the estimator
n_batches = 1
est = tf.estimator.BoostedTreesClassifier(feature_columns, n_batches_per_layer=n_batches)

est.train(train_input_fn, max_steps=10)

I would like to use the model to make a prediction based on a row of training data for testing purposes; something like this: res = est.predict(trainX.loc[0]), however, I'm having a tough time figuring out how to go about it.

Upvotes: 0

Views: 42

Answers (1)

Aniket Bote
Aniket Bote

Reputation: 3574

You have to create an input function just like you did for training.
Code:

def my_input_fn(features, batch_size=256):
    """An input function for prediction."""
    # Convert the inputs to a Dataset without labels.
    return tf.data.Dataset.from_tensor_slices(dict(features)).batch(batch_size)

testX = pd.DataFrame(trainX.loc[0]).T

predictions = est.predict(
    input_fn=lambda: my_input_fn(testX))

The predictions will give you a generator object. You have to iterate over it to get predictions

for pred_dict in predictions:
    class_id = pred_dict['class_ids'][0]
    probability = pred_dict['probabilities'][class_id]
    print(class_id, probability)

The class_id is predicted ID.

Note that pred_dict contains other information as well.

Here is the information contained in pred_dict:

{'all_class_ids': array([0, 1]),
 'all_classes': array([b'0', b'1'], dtype=object),
 'class_ids': array([0], dtype=int64),
 'classes': array([b'0'], dtype=object),
 'logistic': array([0.17926924], dtype=float32),
 'logits': array([-1.5213063], dtype=float32),
 'probabilities': array([0.82073075, 0.17926925], dtype=float32)}

Upvotes: 1

Related Questions