我确实构建了一个基于宽而深的 Tensorflow 教程 ( here is its "Main" file this question is referring to ) 的二元分类器,这在很大程度上要归功于我在 SO 上获得的帮助,仅在“宽”模式下使用。
我用来提取分类猜测的函数是:
pred_iter = model.predict(input_fn=lambda: input_fn(FLAGS.test_data, 1, False, 1)) for pred in pred_iter:
print(pred['classes'])
它在分类方面效果很好,但分类返回的只是 0
或 1
,因此缺乏概率方面。
我想要将分类表示为 0 到 1 之间的数字,以便了解网络对其猜测的“确定性”。
We will train a logistic regression model, and given an individual's information our model will output a number between 0 and 1, which can be interpreted as the probability [...]
但我无法获得预测的这种概率方面。
我尝试使用许多答案中列出的 prob_a 函数,但它可能指的是旧版本的 TF
,所以没有运气。
如何有效地将分类作为概率而不是“一次性”进行?
感谢任何帮助。
我在 Python 3.5、操作系统 Ubuntu 16.04 LTS 上使用 TF 1.8。
最佳答案
tf.estimator.LinearClassifier
实例返回您可以使用的值的字典。您仅在代码中使用 pred[ 'classes' ]
,但概率值也位于 pred[ 'probabilities' ]
中。您也可以直接
print( pred )
它将输出与此类似的内容(带有虚拟数据):
{'logits': array([1.4264423], dtype=float32),
'logistic': array([0.80634636], dtype=float32),
'probabilities': array([0.19365363, 0.80634636], dtype=float32),
'class_ids': array(1),
'classes': array([b'1'], dtype=object) }
这样您就可以看到您还可以访问logits
和class_ids
。
第 1 类的概率是 pred[ 'probabilities' ]
中的第二个值,据我所知,这就是您想要使用的值。
关于python - Wide_deep分类器模型,需要预测概率值,不仅是 "best guess",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50316789/