Python TensorFlow中的二进制分类,意外的大值损失和准确性

Python TensorFlow中的二进制分类,意外的大值损失和准确性,python,machine-learning,neural-network,tensorflow,logistic-regression,Python,Machine Learning,Neural Network,Tensorflow,Logistic Regression,我正在尝试使用一个深度神经网络架构来根据二进制标签值--1和+1进行分类。下面是我在tensorflow中执行此操作的代码 import tensorflow as tf import numpy as np from preprocess import create_feature_sets_and_labels train_x,train_y,test_x,test_y = create_feature_sets_and_labels() x = tf.placeholder('floa

我正在尝试使用一个深度神经网络架构来根据二进制标签值--1和+1进行分类。下面是我在
tensorflow
中执行此操作的代码

import tensorflow as tf
import numpy as np
from preprocess import create_feature_sets_and_labels

train_x,train_y,test_x,test_y = create_feature_sets_and_labels()

x = tf.placeholder('float', [None, 5])
y = tf.placeholder('float')

n_nodes_hl1 = 500
n_nodes_hl2 = 500
n_nodes_hl3 = 500

n_classes = 1
batch_size = 100

def neural_network_model(data):

    hidden_1_layer = {'weights':tf.Variable(tf.random_normal([5, n_nodes_hl1])),
                      'biases':tf.Variable(tf.random_normal([n_nodes_hl1]))}

    hidden_2_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl1, n_nodes_hl2])),
                      'biases':tf.Variable(tf.random_normal([n_nodes_hl2]))}

    hidden_3_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl2, n_nodes_hl3])),
                      'biases':tf.Variable(tf.random_normal([n_nodes_hl3]))}

    output_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl3, n_classes])),
                      'biases':tf.Variable(tf.random_normal([n_classes]))}


    l1 = tf.add(tf.matmul(data, hidden_1_layer['weights']), hidden_1_layer['biases'])
    l1 = tf.nn.relu(l1)

    l2 = tf.add(tf.matmul(l1, hidden_2_layer['weights']), hidden_2_layer['biases'])
    l2 = tf.nn.relu(l2)

    l3 = tf.add(tf.matmul(l2, hidden_3_layer['weights']), hidden_3_layer['biases'])
    l3 = tf.nn.relu(l3)

    output = tf.transpose(tf.add(tf.matmul(l3, output_layer['weights']), output_layer['biases']))
    return output



def train_neural_network(x):
    prediction = neural_network_model(x)
    cost = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(prediction, y))
    optimizer = tf.train.AdamOptimizer().minimize(cost)

    hm_epochs = 10

    with tf.Session() as sess:
        sess.run(tf.initialize_all_variables())

        for epoch in range(hm_epochs):
            epoch_loss = 0
            i = 0
            while i < len(train_x):
                start = i
                end = i + batch_size
                batch_x = np.array(train_x[start:end])
                batch_y = np.array(train_y[start:end])

                _, c = sess.run([optimizer, cost], feed_dict={x: batch_x,
                                              y: batch_y})
                epoch_loss += c
                i+=batch_size

            print('Epoch', epoch, 'completed out of', hm_epochs, 'loss:', epoch_loss)

        # correct = tf.equal(tf.argmax(prediction, 1), tf.argmax(y, 1))
        # accuracy = tf.reduce_mean(tf.cast(correct, 'float'))

        print (test_x.shape)
        accuracy = tf.nn.l2_loss(prediction-y,name="squared_error_test_cost")/test_x.shape[0]
        print('Accuracy:', accuracy.eval({x: test_x, y: test_y}))

train_neural_network(x)
我不明白我得到的值是否正确,因为真正缺乏非MNIST二进制分类示例。准确度和我预期的完全不同。我期望的是一个百分比,而不是那么大的值

我也有点不确定机器学习背后的理论,这就是为什么我不能判断我使用tensorflow方法的正确性

有人能告诉我我对二元分类的方法是否正确吗? 我的代码的准确性部分是否正确?

由此:

二进制标签值--1和+1

。我假设
train_y
test_y
中的值实际上是-1.0和+1.0

这对于您选择的损失函数
sigmoid\u cross\u entropy\u和\u logits
——假设为0.0和+1.0,效果不太好。负
y
值正在造成混乱!然而,损失函数的选择有利于二值分类。我建议将
y
值更改为0和1

此外,从技术上讲,网络的输出不是最终预测。损失函数
sigmoid\u cross\u entropy\u with\u logits
设计用于在输出层中使用sigmoid传递函数的网络,尽管您已经正确地认识到,在这之前应用了损失函数。因此,您的培训代码看起来是正确的

不过,我不能100%肯定tf.transpose,如果你把它去掉,我会看看会发生什么

output = tf.add(tf.matmul(l3, output_layer['weights']), output_layer['biases'])
不管怎样,这都是“logit”输出,但不是您的预测。对于非常自信的预测,
output
的值可能会变得很高,这可能解释了由于缺少sigmoid函数而导致的较高值。因此,添加一个预测张量(表示示例处于正类的概率/置信度):

你可以用它来计算准确度。您的精度计算不应基于L2错误,而应基于正确值的总和-更接近您注释掉的代码(似乎来自多类分类)。要与二进制分类的true/false进行比较,需要设置预测阈值,并与true标签进行比较。大概是这样的:

 predicted_class = tf.greater(prediction,0.5)
 correct = tf.equal(predicted_class, tf.equal(y,1.0))
 accuracy = tf.reduce_mean( tf.cast(correct, 'float') )

精度值应介于0.0和1.0之间。如果你想要一个百分比,当然,只要乘以100

您的网络似乎不稳定。尝试使用较小的层执行计算,或者使用xavier glorot初始化?我认为您的缩进不适合讨论中的Python。请您将其修复为与原始代码相同的代码(我想我看到了您试图粘贴的代码,但是如果您没有正确复制缩进,调试Python会变得更加困难)?@NeilSlater fixed indentation.@KalpeshKrishna我将其缩减为两层。我的准确度是9433441.0。如何在代码中使用xavier glorot初始化?大多数例子包括明确设置重量。非常感谢尼尔!然而,仍然存在一些问题。用详细信息更新问题。@VineetKaushik:如果问题不同,请不要更改。例如,不要添加我建议的修复,并询问为什么它现在不起作用。因为这使我的答案无效——也就是说,我所有的工作,以及对其他任何人的利益都是一样的problem@VineetKaushik:如果我的回答帮助您在培训和测试时克服了错误值过大的问题,那么请改为用新问题问新问题。该死。好啊转到一个新问题。@VineetKaushik:谢谢。这样做更好,每个问题分开,而不是一次长对话。你可以在下一个问题中总是把这个问题联系起来,这样如果有人愿意,他们就可以关注整个讨论。
prediction = tf.sigmoid(output)
 predicted_class = tf.greater(prediction,0.5)
 correct = tf.equal(predicted_class, tf.equal(y,1.0))
 accuracy = tf.reduce_mean( tf.cast(correct, 'float') )