gpt4 book ai didi

python - 使用 Pearson 相关性而不是 tensorflow 中的准确性来报告性能

转载 作者:太空宇宙 更新时间:2023-11-03 15:48:21 25 4
gpt4 key购买 nike

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

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

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 session 中完成所有这些操作:

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 方面还是个新手。您能否指出这里出了什么问题以及如何修复它?

最佳答案

streaming_pearson_correlation函数委托(delegate)给 streaming_covariance ,根据文档,它创建了四个局部变量

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

对于 Tensorflow r0.12+,请使用

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

对于 Tensorflow <= r0.11,使用

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

更多:What is a local variable in tensorflow?

关于python - 使用 Pearson 相关性而不是 tensorflow 中的准确性来报告性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41568721/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com