code: #!/usr/bin/python3
  
  import keras.backend as KB
  import tensorflow as tf
  
  
  def sparse_accuracy_ignoring_last_label(y_true, y_pred):
      nb_classes = KB.int_shape(y_pred)[-1]
      y_pred = KB.reshape(y_pred, (-1, nb_classes))
  
      y_true = KB.one_hot(tf.to_int32(KB.flatten(y_true)),
                          nb_classes + 1)
      unpacked = tf.unstack(y_true, axis=-1)
      legal_labels = ~tf.cast(unpacked[-1], tf.bool)
      y_true = tf.stack(unpacked[:-1], axis=-1)
  
      return KB.sum(tf.to_float(legal_labels & KB.equal(KB.argmax(y_true, axis=-1), KB.argmax(y_pred, axis=-1)))) / KB.sum(tf.to_float(legal_labels))
  
apis: tensorflow.stack tensorflow.cast tensorflow.to_float tensorflow.unstack
api_extract: [(9, 'keras.backend.reshape', 'KB.reshape', (['y_pred', '(-1, nb_classes)'], {}), True, 'import keras.backend as KB\n'), (13, 'tensorflow.unstack', 'tf.unstack', (['y_true'], {'axis': '(-1)'}), True, 'import tensorflow as tf\n'), (15, 'tensorflow.stack', 'tf.stack', (['unpacked[:-1]'], {'axis': '(-1)'}), True, 'import tensorflow as tf\n'), (8, 'keras.backend.int_shape', 'KB.int_shape', (['y_pred'], {}), True, 'import keras.backend as KB\n'), (14, 'tensorflow.cast', 'tf.cast', (['unpacked[-1]', 'tf.bool'], {}), True, 'import tensorflow as tf\n'), (11, 'keras.backend.flatten', 'KB.flatten', (['y_true'], {}), True, 'import keras.backend as KB\n'), (17, 'tensorflow.to_float', 'tf.to_float', (['legal_labels'], {}), True, 'import tensorflow as tf\n'), (17, 'keras.backend.argmax', 'KB.argmax', (['y_true'], {'axis': '(-1)'}), True, 'import keras.backend as KB\n'), (17, 'keras.backend.argmax', 'KB.argmax', (['y_pred'], {'axis': '(-1)'}), True, 'import keras.backend as KB\n')]