Я не получаю вывод по проблеме логистической регрессии. Я использовал набор данных MNIST, чтобы предсказать цифру числа,
Я использовал оптимизатор Адама, он не дает желаемой точности. Модель Снижает стоимость, но не дает точности хорошо.
Мой код выглядит следующим образом.
# In[1]:
import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
get_ipython().run_line_magic('matplotlib', 'inline')
# In[2]:
train_x = mnist.train.images
train_y = mnist.train.labels
X = tf.placeholder(shape=[None,784],dtype=tf.float32,name="X")
Y = tf.placeholder(shape=[None,10],dtype=tf.float32,name="Y")
# In[3]:
#hyperparameters
training_epoches = 25
batch_size = 1000
total_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.005403
cost after epoch 2 : 0.002935
cost after epoch 3 : 0.001866
cost after epoch 4 : 0.001245
cost after epoch 5 : 0.000877
cost after epoch 6 : 0.000652
cost after epoch 7 : 0.000507
cost after epoch 8 : 0.000407
cost after epoch 9 : 0.000334
cost after epoch 10 : 0.000279
cost after epoch 11 : 0.000237
cost after epoch 12 : 0.000204
cost after epoch 13 : 0.000178
cost after epoch 14 : 0.000156
cost after epoch 15 : 0.000138
cost after epoch 16 : 0.000123
cost after epoch 17 : 0.000111
cost after epoch 18 : 0.000100
cost after epoch 19 : 0.000091
cost after epoch 20 : 0.000083
cost after epoch 21 : 0.000076
cost after epoch 22 : 0.000070
cost after epoch 23 : 0.000065
cost after epoch 24 : 0.000060
cost after epoch 25 : 0.000056
Accuracy: 0.1859
Точность 0,1859. Что не ожидается