Created
August 27, 2020 03:40
-
-
Save TeraBytesMemory/dc4984a0c1dabad446d0355f3a0a6dbe to your computer and use it in GitHub Desktop.
[WIP] l2 constraned parts in keras
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
import tensorflow as tf | |
K = tf.keras.backend | |
def l2_constrained_loss(norm): | |
def _l2_constrained_loss(y_true, y_pred): | |
y_pred = tf.nn.softmax(y_pred * norm) | |
return tf.keras.losses.categorical_crossentropy(y_true, y_pred) | |
return _l2_constrained_loss | |
def l2_constrained_accuracy(norm): | |
def _l2_constrained_accuracy(y_true, y_pred): | |
y_pred = tf.nn.softmax(y_pred * norm) | |
return tf.keras.metrics.categorical_accuracy(y_true, y_pred) | |
return _l2_constrained_accuracy | |
def l2_constrained_topk_accuracy(norm, k): | |
def _l2_constrained_topk_accuracy(y_true, y_pred): | |
y_pred = tf.nn.softmax(y_pred * norm) | |
return tf.keras.metrics.top_k_categorical_accuracy(y_true, y_pred, k=k) | |
return _l2_constrained_topk_accuracy |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment