Created
February 23, 2018 08:15
-
-
Save codertimo/f26a4005dc8dce3e96c4f7e1e69f17e9 to your computer and use it in GitHub Desktop.
Keras accuracy metric for sequence predicting with padding
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Description: | |
When we handle the word sequence predicting problem on Keras, we add paddings at the last of blank space. | |
But this makes wrong calculation at Keras default accuracy function. (It calculate the padding as correct too). | |
To solved that problem, I made new custom function for word sequence predicting problem. | |
It isn't calculate accuracy of the padding section, only calculate at actual word part | |
Junseong Kim, codertimo@gmail.com | |
""" | |
""" | |
New Accuracy Function for padding sequence | |
""" | |
import keras.backend as K | |
def word_acc(y_true, y_pred): | |
a = K.argmax(y_pred, axis=-1) | |
b = K.tf.reduce_max(y_true, axis=-1) | |
b = K.tf.cast(b, K.tf.int64) | |
total_index = K.tf.count_nonzero(a+b, 1) | |
wrong_index = K.tf.count_nonzero(a-b, 1) | |
total_index = K.tf.reduce_sum(total_index) | |
wrong_index = K.tf.reduce_sum(wrong_index) | |
correct_index = K.cast(total_index-wrong_index, K.floatx()) | |
total_index = K.cast(total_index, K.floatx()) | |
acc = K.tf.divide(correct_index, total_index) | |
return K.cast(acc,K.floatx()) | |
## Usage | |
model.compile(loss="sparse_categorical_crossentropy", optimizer="adam", metrics=[acc_cal]) | |
model.fit(x, y, epochs=10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment