数学总结集

MMR公式:

MMR=\lambda Sim_{1}(D_{i},Q)-(1-\lambda)maxSim_{2}(D_{i},D_{j})

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

d(x,y)=\sqrt{\sum_{i}^{m}(x_{i}-y_{i})^{^{2}}}

欧式距离归一化

d(x,y)=\frac{\sqrt{\sum_{i}^{m}(x_{i}-y_{i})^{^{2}}}}{\sqrt{\sum x^{2}_{i}}}

向量的模 ,1-范数 2-范数

向量的模 \overrightarrow{ab}=\sqrt{a^{2}+b^{2}}

1-范数 \overrightarrow{ab}=\left | a \right |+\left | b \right |

2-范数\overrightarrow{ab}=a^{2}+b^{2}

Sigmoid:  0 ~ 1

 S(x)=\frac{1}{1+e^{^{-x}}}=\frac{e^{x}}{1+e^{x}}

Tanh: -1 ~ 1

T(x)=\frac{sinh(x)}{cosh(x)}

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值