求助 !! 新手入门,MNIST数据下载出了问题,又怎么用“one_hot”怎么处理label呢?
发布于 21天前 作者 dczlearner 来自问答

欢迎来到AI论坛

mnist = input_data.read_data_sets("MNIST_data_bak/", one_hot=True)

结果:

Instructions for updating:

Please use alternatives such as official/mnist/dataset.py from tensorflow/models.

下面是写的代码:

from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf

mnist = input_data.read_data_sets("MNIST_data_bak/", one_hot=True)

x = tf.placeholder(dtype=tf.float32, shape=(None, 784))

W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
y = tf.nn.softmax(tf.matmul(x, W) + b)

y_ = tf.placeholder(dtype=tf.float32, shape=(None, 10))

cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)

correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))

accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
# 初始化变量
sess = tf.InteractiveSession()
tf.global_variables_initializer().run()
for _ in range(1000):
    batch_xs, batch_ys = mnist.train.next_batch(100)
    sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
    print("TrainSet batch acc : %s " % accuracy.eval({x: batch_xs, y_: batch_ys}))

    print("TestSet acc : %s" % accuracy.eval({x: mnist.test.images, y_: mnist.test.labels}))

回到顶部