python - 为什么这个神经网络的损失总是 0.0 而准确率总是 1.0?

标签 python numpy tensorflow

我正在尝试在大约 80 个条目的数据集上训练基本的前馈神经网络(主要作为概念证明,我知道我的数据集太小)。我的代码基于 the MNIST dataset example 。我选择的批量大小为 10,并通过 8 个步骤运行它:

learning_rate = 0.01
num_steps = 8
batch_size = 10
display_step = 1

num_input = 16
n_hidden_1 = 8
n_hidden_2 = 8
num_classes = 1

X = tf.placeholder("float", [None, num_input])
Y = tf.placeholder("float", [None, num_classes])

weights = {
    'h1': tf.Variable(tf.random_normal([num_input, n_hidden_1])),
    'h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2])),
    'out': tf.Variable(tf.random_normal([n_hidden_2, num_classes]))
}
biases = {
    'b1': tf.Variable(tf.random_normal([n_hidden_1])),
    'b2': tf.Variable(tf.random_normal([n_hidden_2])),
    'out': tf.Variable(tf.random_normal([num_classes]))
}

layer_1 = tf.add(tf.matmul(X, weights['h1']), biases['b1'])
layer_2 = tf.add(tf.matmul(layer_1, weights['h2']), biases['b2'])
logits = tf.matmul(layer_2, weights['out']) + biases['out']

prediction = tf.nn.softmax(logits)
loss_op = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=Y))
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
train_op = optimizer.minimize(loss_op)
correct_pred = tf.equal(tf.argmax(prediction, 1), tf.argmax(Y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
init = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)
    for step in range(0, num_steps):
        batch_x, batch_y = manager.import_data()

        batch_x = batch_x[step * batch_size:(step + 1) * batch_size]
        batch_y = batch_y[step * batch_size:(step + 1) * batch_size]

        batch_x = np.reshape(batch_x, (batch_size, num_input))
        batch_y = np.reshape(batch_y, (batch_size, num_classes))
        sess.run(train_op, feed_dict={X: batch_x, Y: batch_y})
        if step % display_step == 0 or step == 1:
            loss, acc = sess.run([loss_op, accuracy], feed_dict={X: batch_x, Y: batch_y})
            print("Step " + str(step) + ", Minibatch Loss= " + \
                  "{:.4f}".format(loss) + ", Training Accuracy= " + \
                  "{:.3f}".format(acc))

manager.import_data() 返回 numpy 数组的列表。我知道我应该随机选择批处理,并且我最终会实现这一点 - 但是,输出是:

Step 0, Minibatch Loss= 0.0000, Training Accuracy= 1.000
Step 1, Minibatch Loss= 0.0000, Training Accuracy= 1.000
Step 2, Minibatch Loss= 0.0000, Training Accuracy= 1.000
Step 3, Minibatch Loss= 0.0000, Training Accuracy= 1.000
Step 4, Minibatch Loss= 0.0000, Training Accuracy= 1.000
Step 5, Minibatch Loss= 0.0000, Training Accuracy= 1.000
Step 6, Minibatch Loss= 0.0000, Training Accuracy= 1.000
Step 7, Minibatch Loss= 0.0000, Training Accuracy= 1.000

显然情况不应该是这样。我究竟做错了什么?

最佳答案

我猜想在您的训练集中,所有项目都具有相同的标签(例如 0)。

处理神经网络时最好的做法是准备 3 个不同的集合 - 训练、验证和测试,并且类之间的分布大致相同。 train用于训练时,val用于每次迭代结束时保存或忽略模型。测试类似于对模型的现实检查,您不应根据测试分数调整参数。

关于python - 为什么这个神经网络的损失总是 0.0 而准确率总是 1.0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47429267/

相关文章:

python - 如何使用文件列表作为带有 Tensorflow 的 Sagemaker 的训练集?

python - 文本摘要 : failed with exit code 127//windows 10//pdftotext

python - PyQt4 : Connect splitter

Python 和 mechanize 登录脚本

python - 类型错误 : string indices must be integers, 为什么?

python - <lambda>() 接受 1 个位置参数,但给出了 2 个

python - 在 python 中读取 v 7.3 mat 文件

python - 尝试从 Pycharm 安装 sklearn 时出错 | arrayobject.h 不能是绝对的

tensorflow - 添加自定义指标 Keras 子类化 API

python - 拟合 Keras L1 模型