在MNIST数据集上进行逻辑回归时未达到预期的准确率

我在进行逻辑回归问题时未能获得输出。我使用了MNIST数据集来预测数字,但我使用了Adam优化器,准确率并未达到预期。虽然模型降低了成本,但准确率表现不佳。

我的代码如下所示。

# In[1]:import tensorflow as tfimport matplotlib.pyplot as pltimport numpy as npimport pandas as pdfrom tensorflow.examples.tutorials.mnist import input_datamnist = input_data.read_data_sets("MNIST_data/", one_hot=True)get_ipython().run_line_magic('matplotlib', 'inline')# In[2]:train_x = mnist.train.imagestrain_y = mnist.train.labelsX = tf.placeholder(shape=[None,784],dtype=tf.float32,name="X")Y = tf.placeholder(shape=[None,10],dtype=tf.float32,name="Y")# In[3]:#hyperparameterstraining_epoches = 25batch_size = 1000total_batches = int(mnist.train.num_examples/batch_size)W = tf.Variable(tf.random_normal([784,10]))b = tf.Variable(tf.random_normal([10]))# In[6]:y_ = tf.nn.sigmoid(tf.matmul(X,W)+b)cost = tf.reduce_mean(-tf.reduce_sum(Y*tf.log(y_), reduction_indices=1))optimizer = tf.train.AdamOptimizer(0.01).minimize(cost)init = tf.global_variables_initializer()# In[7]:with tf.Session() as sess:    sess.run(init)    for epoches in range(training_epoches):        for i in range(total_batches):            xs_batch,ys_batch = mnist.train.next_batch(batch_size)            sess.run(optimizer,feed_dict={X:train_x,Y:train_y})        print("cost after epoch %i : %f"%(epoches+1,sess.run(cost,feed_dict={X:train_x,Y:train_y})))     correct_prediction = tf.equal(tf.argmax(y_, 1), tf.argmax(Y, 1))    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))    print("Accuracy:", accuracy.eval({X: mnist.test.images, Y: mnist.test.labels}))

代码的输出是:

cost after epoch 1 : 0.005403cost after epoch 2 : 0.002935cost after epoch 3 : 0.001866cost after epoch 4 : 0.001245cost after epoch 5 : 0.000877cost after epoch 6 : 0.000652cost after epoch 7 : 0.000507cost after epoch 8 : 0.000407cost after epoch 9 : 0.000334cost after epoch 10 : 0.000279cost after epoch 11 : 0.000237cost after epoch 12 : 0.000204cost after epoch 13 : 0.000178cost after epoch 14 : 0.000156cost after epoch 15 : 0.000138cost after epoch 16 : 0.000123cost after epoch 17 : 0.000111cost after epoch 18 : 0.000100cost after epoch 19 : 0.000091cost after epoch 20 : 0.000083cost after epoch 21 : 0.000076cost after epoch 22 : 0.000070cost after epoch 23 : 0.000065cost after epoch 24 : 0.000060cost after epoch 25 : 0.000056Accuracy: 0.1859

准确率为0.1859,这与预期不符


回答:

您需要使用 – y_ = tf.nn.softmax(tf.matmul(X,W)+b)

而不是:

y_ = tf.nn.sigmoid(tf.matmul(X,W)+b)

因为MNIST数据集具有多类标签(sigmoid函数适用于两类情况)。

您还可能需要在

cost = tf.reduce_mean(-tf.reduce_sum(Y*tf.log(y_), reduction_indices=1))

中添加一个很小的数字,比如 –

cost = tf.reduce_mean(-tf.reduce_sum(Y*tf.log(y_ + 1e-10), reduction_indices=1))

以防成本结果为NaN

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注