如何在 TensorFlow 中计算 CNN 的准确性

How to compute accuracy of CNN in TensorFlow

我是 TensorFlow 的新手。我正在用我自己的数据集进行二元分类。但是我不知道如何计算准确性。谁能帮我做这个?

我的分类器有 5 个卷积层,后面跟着 2 个全连接层。最后的 FC 层的输出维度为 2,我使用了它:

prob = tf.nn.softmax(classification_features, name="output")

只需计算正确预测的百分比:

prediction = tf.math.argmax(prob, axis=1)
equality = tf.math.equal(prediction, correct_answer)
accuracy = tf.math.reduce_mean(tf.cast(equality, tf.float32))

在 Tensorflow 中更新 2020-11-23 Keras

现在您只需在 model.compilemetrics 参数中指定您想要的即可。

这个 post 来自 3.6 年前,当时 tensorflow 仍处于版本 1。现在 Tensorflow.org 建议使用 Keras 调用,您可以像这样指定您想要的准确性:

model.compile(loss='mse',optimizer='sgd',metrics=['accuracy'])
model.fit(x,y)

砰!当您 运行 "model.fit".

时,您的报告是准确的

如果您使用的是旧版本的 tensorflow 或者只是从头开始编写它,@Androbin 解释得很好。