Is possible to use sklearn.neighbors.KNeighborsClassifier into a tensorflow Session i.e with Tensor?

I am trying to use the KNN classifier inside a Tensorflow session.

But I am getting the following error:

NotImplementedError: Cannot convert a symbolic Tensor (Const:0) to a numpy array. This error may indicate that you're trying to pass a Tensor to a NumPy call, which is not supported

Outside the session the code works just fine:

import tensorflow as tf
from sklearn.neighbors import KNeighborsClassifier
features= tf.constant([[1., 1.], [2., 2.],[2., 2.],[2., 2.],[2., 2.],[2., 2.]])
label= tf.constant([[1], [2], [2], [2], [2], [2]])
model = KNeighborsClassifier(n_neighbors=3)

# Train the model using the training sets
model.fit(features,label)

teste = tf.constant([[1., 1.], [2., 2.]])
#Predict Output
predicted= model.predict(teste) # 0:Overcast, 2:Mild
print(predicted)

But I need it inside the Session, here is a code with example of the error:

import tensorflow as tf
from sklearn.neighbors import KNeighborsClassifier
@tf.function
def add():
    model = KNeighborsClassifier(n_neighbors=3)


    features= tf.constant([[1., 1.], [2., 2.],[2., 2.],[2., 2.],[2., 2.],[2., 2.]])
    label= tf.constant([[1], [2], [2], [2], [2], [2]])
    # Train the model using the training sets
    model.fit(features,label)
    
    return model

add()

Versions :

tf.version.VERSION
'2.6.0'
sklearn.__version__
1.0.1

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

This code may help you solve your problem.

import tensorflow as tf
from sklearn.neighbors import KNeighborsClassifier
tf.config.run_functions_eagerly(True)
@tf.function
def add():
    
    model = KNeighborsClassifier(n_neighbors=3)


    features= tf.constant([[1., 1.], [2., 2.],[2., 2.],[2., 2.],[2., 2.],[2., 2.]])
    label= tf.constant([[1], [2], [2], [2], [2], [2]])
    features = features.numpy()
    label = label.numpy()
    # Train the model using the training sets
    model.fit(features,label)
    
    return model

add()

I have run the code in Google Colab. Downgraded NumPy to 1.19.5

Note:

  • .numpy() changes the tensor to numpy array.
  • Tensorflow 2 has a config option to run functions “eagerly” which will enable getting Tensor values via .numpy() method. 3rd line of the code [Without that line, the .numpy() will not work since @tf.function decorator prohibits the execution of functions like tensor.numpy() for performance reasons.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x