Skip to content

Instantly share code, notes, and snippets.

@TeraBytesMemory
Created July 16, 2020 13:19
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/4b4b776c87af083db66f6c7401d95cc9 to your computer and use it in GitHub Desktop.
Save TeraBytesMemory/4b4b776c87af083db66f6c7401d95cc9 to your computer and use it in GitHub Desktop.
def manifold_mixup_loss(alpha=1.):
def _manifold_mixup_loss(y_true, y_pred):
'''
y_true: (batch_size, onehot_label_size)
y_pred: (batch_size, output_unit_size)
'''
dist = tf.compat.v1.distributions.Beta(alpha, alpha)
beta = dist.sample()
mixuped_y_true = (1. - beta) * tf.reverse(y_true, [0]) + beta * y_true
mixuped_y_pred = (1. - beta) * tf.reverse(y_pred, [0]) + beta * y_pred
mixuped_y_pred = tf.nn.softmax(mixuped_y_pred)
return tf.keras.losses.categorical_crossentropy(mixuped_y_true, mixuped_y_pred)
return _manifold_mixup_loss
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment