Skip to content

Instantly share code, notes, and snippets.

View pranjalg2308's full-sized avatar

Pranjal Gupta pranjalg2308

View GitHub Profile
# L2 Distance
def triplet_loss(alpha, emb_dim):
def loss(y_true, y_pred):
anc, pos, neg = y_pred[:,:emb_size], y_pred[:,emb_size:2*emb_size], y_pred[:,2*emb_size:]
distance1 = tf.sqrt(tf.reduce_sum(tf.pow(anc - pos, 2), 1, keepdims=True))
distance2 = tf.sqrt(tf.reduce_sum(tf.pow(anc - neg, 2), 1, keepdims=True))
return tf.reduce_mean(tf.maximum(distance1 - distance2 + alpha, 0.))
return loss
@pranjalg2308
pranjalg2308 / cosine_triplet_loss.py
Last active May 25, 2020 14:35
Implementation for Cosine similarity for triplet loss in keras
# Cosine Similarity
def triplet_loss(alpha, emb_dim):
def loss(y_true, y_pred):
anc, pos, neg = y_pred[:,:emb_size], y_pred[:,emb_size:2*emb_size], y_pred[:,2*emb_size:]
distance1 = tf.keras.losses.cosine_similarity(anc, pos)
distance2 = tf.keras.losses.cosine_similarity(anc, neg)
return tf.keras.backend.clip(distance1 - distance2 + alpha, 0., None)
return loss
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@color/white"/>
<item>
<bitmap
android:gravity="center"
android:src="@drawable/ic_icon"/>