【问题标题】:Using Pearson Correlation instead of accuracy in tensorflow to report performance使用 Pearson Correlation 而不是 tensorflow 中的准确性来报告性能
【发布时间】:2017-05-24 22:28:05
【问题描述】:

在我的 TF 代码中,我在模型中定义了以下内容:

with tf.name_scope("loss"):
      self.loss = self.contrastive_loss(self.input_y,self.distance, batch_size) 
with tf.name_scope("accuracy"):
      correct_predictions = tf.equal(self.distance, self.input_y)
      self.accuracy=tf.reduce_mean(tf.cast(correct_predictions, "float"), name="accuracy")

但我意识到在我的任务中使用 pearson 相关性更合适。所以,我把它改成了这样:

with tf.name_scope("loss"):
      self.loss = self.contrastive_loss(self.input_y,self.distance, batch_size) 
with tf.name_scope("pearson"):
      self.pearson = tf.contrib.metrics.streaming_pearson_correlation(self.distance, self.input_y, name="pearson")

我在计算图中调用我的模型,在训练循环中我有这个评估代码:

acc = dev_step(dev_x1_batch, dev_x2_batch, dev_y_batch, X) 

调用这个方法:

def dev_step(x1_batch, x2_batch, y_batch, X):
    """
    A single training step
    """ 
    if random()>0.5:
        feed_dict = {
                         siameseModel.input_x1: x1_batch,
                         siameseModel.input_x2: x2_batch,
                         siameseModel.input_y: y_batch,
                         siameseModel.dropout_keep_prob: FLAGS.dropout_keep_prob,
                         siameseModel.embedding_placeholder: X
        }
    else:
        feed_dict = {
                         siameseModel.input_x1: x2_batch,
                         siameseModel.input_x2: x1_batch,
                         siameseModel.input_y: y_batch,
                         siameseModel.dropout_keep_prob: FLAGS.dropout_keep_prob,
                         siameseModel.embedding_placeholder: X
        }
    step, _, loss, pearson, dist, out1, out2 = sess.run([global_step, siameseModel.embedding_init, siameseModel.loss, siameseModel.pearson, siameseModel.distance, siameseModel.out1, siameseModel.out2],  feed_dict)
    print("DEV {}: step {}, loss {:g}, acc {:g}".format(time_str, step, loss, pearson))
    return pearson

我在 TF 会话中所做的所有这些:

sess = tf.Session(config=session_conf)
....code to create instance of model, definition of training procedure
sess.run(tf.initialize_all_variables())
...training loop

但我收到此错误:

tensorflow/core/framework/op_kernel.cc:968] Failed precondition: Attempting to use uninitialized value pearson/pearson/covariance/count
 [[Node: pearson/pearson/covariance/count/read = Identity[T=DT_FLOAT, _class=["loc:@pearson/pearson/covariance/count"], _device="/job:localhost/replica:0/task:0/cpu:0"](pearson/pearson/covariance/count)]]
tensorflow/core/framework/op_kernel.cc:968] Failed precondition: Attempting to use uninitialized value pearson/pearson/covariance/count
 [[Node: pearson/pearson/covariance/count/read = Identity[T=DT_FLOAT, _class=["loc:@pearson/pearson/covariance/count"], _device="/job:localhost/replica:0/task:0/cpu:0"](pearson/pearson/covariance/count)]]
tensorflow/core/framework/op_kernel.cc:968] Failed precondition: Attempting to use uninitialized value pearson/pearson/covariance/count
 [[Node: pearson/pearson/covariance/count/read = Identity[T=DT_FLOAT, _class=["loc:@pearson/pearson/covariance/count"], _device="/job:localhost/replica:0/task:0/cpu:0"](pearson/pearson/covariance/count)]]
tensorflow/core/framework/op_kernel.cc:968] Failed precondition: Attempting to use uninitialized value pearson/pearson/covariance/count
 [[Node: pearson/pearson/covariance/count/read = Identity[T=DT_FLOAT, _class=["loc:@pearson/pearson/covariance/count"], _device="/job:localhost/replica:0/task:0/cpu:0"](pearson/pearson/covariance/count)]]
tensorflow/core/framework/op_kernel.cc:968] Failed precondition: Attempting to use uninitialized value pearson/pearson/covariance/count
 [[Node: pearson/pearson/covariance/count/read = Identity[T=DT_FLOAT, _class=["loc:@pearson/pearson/covariance/count"], _device="/job:localhost/replica:0/task:0/cpu:0"](pearson/pearson/covariance/count)]]
tensorflow/core/framework/op_kernel.cc:968] Failed precondition: Attempting to use uninitialized value pearson/pearson/covariance/count
 [[Node: pearson/pearson/covariance/count/read = Identity[T=DT_FLOAT, _class=["loc:@pearson/pearson/covariance/count"], _device="/job:localhost/replica:0/task:0/cpu:0"](pearson/pearson/covariance/count)]]
Traceback (most recent call last):
  File "train.py", line 260, in <module>
acc = dev_step(dev_x1_batch, dev_x2_batch, dev_y_batch, X)
  File "train.py", line 207, in dev_step
