TensorFlow运行MNIST样例碰到下载数据的问题

拜拜、爱过 提交于 2019-11-26 03:58:02

Tensorflow安装完成之后,首先使用Softmax实现MNIST。

首先运行以下代码下载MNIST数据集。

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)

提示错误:IOError: [Errno socket error] [Errno 8] _ssl.c:510: EOF occurred in violation of protocol,无法下载数据集。

错误截图:


Socket报错,初步判断是网络协议问题导致下载失败。google一圈没找到解决办法。最后只能手动去下载MINIST数据集,MNIST数据集下载,然后在当前目录下新建MNIST_data文件夹,将四个文件放入,然后执行Softmax代码,问题解决,最后结果为0.9115。

Softmax代码:

# encoding: utf-8 

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

mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
print("完成")

x = tf.placeholder(tf.float32, [None, 784])

# paras
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(tf.float32, [None, 10])

# loss func
cross_entropy = -tf.reduce_sum(y_ * tf.log(y))

train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)

# init
init = tf.initialize_all_variables()

sess = tf.Session()
sess.run(init)

# train
for i in range(1000):
    batch_xs, batch_ys = mnist.train.next_batch(100)
    sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})

correct_prediction = tf.equal(tf.arg_max(y, 1), tf.arg_max(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))

print sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels})



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!