在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

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

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