为什么这个神经网络的损失始终为0.0,精度始终为1.0?

时间:2017-11-22 07:45:08

标签: 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

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

1 个答案:

答案 0 :(得分:1)

我猜你的训练集中所有项目都有相同的标签(例如0)。

处理神经网络时的最佳行动方案是准备3种不同的集合,训练和测试,并在类之间进行大致相同的分配。在训练时使用训练,在每次迭代结束时使用val来保存或忽略模型。测试类似于模型的现实检查,您不应该根据测试分数来调整参数。