0

I am trying to use a custom F1 score function in Keras but my target variable is an array and I don't want to one hot encode them, so I am using sparse_categorical_crossentropy as a loss.

from keras import backend as K
def f1(y_true, y_pred):
  print(type(y_true))
  return y_true

When I run the above code I am getting y_true and y_pred as <class 'tensorflow.python.framework.ops.Tensor'>. How can we convert y_true and y_pred to NumPy array, so that I can implement sklearn's F1 score function upon them?

Hamada
  • 1,836
  • 3
  • 13
  • 27

1 Answers1

0

Use (as also suggested by @learner in comments):

y_true = y_true.numpy()
y_pred = y_pred.numpy()
Ehsan
  • 12,072
  • 2
  • 20
  • 33