【问题标题】:Neural net giving wrong and varying answers even with a high accuracy即使精度很高,神经网络也会给出错误和不同的答案
【发布时间】:2025-12-08 09:05:01
【问题描述】:

我为 notMNIST 数据集编写了一个网络,其中包含来自 A-J 的灰度 28*28 字母。

该模型在测试数据集上给出了 89% 的准确率,但是当我从训练数据集中检查单个图像的结果时(甚至不谈论自定义图像)它给出了错误的结果,而且,如果我一遍又一遍地运行相同的代码块,单个输出的结果会有所不同。

我一定是做错了什么,但我才刚刚开始学习深度学习。

我的模型 ->

batch_size = 128
#tensorflow datasets
tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size*image_size))
tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels))
tf_valid_dataset = tf.constant(valid_dataset)
tf_test_dataset = tf.constant(test_dataset)
#weights and biases for layer 1
weights_l1 = tf.Variable(
    tf.truncated_normal([image_size*image_size, 1024])
)
biases_l1 = tf.Variable(
    tf.zeros([1024])
)
#output layer weights and biases
weights = tf.Variable(
    tf.truncated_normal([1024, num_labels])
)
biases = tf.Variable(
    tf.zeros([num_labels])
)

hl1 = tf.matmul(tf_train_dataset, weights_l1) + biases_l1
hl1 = tf.nn.relu(hl1)

logits = tf.matmul(hl1, weights) + biases

loss = tf.reduce_mean(
    tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits)
)


optimizer = tf.train.GradientDescentOptimizer(0.05).minimize(loss)

#validation predictions
v_hl1 = tf.matmul(tf_valid_dataset, weights_l1) + biases_l1
v_hl1 = tf.nn.relu(v_hl1)
v_logits = tf.matmul(v_hl1, weights) + biases
#test predictions
t_hl1 = tf.matmul(tf_test_dataset, weights_l1) + biases_l1
t_hl1 = tf.nn.relu(t_hl1)
t_logits = tf.matmul(t_hl1, weights) + biases

train_prediction = tf.nn.softmax(logits)
valid_prediction = tf.nn.softmax(v_logits)
test_prediction = tf.nn.softmax(t_logits)

训练步骤->

num_steps = 3001

with tf.Session() as session:
    tf.global_variables_initializer().run()
    for step in range(num_steps):
        offset = (step*batch_size) % (train_labels.shape[0]-batch_size)
        feed_dict = {tf_train_dataset: train_dataset[offset: offset+batch_size, :], 
                     tf_train_labels: train_labels[offset: offset+batch_size, :]
                    }
        _, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict)
        if(step%500 == 0):
            print ('minibatch no.', step)
            print ('current loss', l)
            print("Minibatch accuracy: %.1f%%" % accuracy(predictions, train_labels[offset: offset+batch_size, :]))
            print("Validation accuracy: %.1f%%" % accuracy(valid_prediction.eval(), valid_labels))
    print("Test accuracy: %.1f%%" % accuracy(test_prediction.eval(), test_labels))

输出->

小批号0 当前损失 351.531 小批量准确率:7.8% 验证准确率:27.7% 小批号500 当前损失 6.78443 小批量准确率:82.0% 验证准确率:81.9% 小批号1000 当前损失 6.5816 小批量准确率:80.5% 验证准确率:81.9% 小批号1500 电流损失 4.70451 小批量准确率:81.2% 验证准确率:82.4% 小批号2000 当前损失 3.25759 小批量准确率:84.4% 验证准确率:79.1% 小批号2500 当前损失 4.18851 小批量准确率:82.8% 验证准确率:81.6% 小批号3000 当前损失 2.84338 小批量准确率:86.7% 验证准确率:83.0% 测试准确率:89.0%

测试图像->

(假设图像是“F”)

image_file = 'EE.png'
image_data = (ndimage.imread(image_file).astype(float) - 
                    pixel_depth / 2) / pixel_depth
new_image_data = image_data.reshape([1, 784])
new_image_data = tf.convert_to_tensor(new_image_data)
new_image_data = tf.cast(new_image_data, dtype=tf.float32)
answer = tf.matmul(new_image_data, weights_l1) + biases_l1
answer = tf.nn.relu(answer)
answer = tf.matmul(answer, weights) + biases
answer = tf.nn.softmax(answer)
with tf.Session() as sess:
    tf.global_variables_initializer().run()
    print(sess.run(answer))

谢谢

【问题讨论】:

    标签: python neural-network deep-learning


    【解决方案1】:

    使用此代码,您可以将网络重新初始化为随机值,然后尝试求解您的图像输入。

    with tf.Session() as sess:
        tf.global_variables_initializer().run()
        print(sess.run(answer))
    

    以下代码产生您想要的输出:

    logits.eval(feed_dict={tf_train_dataset:answer})
    

    您可以在测试集和验证集上重复使用变量进行计算。您不必使用新的张量。如果要使用不同的变量,则需要访问相同的变量。我强烈建议你按照 tensorflow 主页提供的标准 MNIST 教程来了解底层概念。

    如果您不需要理解这些概念,我可以向您指出 keras (https://keras.io/),它是 tensorflow 的包装器,它隐藏了这种复杂性。

    【讨论】:

    • 谢谢,但我仍然无法运行代码。 feed_dict 不也需要给 tf_train_labels 赋予一些价值吗?
    • 还有,我们为什么要在 logits 上应用 .eval()?它不应该应用于 train_predictions 吗?
    • 因为 logits 描述了学习的函数。通过优化 logits 与标签的交叉熵所获得的损失,可以学习函数并通过评估该函数,您可以获得所需的输出
    • 实际上我的 train_prediction 包含了 softmax 的 logits,所以我必须对其进行评估,但现在我的代码可以正常工作了!谢谢
    • 更新了我的答案以解决您代码中的另一个问题。
    最近更新 更多