我正在使用TensorFlow,并且有2个张量prediction
和label
,其中的标签不是很热。如何确定预测的准确性?我尝试使用tf.metrics.accuracy
和tf.metrics.auc
,但都返回了[0, 0]
,这是我的神经网络:
import tensorflow.compat.v1 as tf
from random import randint
import numpy as np
import math
from tensorflow.examples.tutorials.mnist import input_data
global mnist
class AICore:
def __init__(self, nodes_in_each_layer):
self.data_in_placeholder = tf.placeholder("float", [None, nodes_in_each_layer[0]])
self.data_out_placeholder = tf.placeholder("float")
self.init_neural_network(nodes_in_each_layer)
def init_neural_network(self, n_nodes_h):
#n_nodes_h constains the number of nodes for each layer
#n_nodes_h[0] = number of inputs
#n_nodes_h[-1] = number of outputs
self.layers = [None for i in range(len(n_nodes_h)-1)]
for i in range(1, len(n_nodes_h)):
self.layers[i-1] = {"weights":tf.Variable(tf.random_normal([n_nodes_h[i-1], n_nodes_h[i]])),
"biases":tf.Variable(tf.random_normal([n_nodes_h[i]]))}
def neural_network_model(self, data):
for i in range(len(self.layers)):
data = tf.matmul(data, self.layers[i]["weights"]) + self.layers[i]["biases"]
if i != len(self.layers)-1:
data = tf.nn.relu(data)
return data
def train_neural_network(self, data, hm_epochs):
prediction = self.neural_network_model(self.data_in_placeholder)
cost = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=prediction, labels=self.data_out_placeholder))
accuracy = ???
optimiser = tf.train.AdamOptimizer().minimize(cost)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
sess.run(tf.local_variables_initializer())
for epoch in range(hm_epochs):
for _ in range(int(data.length/batch_size)):
epoch_x, epoch_y = data.next_batch(batch_size)
feed_dict = {self.data_in_placeholder: epoch_x, self.data_out_placeholder: epoch_y}
_, c = sess.run([optimiser, cost], feed_dict=feed_dict)
print("accuracy =", accuracy_percentage)
n_nodes_h = [784, 500, 500, 500, 10]
batch_size = 100
hm_epochs = 10
mnist = input_data.read_data_sets("/tmp/data/", one_hot = True)
class Data:
def __init__(self):
self.length = mnist.train.num_examples
def next_batch(self, batch_size):
global mnist
return mnist.train.next_batch(batch_size)
data_generator = Data()
core = AICore(n_nodes_h)
core.train_neural_network(data_generator, hm_epochs)
但是我不知道如何计算准确度百分比。
答案 0 :(得分:10)
对于这种要求,敏感度是一个很好的指标(敏感度基本上代表了模型在检测准确性(例如肯定/欺诈)方面的表现)。有一些open-source
python项目将帮助您继续前进:访问参考:sensitivity-analysis。
可以使用您的预测的confusion matrix
计算灵敏度,例如:
from sklearn.metrics import confusion_matrix
混淆矩阵基本上是原始分布与预测分布的表示。然后可以使用此矩阵上的非常简单的公式来计算灵敏度。您可以详细了解和练习Confusion matrix
:请访问参考文献:confusion-matrix。
#Confusion matrix, Accuracy, sensitivity and specificity
from sklearn.metrics import confusion_matrix
我对一个数据集进行了分析,例如test-bits
,要计算准确性,敏感性和特异性,您可以详细了解:请访问参考文献:calculate-sensitivity-specifity-of-neural-network。
cm1 = confusion_matrix(test_df[['test']],predicted_class1)
print('Confusion Matrix : \n', cm1)
混乱矩阵:[[37767 4374] [30521 27338]]
然后计算所需的参数:
total1=sum(sum(cm1))
#####from confusion matrix calculate accuracy
accuracy1=(cm1[0,0]+cm1[1,1])/total1
print ('Accuracy : ', accuracy1)
sensitivity1 = cm1[0,0]/(cm1[0,0]+cm1[0,1])
print('Sensitivity : ', sensitivity1 )
specificity1 = cm1[1,1]/(cm1[1,0]+cm1[1,1])
print('Specificity : ', specificity1)
准确度:0.65105
灵敏度:0.896205595501
特异性:0.472493475518