How to define a custom accuracy in Keras to ignore samples with a particular gold label?


I want to write in Keras a custom metric (I am using the tensorflow backend) equivalent to categorical_accuracy, but where the output for samples with a particular gold label (in my case 0, from y_true) have to be ignored. For example, if my outputs were:

Pred 1 - Gold 0

Pred 1 - Gold 1

The accuracy would be 1, since samples with the gold label 0 have to be ignored. That said, the function I wrote (and that is not giving the expected results) is:

def my_accuracy(y_true, y_pred): mask = K.any(K.not_equal(K.argmax(y_true, axis=-1), 0), axis=-1, keepdims=True) masked_y_true = y_true*K.cast(mask, K.dtype(y_true)) masked_y_pred = y_pred*K.cast(mask, K.dtype(y_pred)) return keras.metrics.categorical_accuracy(masked_y_true, masked_y_pred)`

Any help is appreciated, thanks!


You could try this approach:

def ignore_accuracy_of_class(class_to_ignore=0): def ignore_acc(y_true, y_pred): y_true_class = K.argmax(y_true, axis=-1) y_pred_class = K.argmax(y_pred, axis=-1) ignore_mask = K.cast(K.not_equal(y_pred_class, class_to_ignore), 'int32') matches = K.cast(K.equal(y_true_class, y_pred_class), 'int32') * ignore_mask accuracy = K.sum(matches) / K.maximum(K.sum(ignore_mask), 1) return accuracy return ignore_acc


  • Error: The database connection must be open to store files
  • Register subclass to an ABC class inside __init_subclass__ does not fully work
  • Keras, binary segmentation, add weight to loss function
  • Angular Service Worker, caching api calls for offline app
  • when I use wkwebview to load local html,I find it renders slower than uiwebview
  • How do I send an Object along with an attached File in a Multipart superagent request?
  • Microsoft.Office.Interop.Outlook 2010 Reference in VS 2012 Using .Net 2.0
  • Extract and save attachment from email (via SES) into AWS S3
  • How to stop jobs scheduled using spring task
  • I receive “incompatible character encodings: CP850 and UTF-8” when displaying the £ symbol on my ram
  • Installing Haskell Gloss
  • How to turn of electric-indent-mode for specific Major mode?
  • Using prepared statement in Groovy
  • Alexa Dialog Model Step and dialogState is never in COMPLETED
  • Update Stripe Credit Card with Coffeescript
  • Java Rest call with different user certs
  • Convert certificate to byte array
  • Setting headers for CakePHP Controller unit tests
  • WooCommerce Free Shipping - Remove raw or change the text name on checkout and email
  • Why context.Wait in StartAsync didn't stop the dialog
  • Missing App Store icon codename one build
  • Erlang needs to connect to https server?
  • NextJS auth with an external server
  • Webdriver 3.14 IE11: session lost when click link/button that opens a window/popup
  • Checking for valid enum types from protobufs
  • JavaMail connection problems [duplicate]
  • Add checkbox dynamically using angular 2
  • how to run a different select statement based on condition in Hive SQL
  • WPF custom control and direct content support
  • Create/delete users from text file using Bash script
  • Time Complexity of Fibonacci Algorithm [duplicate]
  • How to check if object is null in Java?