【问题标题】:Python: Calculating the accuracy of a neural network using TensorFlowPython:使用 TensorFlow 计算神经网络的准确性
【发布时间】:2019-10-28 20:17:41
【问题描述】:

我正在使用 TensorFlow,我有 2 个张量 predictionlabel,其中标签不是一个热门的。我如何计算出我的预测的准确性?我尝试使用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)

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

【问题讨论】:

标签: python tensorflow average


【解决方案1】:

对于这样的要求,敏感性是一个很好的指标(敏感性基本上代表了模型在检测准确性方面的好坏,例如阳性/欺诈)。有一些open-source python 项目可以帮助你前进:访问参考:sensitivity-analysis

可以使用您的预测的confusion matrix 计算灵敏度,例如:

from sklearn.metrics import confusion_matrix

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

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

#Confusion matrix, Accuracy, sensitivity, and specificity
from sklearn.metrics import confusion_matrix


cm1 = confusion_matrix(test_df[['test']],predicted_class1)
print('Confusion Matrix :', 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

【讨论】:

    猜你喜欢
    • 2017-08-05
    • 2020-05-15
    • 2018-10-19
    • 2017-08-31
    • 2015-10-22
    • 2019-01-07
    • 1970-01-01
    • 2021-08-03
    相关资源
    最近更新 更多