Python:使用TensorFlow计算神经网络的准确性

The*_*ard 0 python average tensorflow

我使用TensorFlow,我有2张量prediction,并label在标签不是一个热点。如何确定预测的准确性?我尝试使用tf.metrics.accuracytf.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)
Run Code Online (Sandbox Code Playgroud)

但我不知道如何计算准确度百分比。

Muh*_*man 9

对于这样的要求,灵敏度是一个很好的度量标准(灵敏度基本上代表了模型在检测精度(例如阳性/欺诈)方面的质量)。有一些open-sourcepython项目将帮助您继续前进:访问参考:敏感性分析

可以使用以下confusion matrix预测来计算灵敏度:

from sklearn.metrics import confusion_matrix
Run Code Online (Sandbox Code Playgroud)

混淆矩阵基本上是原始分布与预测分布的表示。然后可以使用此矩阵上的非常简单的公式来计算灵敏度。您可以Confusion matrix详细学习和实践:访问参考:confusion-matrix

#Confusion matrix, Accuracy, sensitivity and specificity
from sklearn.metrics import confusion_matrix
Run Code Online (Sandbox Code Playgroud)

我已经对数据集进行了分析,例如test-bits,为了计算准确性,敏感性和特异性,您可以详细了解:访问参考:calculate-sensitivity-specifity-neural-network

cm1 = confusion_matrix(test_df[['test']],predicted_class1)
print('Confusion Matrix : \n', cm1)
Run Code Online (Sandbox Code Playgroud)

混淆矩阵:[[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)
Run Code Online (Sandbox Code Playgroud)

精度:0.65105

灵敏度:0.896205595501

特异性:0.472493475518