def top_K_loss_margin(self,sentence,image,K=50,margin=0.2):
sim_matrix = tf.matmul(sentence, image, transpose_b=True)
s_square = tf.reduce_sum(tf.square(sentence), axis=1)
im_square = tf.reduce_sum(tf.square(image), axis=1)
d = 1-tf.sigmoid(sim_matrix)
positive = tf.stack([tf.matrix_diag_part(d)] * K, axis=1)
length = tf.shape(d)[-1]
dd = tf.matrix_set_diag(d, 8 * tf.ones([length]))
flag =8-7*tf.sign(tf.nn.relu(self.sen_margin-self.sen_similarity))
sen_loss_K ,_ = tf.nn.top_k(-1.0 * dd *flag, K, sorted=False) # note: this is negative value
im_loss_K,_ = tf.nn.top_k(-tf.transpose(1.0 * dd*flag), K, sorted=False) # note: this is negative value
sentence_center_loss = -tf.log(1-positive+1e-12)-tf.log(-sen_loss_K+1e-12)
image_center_loss = -tf.log(1-positive+1e-12)-tf.log(-im_loss_K+1e-12)
self.d_neg = tf.reduce_mean((sen_loss_K + im_loss_K)/-2.0)
self.d_pos =tf.reduce_mean(positive)
self.endpoint['debug/im_loss_topK'] = -1.0 * im_loss_K
self.endpoint['debug/sen_loss_topK'] = -1.0 * sen_loss_K
self.endpoint['debug/d_Matrix'] = d
self.endpoint['debug/positive'] = positive
self.endpoint['debug/s_center_loss'] = sentence_center_loss
self.endpoint['debug/i_center_loss'] = image_center_loss
self.endpoint['debug/S'] = sim_matrix
self.endpoint['debug/sentence_square'] = s_square
self.endpoint['debug/image_square'] = im_square
return tf.reduce_sum(sentence_center_loss), tf.reduce_sum(image_center_loss)
Bidirectionnet_GMM_sigmod9000feat.py 文件源码
python
阅读 32
收藏 0
点赞 0
评论 0
评论列表
文章目录