Python Forum
F-score, Precision, and Recall values
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
F-score, Precision, and Recall values
#1
from tensorflow.keras import backend as K
.
.
.
def computeMetrics(true, pred): # considering sigmoid activation, threshold = 0.5
pred = K.cast(K.greater(pred, 0.5), K.floatx())

gP = K.cast( K.sum(true), K.floatx()) + K.epsilon()
cP = K.sum(true * pred) + K.epsilon()
pP = K.sum(pred) + K.epsilon()

precision = cP / pP
recall = cP / gP
f1 = (2 * precision * recall) / (precision + recall)

return f1, precision, recall

metrics =computeMetrics(y_test, y_pred)

print('metrics -----> ',metrics)


output:
metrics -----> (<tf.Tensor 'truediv_2:0' shape=() dtype=float32>, <tf.Tensor 'truediv:0' shape=() dtype=float32>, <tf.Tensor 'truediv_1:0' shape=() dtype=float32>)



Anyone can help to get the values from the metrics ?
Reply
#2
You return three values and assign these to one variable, so that´s a tuple. Nothing you really want.

Python has an interesting function named dir() to inspect variables
Using this one can see that there is a method .numpy() to return the value of a tensor.

f1, precision, recall = computeMetrics([1.0, 2.0, 3.0, 4.0], [1.1, 1.9, 3.2, 4.1])

print('metrics: ', f1.numpy(), precision.numpy(), recall.numpy())
Output:
metrics: 1.4285715 2.5 1.0
For readability i would return the values instead of the tensors

    return f1.numpy(), precision.numpy(), recall.numpy()

f1, precision, recall = computeMetrics([1.0, 2.0, 3.0, 4.0], [1.1, 1.9, 3.2, 4.1])

print('metrics: ', f1, precision, recall)
Reply
#3
Thanks ThomasL
Now I am getting the following error:

print('metrics: ', f1.numpy(), precision.numpy(), recall.numpy())
AttributeError: 'numpy.float32' object has no attribute 'numpy'
>>>

I am not sure if this related to casting.
Reply
#4
Did you use .numpy() twice?

    return f1.numpy(), precision.numpy(), recall.numpy()
 
f1, precision, recall = computeMetrics([1.0, 2.0, 3.0, 4.0], [1.1, 1.9, 3.2, 4.1])
 
print('metrics: ', f1.numpy(), precision.numpy(), recall.numpy())
AttributeError: 'numpy.float32' object has no attribute 'numpy'
means that the variables are already a numerical object that you can use/print directly
so delete the .numpy() or don´t use it twice on the same object.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  precision & recall gracenz 1 961 Sep-21-2022, 02:14 AM
Last Post: jefsummers
  Time class with picosecond precision marcocod 5 3,582 Jul-03-2020, 12:12 AM
Last Post: Larz60+
  F-score and Recall values Greater Than 1 Hani 5 2,377 May-13-2020, 01:47 AM
Last Post: Hani
  Difference between R^2 and .score donnertrud 1 6,822 Jan-08-2020, 05:14 PM
Last Post: jefsummers
  problem to add precision, recall and f1-score edys 7 4,043 May-28-2019, 04:48 AM
Last Post: heiner55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020