全部参考以下文章:
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越大)