4

Custom Metrics for Keras and TensorFlow

 1 year ago
source link: https://gist.github.com/arnaldog12/5f2728f229a8bd3b4673b72786913252
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
Custom Metrics for Keras and TensorFlow

Hi, thanks for sharing. I'm new with DL, i would ask how to calculate the y_true?

Author

arnaldog12 commented Nov 25, 2020

the y_true is the ground-truth of your dataset.

the y_true is the ground-truth of your dataset.

thanks for your reply, i faced an error and solved by changing the datatype of the label dataset. My question is after called the precision when i tried to "print" the value it showed nothing. I working in an ensemble learning model . sorry for my trivial question.
regards

Hi Arnal,
I realized that my balanced accuracy values calculated within tensorflow are not the same ones calculated by the regular confusion matrix from the caret package (it's in R, but the code is very similar). Have you had any experience with that in python? I tried both codes for balanced accuracy:

Tensorflow

balanced_acc <- custom_metric("balanced_acc",function(y_true,y_pred){
  y_pred_pos = k_round(k_clip(y_pred, 0, 1))
  y_pred_neg = 1 - y_pred_pos
  
  y_pos = k_round(k_clip(y_true, 0, 1))
  y_neg = 1 - y_pos
  
  tp = k_sum(y_pos * y_pred_pos)
  tn = k_sum(y_neg * y_pred_neg)
  
  fp = k_sum(y_neg * y_pred_pos)
  fn = k_sum(y_pos * y_pred_neg)
  
  sensi = (tp/(tp + fn + k_epsilon()))
  specifi = (tn/(tn + fp + k_epsilon()))
  
  return((sensi + specifi )/ 2 )
  
})


##or 

balanced_acc <- custom_metric("balanced_acc",function(y_true,y_pred){

  tp = k_sum(k_round(k_clip(y_true * y_pred, 0, 1)))
  tn =  k_sum(k_round(k_clip((1 - y_true) * (1 - y_pred), 0, 1)))
  
  fp = k_sum(k_round(k_clip((1 - y_true) * y_pred, 0, 1)))
  fn = k_sum(k_round(k_clip(y_true * (1 - y_pred), 0, 1)))
  
  sensi = (tp/(tp + fn + k_epsilon()))
  specifi = (tn/(tn + fp + k_epsilon()))
  
  return((sensi + specifi )/ 2 )
  
})

caret

  conf.matrix <-  caret::confusionMatrix(
    factor(pred_model, levels = 0:1),
    factor(y_test, levels = 0:1),
    positive = "1"
  )
  
  BalancedAccuracy <- conf.matrix$byClass[11]

Author

arnaldog12 commented Dec 1, 2021

Hi, @DohaNaga
I would check the shape of each matrix and if they are disposed in the same way (i.e., if rows are samples and cols are features).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK