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
CodePudding user response:
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 liketensor.numpy()
for performance reasons.