Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Variables 保存并加载Keras/Tensorflow损耗函数';内部状态变量_Variables_Tensorflow_Keras_Center_Loss - Fatal编程技术网

Variables 保存并加载Keras/Tensorflow损耗函数';内部状态变量

Variables 保存并加载Keras/Tensorflow损耗函数';内部状态变量,variables,tensorflow,keras,center,loss,Variables,Tensorflow,Keras,Center,Loss,我正在Keras中使用Tensorflow后端实现一个模型,该模型使用本文中定义的中心损失函数。我目前正在使用我在Keras存储库中找到的中心丢失的实现: def _center_loss_func(features, labels, alpha, num_classes, centers, feature_dim): assert feature_dim == features.get_shape()[1] labels = K.re

我正在Keras中使用Tensorflow后端实现一个模型,该模型使用本文中定义的中心损失函数。我目前正在使用我在Keras存储库中找到的中心丢失的实现:

def _center_loss_func(features, labels, alpha, num_classes,
                  centers, feature_dim):
    assert feature_dim == features.get_shape()[1]    
    labels = K.reshape(labels, [-1])
    labels = tf.to_int32(labels)
    centers_batch = tf.gather(centers, labels)
    diff = (1 - alpha) * (centers_batch - features)
    centers = tf.scatter_sub(centers, labels, diff)
    loss = tf.reduce_mean(K.square(features - centers_batch))
    return loss

def get_center_loss(alpha, num_classes, feature_dim):
    """Center loss based on the paper "A Discriminative 
       Feature Learning Approach for Deep Face Recognition"
       (http://ydwen.github.io/papers/WenECCV16.pdf)
   """    
   # Each output layer use one independed center: scope/centers
   centers = K.zeros([num_classes, feature_dim])
   @functools.wraps(_center_loss_func)
   def center_loss(y_true, y_pred):
       return _center_loss_func(y_pred, y_true, alpha, 
                                num_classes, centers, feature_dim)
   return center_loss
我做了一些测试,这个实现似乎正在工作,因为类正在被分离并可视化地聚集在中心周围。问题是培训结束后,我无法访问中心值。

如果我想从保存的检查点恢复训练,我需要它们。代码总是将中心初始化为零,这是因为要使用加载模型,我必须在
自定义对象
字典中提供
center\u loss
。但是,我只能直接访问
get\u center\u loss
函数,该函数将
centers
实例化为零并返回
center\u loss
。我的想法是将一个numpy数组
initial\u centers
传递给
get\u center\u loss
,并将这些中心初始化为
centers=K.variable(value=initial\u centers)

另一种可能是将centers变量移动到层或优化器内部,但我不知道如何操作


任何帮助都将不胜感激。

看起来中心并不是代码中的变量。看起来像是张量。因此,中心的价值永远不会得到更新。尝试使用tf.get_变量构造中心,然后它将自动保存/加载到检查点或从检查点加载。@Alexandre,据我所知,它应该是一个变量,对吗?无论如何,我会把问题更新得更具体一点。谢谢你的回答!啊,我明白了。因此,代码中的问题是,在
centers=tf.scatter\u sub(centers,labels,diff)
中创建的值中心从未使用过,因此tensorflow不会执行赋值操作。用tf.control_dependencies([centers]):块将损失的定义放在
中可以解决这个问题。