tensorflow center loss代码注释

def center_loss(features, label, alfa, nrof_classes):
    """Center loss based on the paper "A Discriminative Feature Learning Approach for Deep Face Recognition"
       (http://ydwen.github.io/papers/WenECCV16.pdf)
    """
    #获取特征向量长度
    nrof_features = features.get_shape()[1]
    #生成可以共享的变量centers,由于center loss在计算图中只存在于一个节点处,因此这个变量只使用一次
    #不需要所谓的variable_scope,就可以实现每次共享?
    centers = tf.get_variable('centers', [nrof_classes, nrof_features], dtype=tf.float32,
        initializer=tf.constant_initializer(0), trainable=False)
    label = tf.reshape(label, [-1])
    #取出对应label下对应的center值,注意label里面的值可能会重复,因为一个标签下有可能会出现多个人
    centers_batch = tf.gather(centers, label)
    #求特征点到中心的距离并乘以一定的系数,alfa是center的更新速度,越大代表更新的越慢
    diff = (1 - alfa) * (centers_batch - features)
    #更新center,输出是将对应于label的centers减去对应的diff,如果同一个标签出现多次,那么就减去多次
    centers = tf.scatter_sub(centers, label, diff)
    #求center loss,这里是将l2_loss里面的值进行平方相加,再除以2,并没有进行开方
    loss = tf.nn.l2_loss(features - centers_batch)
    return loss, centers

你可能感兴趣的:(深度学习(deep,learning),tensorflow)