step, _, loss, pearson, dist, out1, out2 = sess.run([global_step, siameseModel.embedding_init, siameseModel.loss, siameseModel.pearson, siameseModel.distance, siameseModel.out1, siameseModel.out2],  feed_dict)
  File "/Users/kurt/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 717, in run
run_metadata_ptr)
  File "/Users/kurt/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 915, in _run
feed_dict_string, options, run_metadata)
  File "/Users/kurt/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 965, in _do_run
target_list, options, run_metadata)
  File "/Users/kurt/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 985, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors.FailedPreconditionError: Attempting to use uninitialized value pearson/pearson/covariance/count
 [[Node: pearson/pearson/covariance/count/read = Identity[T=DT_FLOAT, _class=["loc:@pearson/pearson/covariance/count"], _device="/job:localhost/replica:0/task:0/cpu:0"](pearson/pearson/covariance/count)]]

Caused by op u'pearson/pearson/covariance/count/read', defined at:
  File "train.py", line 97, in <module>
batch_size=FLAGS.batch_size)
  File "filepath/", line 89, in __init__
self.pearson = tf.contrib.metrics.streaming_pearson_correlation(self.distance, self.input_y, name="pearson")
  File "/Users/kurt/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/contrib/metrics/python/ops/metric_ops.py", line 2490, in streaming_pearson_correlation
predictions, labels, weights=weights, name='covariance')
  File "/Users/kurt/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/contrib/metrics/python/ops/metric_ops.py", line 2367, in streaming_covariance
count = _create_local('count', [])
  File "/Users/kurt/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/contrib/metrics/python/ops/metric_ops.py", line 135, in _create_local
collections=collections)
  File "/Users/kurt/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/ops/variables.py", line 215, in __init__
dtype=dtype)
  File "/Users/kurt/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/ops/variables.py", line 327, in _init_from_args
self._snapshot = array_ops.identity(self._variable, name="read")
  File "/Users/kurt/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/ops/gen_array_ops.py", line 1128, in identity
result = _op_def_lib.apply_op("Identity", input=input, name=name)
  File "/Users/kurt/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 749, in apply_op
op_def=op_def)
  File "/Users/kurt/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2380, in create_op
original_op=self._default_original_op, op_def=op_def)
  File "/Users/kurt/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1298, in __init__
self._traceback = _extract_stack()

FailedPreconditionError (see above for traceback): Attempting to use uninitialized value pearson/pearson/covariance/count
 [[Node: pearson/pearson/covariance/count/read = Identity[T=DT_FLOAT, _class=["loc:@pearson/pearson/covariance/count"], _device="/job:localhost/replica:0/task:0/cpu:0"](pearson/pearson/covariance/count)]]

对不起,如果问题非常基本 - 我还是 TF 的新手。您能否指出这里出了什么问题以及如何解决?

【问题讨论】:

    标签: python tensorflow


    【解决方案1】:

    streaming_pearson_correlation 函数委托给streaming_covariance,并根据文档创建四个局部变量

    tf.initialize_all_variables() 不初始化局部变量。

    对于 TensorFlow r0.12+,使用

    tf.group(tf.global_variables_initializer(), tf.local_variables_initializer())
    

    对于 TensorFlow

    tf.group(tf.initialize_all_variables(), tf.initialize_variables(tf.local_variables()))
    

    更多:What is a local variable in tensorflow?

    【讨论】:

    • 感谢@standby!我用 sess.run(tf.group(tf.global_variables_initializer(), tf.local_variables_initializer())) 替换了 'sess.run(tf.initialize_all_variables())' 但我得到了这个错误: AttributeError: 'module' object has no属性 'global_variables_initializer' 我使用 TF 0.11.0rc0
    • @standby 在这篇文章中,它说我应该使用上一个:stackoverflow.com/questions/40511562/…你能澄清一下吗?
    • @kurt initialize_all_variables 在 r0.12 中被替换为 global_vairables_initializer。我认为您可以在 中使用 tf.initialize_variables(tf.local_variables())
    • @standby 帮助感谢,但在网络的另一部分出现了另一个类似的问题:tensorflow/core/framework/op_kernel.cc:968] 失败的前提条件:尝试使用未初始化的值 bwside1/BiRNN/ FW/MultiRNNCell/Cell0/BasicLSTMCell/Linear/Matrix [[节点:bwside1/BiRNN/FW/MultiRNNCell/Cell0/BasicLSTMCell/Linear/Matrix/read = Identity[T=DT_FLOAT, _device="/job:localhost/replica:0 /task:0/cpu:0"](bwside1/BiRNN/FW/MultiRNNCell/Cell0/BasicLSTMCell/Linear/Matrix)]] 可能我应该尝试升级,看看它是否解决了这个问题。
    • 我的意思是你可以使用 tf.initialize_variables(tf.local_variables()) 而不是 tf.local_variables_initializer()。您仍应将其与 tf.initialize_all_variables() 分组以初始化全局变量。
    猜你喜欢
    • 1970-01-01
    • 2018-11-07
    • 1970-01-01
    • 1970-01-01
    • 2017-03-08
    • 2020-11-22
    • 2011-09-05
    • 1970-01-01
    • 2021-06-04
    相关资源
    最近更新 更多