如何以简单的方式计算张量流中的auc?

时间:2017-09-25 15:24:26

标签: tensorflow

sess.run(tf.metrics.auc(labels, preds))

我尝试使用tensorflow来计算auc。我的版本是1.0。 但是有一些错误。 任何人都可以举例说明如何使用它? 我看到以前的一些问题,但似乎已经过时了。 任何人都可以在新版本中提供正确的代码?     FailedPreconditionError:尝试使用未初始化的值auc / false_positives              [[节点:auc / false_positives / read = IdentityT = DT_FLOAT,_class = [“loc:@ auc / false_positives”],_ device =“/ job:localhost / replica:0 / task:0 / cpu:0”]] < / p>

Caused by op u'auc/false_positives/read', defined at:
  File "/home/xuemeng.cyn/anaconda2/bin/ipython", line 6, in <module>
    sys.exit(IPython.start_ipython())
  File "/home/xuemeng.cyn/anaconda2/lib/python2.7/site-packages/IPython/__init__.py", line 119, in start_ipython
    return launch_new_instance(argv=argv, **kwargs)
  File "/home/xuemeng.cyn/anaconda2/lib/python2.7/site-packages/traitlets/config/application.py", line 658, in launch_instance
    app.start()
  File "/home/xuemeng.cyn/anaconda2/lib/python2.7/site-packages/IPython/terminal/ipapp.py", line 348, in start
    self.shell.mainloop()
  File "/home/xuemeng.cyn/anaconda2/lib/python2.7/site-packages/IPython/terminal/interactiveshell.py", line 486, in mainloop
    self.interact()
  File "/home/xuemeng.cyn/anaconda2/lib/python2.7/site-packages/IPython/terminal/interactiveshell.py", line 477, in interact
    self.run_cell(code, store_history=True)
  File "/home/xuemeng.cyn/anaconda2/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 2717, in run_cell
    interactivity=interactivity, compiler=compiler, result=result)
  File "/home/xuemeng.cyn/anaconda2/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 2827, in run_ast_nodes
    if self.run_code(code, result):
  File "/home/xuemeng.cyn/anaconda2/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 2881, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-29-2bca3b61757e>", line 1, in <module>
    sess.run(tf.metrics.auc(labels, preds))
  File "/home/xuemeng.cyn/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/metrics_impl.py", line 626, in auc
    labels, predictions, thresholds, weights)
  File "/home/xuemeng.cyn/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/metrics_impl.py", line 544, in _confusion_matrix_at_thresholds
    false_p = _create_local('false_positives', shape=[num_thresholds])
  File "/home/xuemeng.cyn/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/metrics_impl.py", line 196, in _create_local
    validate_shape=validate_shape)
  File "/home/xuemeng.cyn/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 1679, in variable
    caching_device=caching_device, name=name, dtype=dtype)
  File "/home/xuemeng.cyn/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/variables.py", line 199, in __init__
    expected_shape=expected_shape)
  File "/home/xuemeng.cyn/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/variables.py", line 330, in _init_from_args
    self._snapshot = array_ops.identity(self._variable, name="read")
  File "/home/xuemeng.cyn/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/gen_array_ops.py", line 1400, in identity
    result = _op_def_lib.apply_op("Identity", input=input, name=name)
  File "/home/xuemeng.cyn/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 767, in apply_op
    op_def=op_def)
  File "/home/xuemeng.cyn/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2630, in create_op
    original_op=self._default_original_op, op_def=op_def)
  File "/home/xuemeng.cyn/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1204, in __init__
    self._traceback = self._graph._extract_stack()  # pylint: disable=protected-access

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

1 个答案:

答案 0 :(得分:1)

auc函数创建用于计算true_positives的局部变量:true_negativesfalse_positivesfalse_negativesAUC。所以你需要初始化它们:

tf.local_variables_initializer().run()