Home > Mobile >  Tensorflow TypeError: Cannot convert 1e-12 to EagerTensor of dtype int32
Tensorflow TypeError: Cannot convert 1e-12 to EagerTensor of dtype int32

Time:10-14

I have a multiclass classification machine learning application for which I want to calculate the f1 score using tensorflow. The predicted and actual values are stored in pandas dataframes y_pred and y_act respectively. Both are populated with 1's and 0's. So I do something like this:

# convert dataframes to numpy
pred_numpy = numpy.asarray([y_pred], numpy.int32)
act_numpy = numpy.asarray([y_act], numpy.int32)

# compute multiclass f1
metric = tfa.metrics.F1Score(num_classes=num_classes, average="macro")
metric.update_state(act_numpy, pred_numpy)
print(metric.result().numpy())

However I get the following error

TypeError: Cannot convert 1e-12 to EagerTensor of dtype int32

There must be something with the type casting from pandas to tensorflow which is throwing the error. I have tried a series of mitigations to no avail.

I tried converting the numpy arrays to tensors like so: pred_tf = tf.convert_to_tensor(pred_numpy, numpy.int32)

I tried ensuring the pandas dataframe has no 1e-12 instances with: y_pred = y_pred.replace(1e-12, 0)

I tried converting to numpy without the numpy.int32 option.

However I still get the same error. Any tips for converting from pandas to tensors successfully without getting this error?

CodePudding user response:

This is the code sample provided on the tfa api docs:

metric = tfa.metrics.F1Score(num_classes=3, threshold=0.5)
y_true = np.array([[1, 1, 1],
                   [1, 0, 0],
                   [1, 1, 0]], np.int32)
y_pred = np.array([[0.2, 0.6, 0.7],
                   [0.2, 0.6, 0.6],
                   [0.6, 0.8, 0.0]], np.float32)
metric.update_state(y_true, y_pred)
result = metric.result()
result.numpy()

Note that the y_pred array is the original outputs of the model and is float, likely from a softmax activation. If you look at the code for the function, this is supported as it performs an argmax along the final dimension, or thresholds the probabilities. Therefore, if you cast these to an int, the probabilities will all be truncated to 0, although I suspect you're passing the already argmaxed values anyway.

I suspect this specific error comes from this line in the API:

y_pred = tf.logical_and(y_pred >= threshold, tf.abs(y_pred) > 1e-12)

as it can't compare the int32 you're providing with 1e-12 and tries to convert the second value.

  • Related