0

I'm trying to set up a KNN using the tf.estimator API, by creating a custom Estimator. A KNN has no concept of training, so I'd just like to define a Prediction step.

I tried to set it up as:

def input_fn():
    train_data = np.random.sample((100, 2))
    data = tf.data.Dataset.from_tensor_slices(train_data)
    return data

def model_fn(features, labels, mode, params, config):    
    k=10
    distance = tf.reduce_sum(tf.abs(tf.subtract(features, tf.expand_dims(features, 1))), axis=2)
    _, top_k_indices = tf.nn.top_k(tf.negative(distance), k=k)    
    return tf.estimator.EstimatorSpec(mode, predictions=top_k_indices)

run_config = tf.estimator.RunConfig(save_summary_steps=None,
                                    save_checkpoints_secs=None)
estimator = tf.estimator.Estimator(model_fn, config=run_config)

next(estimator.predict(input_fn))

But I get the error ValueError: Could not find trained model in model_dir: /tmp/tmpPGjIhN.

I would like the model to predict without having trained, by just returning the 10 nearest neighbours as they are defined in the model_fn. We do not need to train to initialize weights.

  • Not really sure what you are asking. How can it predict if it hasn't been trained on anything? Are you looking to load a model from file? – krflol May 06 '19 at 20:02
  • I edited the post - I would like to predict the 10 nearest neighbours of the feature set. There is no need to train to initialize/optimize weight parameters for a KNN model. I don't want to load any model – Baptiste Cumin May 06 '19 at 20:10
  • KNN is a supervised learning algorithm. It absolutely needs training. – krflol May 06 '19 at 20:12
  • I think it depends what you mean by train - we do need to store training data in memory but that wouldn't be stored in a model file in the traditional sense. See https://stackoverflow.com/questions/10814731/knn-training-testing-and-validation – Baptiste Cumin May 06 '19 at 20:17
  • As in my answer, a lot of machine learning libraries use the term "fit" and "train" interchangeably. You need to 'fit' your model. – krflol May 06 '19 at 20:19
  • Can you call 'estimator.export_saved_model()' before doing prediction? that might dump a model to your `model_dir`. see https://www.tensorflow.org/api_docs/python/tf/estimator/Estimator#export_saved_model. BTW, you don't need to have a `train` step but you need to export the model, which is done in 'train` periodically. – greeness May 06 '19 at 22:07

0 Answers0