TensorFlow MNIST手写数字分类:低训练集准确率的根本原因及修复方案
在使用TensorFlow进行MNIST手写数字分类时,许多开发者会遇到一个难题:即使对训练集和测试集进行了像素归一化,训练集的准确率仍然异常低。本文将深入分析此问题,并结合代码示例提供有效的解决方案。
问题根源在于原始代码中y_pred的计算方式。代码中y_pred = tf.nn.softmax(tf.matmul(X, W) + B)这一行,错误地将softmax函数应用于未经softmax处理的预测结果。tf.nn.softmax_cross_entropy_with_logits函数期望输入的是未经softmax处理的预测值(logits)。原始代码却将softmax后的结果传入该函数,导致交叉熵损失函数计算错误,最终影响模型训练效果,导致训练集准确率极低。
为了解决这个问题,我们需要调整y_pred的计算方式以及准确率的计算方式。正确的做法是在损失函数计算后应用softmax函数获取最终的预测概率,而损失函数计算则使用未经softmax处理的预测值。
修正后的代码如下:
# 导入必要的库import numpy as npimport tensorflow as tfimport matplotlib.pyplot as pltfrom tensorflow.examples.tutorials.mnist import input_dataimport osimport pickle# 超参数设置numClasses = 10inputSize = 784batch_size = 64learning_rate = 0.05# 下载数据集mnist = input_data.read_data_sets('original_data/', one_hot=True)train_img = mnist.train.imagestrain_label = mnist.train.labelstest_img = mnist.test.imagestest_label = mnist.test.labelstrain_img /= 255.0test_img /= 255.0X = tf.compat.v1.placeholder(tf.float32, shape=[None, inputSize])y = tf.compat.v1.placeholder(tf.float32, shape=[None, numClasses])W = tf.Variable(tf.random_normal([inputSize, numClasses], stddev=0.1))B = tf.Variable(tf.constant(0.1), [numClasses])y_pred = tf.matmul(X, W) + B # 修正:移除softmaxloss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=y_pred)) + 0.01 * tf.nn.l2_loss(W)opt = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss)correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(tf.nn.softmax(y_pred), 1)) # 修正:在计算准确率时应用softmaxaccuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))saver = tf.train.Saver()multiclass_parameters = {}# 运行with tf.Session() as sess: sess.run(tf.global_variables_initializer()) # 开始训练 for epoch in range(20): total_batch = int(len(train_img) / batch_size) for batch in range(total_batch): batch_input = train_img[batch * batch_size: (batch + 1) * batch_size] batch_label = train_label[batch * batch_size: (batch + 1) * batch_size] _, trainingLoss = sess.run([opt, loss], feed_dict={X: batch_input, y: batch_label}) train_acc = sess.run(accuracy, feed_dict={X: train_img, y: train_label}) print("Epoch %d Training Accuracy %g" % (epoch + 1, train_acc))
登录后复制
本文来自互联网或AI生成,不代表软件指南立场。本站不负任何法律责任。