Skip to content

Instantly share code, notes, and snippets.

@TeraBytesMemory
Created August 27, 2020 03:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TeraBytesMemory/dc4984a0c1dabad446d0355f3a0a6dbe to your computer and use it in GitHub Desktop.
Save TeraBytesMemory/dc4984a0c1dabad446d0355f3a0a6dbe to your computer and use it in GitHub Desktop.
[WIP] l2 constraned parts in keras
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