Blair Burns
Blair Burns

Reputation: 51

tf.estimator wants label_data and batch_size for prediction Tensorflow

I have created a network using high level tf APIs such as tf.estimator.

Training and evaluating work fine and produce an output. However when predicting on new data, get_inputs() requires label_data and batch_size.

The error is: TypeError: get_inputs() missing 2 required positional arguments: 'label_data' and 'batch_size'

How can I resolve this so I can make a prediction?

Here is my code:

predictTest = [0.34, 0.65, 0.88]

predictTest is just a test and won't be my real prediction data.

get_inputs(), this is where the error is thrown.

def get_inputs(feature_data, label_data, batch_size, n_epochs=None, shuffle=True):
        dataset = tf.data.Dataset.from_tensor_slices(
            (feature_data, label_data))

    dataset = dataset.repeat(n_epochs)
    if shuffle:
        dataset = dataset.shuffle(len(feature_data))
    dataset = dataset.batch(batch_size)
    features, labels = dataset.make_one_shot_iterator().get_next()
    return features, labels

Prediction inputs:

def predict_input_fn():
    return get_inputs(
    predictTest,
    n_epochs=1,
    shuffle=False
    )

Predicting:

predict = estimator.predict(predict_input_fn)
print("Prediction: {}".format(list(predict)))

Upvotes: 1

Views: 960

Answers (2)

Blair Burns
Blair Burns

Reputation: 51

I worked out that I must create a new get_inputs() function for the prediction.

If I use the get_inputs() that train and evaluate use, it is expecting data it won't get.

get_inputs:

def get_inputs(feature_data, label_data, batch_size, n_epochs=None, shuffle=True):
    dataset = tf.data.Dataset.from_tensor_slices( #from_tensor_slices
        (feature_data, label_data))

    dataset = dataset.repeat(n_epochs)
    if shuffle:
        dataset = dataset.shuffle(len(feature_data))
    dataset = dataset.batch(batch_size)
    features, labels = dataset.make_one_shot_iterator().get_next()
    return features, labels

Make a new function called pred_get_inputs that doesn't require label_data or batch_size:

def get_pred_inputs(feature_data,n_epochs=None, shuffle=False):
    dataset = tf.data.Dataset.from_tensor_slices( #from_tensor_slices
        (feature_data))

    dataset = dataset.repeat(n_epochs)
    if shuffle:
        dataset = dataset.shuffle(len(feature_data))
    dataset = dataset.batch(1)
    features = dataset
    return features

Upvotes: 1

SarwarKhan
SarwarKhan

Reputation: 143

The testing of any model has two types. 1) you want accuracy, recall etc. you need to provide a label for the test data. if you don't provide label it will give you an error. 2) you just want to test your model without calculating the accuracy than you don't need a label but here the prediction will be different.

Upvotes: 0

Related Questions