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.