【发布时间】:2019-10-28 20:17:41
【问题描述】:
我正在使用 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)
但我不知道如何以百分比计算准确率。
【问题讨论】:
-
我们需要更多信息来回答这个问题。见how to create a complete, minimal, reproducible example
-
现在好点了吗?
-
不,我应该能够复制和粘贴您的代码,它会重现您的错误/问题。
标签: python tensorflow average