# coding: utf-8
# In[2]:
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
# In[3]:
#载入数据集
mnist = input_data.read_data_sets("MNIST_data",one_hot=True)
#每个批次的大小,每次放入的大小,每次放入 100张图片 以矩阵的方式
batch_size = 100
#计算一共有多少个批次,数量整除大小训练出有多少批次
n_batch = mnist.train.num_examples // batch_size
#定义两个placeholder,None,表示任意值, 一张图片常为784的向量
x = tf.placeholder(tf.float32,[None,784])#, 一张图片常为784的向量
y = tf.placeholder(tf.float32,[None,10])#0-9有十个数字,所以为10
#创建一个简单的神经网络
W = tf.Variable(tf.zeros([784,10]))#输入层
b = tf.Variable(tf.zeros([10]))#输出层十个标签
prediction = tf.nn.softmax(tf.matmul(x,W)+b)
#二次代价函数
loss = tf.reduce_mean(tf.square(y-prediction))
#使用梯度下降法
train_step = tf.train.GradientDescentOptimizer(0.2).minimize(loss)
#初始化变量
init = tf.global_variables_initializer()
#结果存放在一个布尔型列表中
correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1))#argmax返回一维张量中最大的值所在的位置
#求准确率
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
with tf.Session() as sess:
sess.run(init)
for epoch in range(21):
for batch in range(n_batch):
batch_xs,batch_ys = mnist.train.next_batch(batch_size)
sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys})
acc = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels})
print("Iter " + str(epoch) + ",Testing Accuracy " + str(acc))
#输出
Instructions for updating:
Please use alternatives such as official/mnist/dataset.py from tensorflow/models.
WARNING:tensorflow:From C:\Program Files\Anaconda3\lib\site-packages\tensorflow\contrib\learn\python\learn\datasets\mnist.py:260: maybe_download (from tensorflow.contrib.learn.python.learn.datasets.base) is deprecated and will be removed in a future version.
Instructions for updating:
Please write your own downloading logic.
WARNING:tensorflow:From C:\Program Files\Anaconda3\lib\site-packages\tensorflow\contrib\learn\python\learn\datasets\mnist.py:262: extract_images (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
Instructions for updating:
Please use tf.data to implement this functionality.
Extracting MNIST_data\train-images-idx3-ubyte.gz
WARNING:tensorflow:From C:\Program Files\Anaconda3\lib\site-packages\tensorflow\contrib\learn\python\learn\datasets\mnist.py:267: extract_labels (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
Instructions for updating:
Please use tf.data to implement this functionality.
Extracting MNIST_data\train-labels-idx1-ubyte.gz
WARNING:tensorflow:From C:\Program Files\Anaconda3\lib\site-packages\tensorflow\contrib\learn\python\learn\datasets\mnist.py:110: dense_to_one_hot (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
Instructions for updating:
Please use tf.one_hot on tensors.
Extracting MNIST_data\t10k-images-idx3-ubyte.gz
Extracting MNIST_data\t10k-labels-idx1-ubyte.gz
WARNING:tensorflow:From C:\Program Files\Anaconda3\lib\site-packages\tensorflow\contrib\learn\python\learn\datasets\mnist.py:290: DataSet.__init__ (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
Instructions for updating:
Please use alternatives such as official/mnist/dataset.py from tensorflow/models.
2018-07-21 09:44:01.748002: I T:\src\github\tensorflow\tensorflow\core\platform\cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
Iter 0,Testing Accuracy 0.8324
Iter 1,Testing Accuracy 0.8709
Iter 2,Testing Accuracy 0.8816
Iter 3,Testing Accuracy 0.8887
Iter 4,Testing Accuracy 0.8937
Iter 5,Testing Accuracy 0.8972
Iter 6,Testing Accuracy 0.9011
Iter 7,Testing Accuracy 0.902
Iter 8,Testing Accuracy 0.9041
Iter 9,Testing Accuracy 0.9048
Iter 10,Testing Accuracy 0.9066
Iter 11,Testing Accuracy 0.9078
Iter 12,Testing Accuracy 0.9081
Iter 13,Testing Accuracy 0.9095
Iter 14,Testing Accuracy 0.9102
Iter 15,Testing Accuracy 0.9108
Iter 16,Testing Accuracy 0.9117
Iter 17,Testing Accuracy 0.9128
Iter 18,Testing Accuracy 0.9127
Iter 19,Testing Accuracy 0.9141
Iter 20,Testing Accuracy 0.9139
MNIST数据集分类简单版本
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- MNIST手写体数字识别操作笔记 1.MNIST数据集下载地址http://yann.lecun.com/exdb...
- MNIST(Mixed National Institute of Standards and Technolog...
- 使用的模型为LSTM parameters EPOCH = 1BATCH_SIZE = 64TIME_STEP =...