Last active
August 7, 2019 13:35
-
-
Save mikeoconnor0308/521ae2eb1555edc6550014ce0500e6a2 to your computer and use it in GitHub Desktop.
Predict Input Function for DNNClassifier using Numeric Features
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# for use with https://github.com/marcsto/rl/blob/master/src/fast_predict2.py | |
# feature labels that were used to train the network. | |
FEATURES = ['Quat_x', 'Quat_y', 'Quat_z', 'Quat_w'] | |
def generator_evaluation_fn(generator): | |
""" Input function for numeric feature columns using the generator. """ | |
def _inner_input_fn(): | |
# set datatypes according to your data. | |
datatypes = tuple(len(FEATURES) * [tf.float32] | |
dataset = tf.data.Dataset().from_generator(generator, output_types=datatypes).batch(1) | |
iterator = dataset.make_one_shot_iterator() | |
features = iterator.get_next() | |
# create a feature dictionary. | |
feature_dict = dict(zip(FEATURES, features)) | |
return feature_dict | |
return _inner_input_fn | |
# Feature columns describe how to use the input. | |
my_feature_columns = [] | |
for key in FEATURES: | |
my_feature_columns.append(tf.feature_column.numeric_column(key=key)) | |
# Wrap the classifier in the FastPredict class to improve prediction speeds. | |
classifier = fastpredict.FastPredict(tf.estimator.DNNClassifier( | |
feature_columns=my_feature_columns, | |
generator_evaluation_fn | |
) | |
# some example data. | |
feature_values = [0, 0, 0, 1] | |
# crucial step: wrap the features in a list as a batch is expected, and wrap the whole thing as a tuple. | |
predict_x = tuple([[x] for x in feature_values]) | |
# call the predict, happy fast predicting! | |
predictions = classifier.predict(predict_x) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If I have a model behind an API and I want to keep it in memory and accept new data, is this the solution? This runs when I first make my call, but the generator doesn't seem to receive new data on subsequent calls. The only way I've been able to get around this is by resetting self.predictions within fastpredict, which then reloads the entire model. I guess I'm just not clear on the flow of the data.