全部參考以下文章:
3.Siamese network 孿生神經網絡的應用,僞孿生網絡
詳細的部分摘抄和記錄:
# tensorflow僞代碼
def contrastive_loss(self, y,d,batch_size):
tmp= y *tf.square(d)
#tmp= tf.mul(y,tf.square(d))
tmp2 = (1-y) *tf.square(tf.maximum((1 - d),0))
return tf.reduce_sum(tmp +tmp2)/batch_size/2
歐式距離表示兩個文本向量在空間之間的距離,如果距離很小說明樣本相似;反過來,兩個文本越相似,那麼它們之間的歐式距離越小;從上面圖中我們可以看出,x軸爲d(歐式距離),以紅色虛線爲例,它們描述了兩個相似文本loss與歐式距離之間的關係,因爲歐式距離越大與"兩個文本相似"這個事實事與願違,那麼我們就對它們懲罰越大(即loss越大)