使用Pearson Correlation而不是tensorflow中的准确度来报告性能

时间:2017-01-10 12:18:54

标签: python tensorflow

在我的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")

但我意识到在我的任务中使用皮尔逊相关更合适。所以,我改为:

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中相当新手。你能否指出这里有什么问题以及如何解决?

1 个答案:

答案 0 :(得分:1)

根据文档,

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

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

对于Tensorflow r0.12 +,请使用

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

对于Tensorflow&lt; = r0.11,请使用

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

更多:What is a local variable in tensorflow?