MMR公式:
The core idea behind MMR in this context is to select keyword that is relevant to underlying document as well as every progressive keyword that you select has minimal similarity with the already selected set of keywords. Below equation shows the mathematical represenation of the same —
欧式距离 euclidean_distance
欧式距离归一化
向量的模 ,1-范数 2-范数
向量的模
1-范数
2-范数
Sigmoid: 0 ~ 1
Tanh: -1 ~ 1
def mse_loss(y1,y2):
return tf.reduce_mean(tf.squared_difference(y1, y2))
def arcface_loss(embedding, weights, labels, s=64., m=0.5, m3=0.1):
# def arcface_loss(x,y)
'''
:param embedding: the input embedding vectors
:param labels: the input labels, the shape should be eg: (batch_size, 1)
:param s: scalar value default is 64
:param out_num: output class num
:param m: the margin value, default is 0.5
:return: the final cacualted output, this output is send into the tf.nn.softmax directly
'''
cos_m = math.cos(m)
sin_m = math.sin(m)
mm = sin_m * m # issue 1
threshold = math.cos(math.pi - m)
# with tf.variable_scope('arcface_loss'):
# inputs and weights norm
embedding_norm = tf.norm(embedding, axis=1, keepdims=True)
embedding = tf.div(embedding, embedding_norm, name='norm_embedding')
# weights = tf.get_variable(name='embedding_weights', shape=(embedding.get_shape().as_list()[-1], out_num),
# initializer=w_init, dtype=tf.float32)
# cos(theta+m)
# labels = tf.Print(labels, [tf.shape(labels)], 'embedding shape')
cos_t = tf.matmul(embedding, weights, name='cos_t')
cos_t2 = tf.square(cos_t, name='cos_2')
sin_t2 = tf.subtract(1., cos_t2, name='sin_2')
sin_t = tf.sqrt(sin_t2, name='sin_t')
cos_mt = s * tf.subtract(tf.multiply(cos_t, cos_m),
tf.multiply(sin_t, sin_m), name='cos_mt')
# this condition controls the theta+m should in range [0, pi]
# 0<=theta+m<=pi
# -m<=theta<=pi-m
cond_v = cos_t - threshold
# swift_act(x):
# cond = tf.cast(swish_act(cond_v), dtype=tf.bool)
cond = tf.cast(tf.nn.relu(cond_v, name='if_else'), dtype=tf.bool)
keep_val = s * (cos_t - mm)
cos_mt_temp = tf.where(cond, cos_mt, keep_val) ##?? 为何要这一步
# labels = tf.one_hot(labels, depth=out_num, name='one_hot_mask')
# mask = tf.squeeze(mask, 1)
# labels = tf.Print(labels, [tf.shape(labels)], 'label shape')
print('cost_t ={}'.format(cos_t.shape))
print('embeddings = {}'.format(embedding.shape))
print('labels = {}'.format(labels.shape))
inv_mask = tf.subtract(1., tf.cast(labels, tf.float32), name='inverse_mask')
print('inv_mask = {}'.format(inv_mask.shape))
s_cos_t = tf.multiply(s, cos_t, name='scalar_cos_t')
output = tf.add(tf.multiply(s_cos_t, inv_mask), tf.multiply(
cos_mt_temp, labels), name='arcface_loss_output')
# output = tf.subtract(tf.add(tf.multiply(s_cos_t, inv_mask), tf.multiply(
# cos_mt_temp, labels), name='arcface_loss_output'), labels*m3)
return output
def arcface_as_predict(embedding, weights, s=64., m=0.5, m3=0.1):
# def arcface_loss(x,y)
'''
:param embedding: the input embedding vectors
:param labels: the input labels, the shape should be eg: (batch_size, 1)
:param s: scalar value default is 64
:param out_num: output class num
:param m: the margin value, default is 0.5
:return: the final cacualted output, this output is send into the tf.nn.softmax directly
'''
cos_m = math.cos(m)
sin_m = math.sin(m)
mm = sin_m * m # issue 1
threshold = math.cos(math.pi - m)
# with tf.variable_scope('arcface_loss'):
# inputs and weights norm
embedding_norm = tf.norm(embedding, axis=1, keepdims=True)
embedding = tf.div(embedding, embedding_norm, name='norm_embedding')
# weights = tf.get_variable(name='embedding_weights', shape=(embedding.get_shape().as_list()[-1], out_num),
# initializer=w_init, dtype=tf.float32)
# cos(theta+m)
# labels = tf.Print(labels, [tf.shape(labels)], 'embedding shape')
cos_t = tf.matmul(embedding, weights, name='cos_t')
cos_t2 = tf.square(cos_t, name='cos_2')
sin_t2 = tf.subtract(1., cos_t2, name='sin_2')
sin_t = tf.sqrt(sin_t2, name='sin_t')
cos_mt = s * tf.subtract(tf.multiply(cos_t, cos_m),
tf.multiply(sin_t, sin_m), name='cos_mt')
# this condition controls the theta+m should in range [0, pi]
# 0<=theta+m<=pi
# -m<=theta<=pi-m
cond_v = cos_t - threshold
# swift_act(x):
# cond = tf.cast(swish_act(cond_v), dtype=tf.bool)
cond = tf.cast(tf.nn.relu(cond_v, name='if_else'), dtype=tf.bool)
keep_val = s * (cos_t - mm)
cos_mt_temp = tf.where(cond, cos_mt, keep_val)
# labels = tf.one_hot(labels, depth=out_num, name='one_hot_mask')
# mask = tf.squeeze(mask, 1)
# labels = tf.Print(labels, [tf.shape(labels)], 'label shape')
# print('cost_t ={}'.format(cos_t.shape))
# print('embeddings = {}'.format(embedding.shape))
# print('labels = {}'.format(labels.shape))
# inv_mask = tf.subtract(1., tf.cast(labels,tf.float32), name='inverse_mask')
# print('inv_mask = {}'.format(inv_mask.shape))
s_cos_t = tf.multiply(s, cos_t, name='scalar_cos_t')
# output = tf.add(tf.multiply(s_cos_t, inv_mask), tf.multiply(
# cos_mt_temp, labels), name='arcface_loss_output')
# output = tf.subtract(tf.add(tf.multiply(s_cos_t, inv_mask), tf.multiply(
# cos_mt_temp, labels), name='arcface_loss_output'), labels*m3)
return s_cos_t ##因为在预测的时候不能+m,要用实际的theta角度,所以 是s_cos_t,而不是 cos_mtdef arcface_as_predict(embedding, weights, s=64., m=0.5, m3=0.1):
# def arcface_loss(x,y)
'''
:param embedding: the input embedding vectors
:param labels: the input labels, the shape should be eg: (batch_size, 1)
:param s: scalar value default is 64
:param out_num: output class num
:param m: the margin value, default is 0.5
:return: the final cacualted output, this output is send into the tf.nn.softmax directly
'''
cos_m = math.cos(m)
sin_m = math.sin(m)
mm = sin_m * m # issue 1
threshold = math.cos(math.pi - m)
# with tf.variable_scope('arcface_loss'):
# inputs and weights norm
embedding_norm = tf.norm(embedding, axis=1, keepdims=True)
embedding = tf.div(embedding, embedding_norm, name='norm_embedding')
# weights = tf.get_variable(name='embedding_weights', shape=(embedding.get_shape().as_list()[-1], out_num),
# initializer=w_init, dtype=tf.float32)
# cos(theta+m)
# labels = tf.Print(labels, [tf.shape(labels)], 'embedding shape')
cos_t = tf.matmul(embedding, weights, name='cos_t')
cos_t2 = tf.square(cos_t, name='cos_2')
sin_t2 = tf.subtract(1., cos_t2, name='sin_2')
sin_t = tf.sqrt(sin_t2, name='sin_t')
cos_mt = s * tf.subtract(tf.multiply(cos_t, cos_m),
tf.multiply(sin_t, sin_m), name='cos_mt')
# this condition controls the theta+m should in range [0, pi]
# 0<=theta+m<=pi
# -m<=theta<=pi-m
cond_v = cos_t - threshold
# swift_act(x):
# cond = tf.cast(swish_act(cond_v), dtype=tf.bool)
cond = tf.cast(tf.nn.relu(cond_v, name='if_else'), dtype=tf.bool)
keep_val = s * (cos_t - mm)
cos_mt_temp = tf.where(cond, cos_mt, keep_val)
# labels = tf.one_hot(labels, depth=out_num, name='one_hot_mask')
# mask = tf.squeeze(mask, 1)
# labels = tf.Print(labels, [tf.shape(labels)], 'label shape')
# print('cost_t ={}'.format(cos_t.shape))
# print('embeddings = {}'.format(embedding.shape))
# print('labels = {}'.format(labels.shape))
# inv_mask = tf.subtract(1., tf.cast(labels,tf.float32), name='inverse_mask')
# print('inv_mask = {}'.format(inv_mask.shape))
s_cos_t = tf.multiply(s, cos_t, name='scalar_cos_t')
# output = tf.add(tf.multiply(s_cos_t, inv_mask), tf.multiply(
# cos_mt_temp, labels), name='arcface_loss_output')
# output = tf.subtract(tf.add(tf.multiply(s_cos_t, inv_mask), tf.multiply(
# cos_mt_temp, labels), name='arcface_loss_output'), labels*m3)
return s_cos_t ##因为在预测的时候不能+m,要用实际的theta角度,所以 是s_cos_t,而不是 cos_mt