# 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
网友评论