< >
Home » Turbot-DL入门教程 » Turbot-DL入门教程篇-TensorFlow应用-MNIST例程

Turbot-DL入门教程篇-TensorFlow应用-MNIST例程

说明:

  • 介绍如何使用tensorflow实现MNIST例程

环境:

  • Python 3.5.2

步骤:

  • 下载MNIST数据集:http://yann.lecun.com/exdb/mnist/

    t10k-images-idx3-ubyte.gz
    t10k-labels-idx1-ubyte.gz
    train-images-idx3-ubyte.gz
    train-labels-idx1-ubyte.gz

  • 把下载的数据集放到文件夹MNIST_data中

  • 新建文件

$ vim tensorflow_mnist.py
  • 内容如下
#!/usr/bin/env python3  
# -*- coding: utf-8 -*-  

import input_data  
import tensorflow as tf  

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

x = tf.placeholder(tf.float32,[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("float", [None,10])  

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

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

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

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.argmax(y,1),tf.argmax(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}) )  
  • 执行
pythton3 tensorflow_mnist.py
  • 结果如下
Extrackting MNIST_data/train-images-idx3-ubyte.gz
Extrackting MNIST_data/train-labels-idx1-ubyte.gz
Extrackting MNIST_data/t10k-images-idx3-ubyte.gz
Extrackting MNIST_data/t10k-labels-idx1-ubyte.gz
I tensorflow/core/platform/cpu_feature_guard.cc:137 Your CPU supports instructions that this Tensorflow binary was not compiled to use: SSE4.
I SSE4.2 AVX AVX2 FMA
0.9179    
  • 最终结果应该在91%左右

纠错,疑问,交流: 请进入讨论区点击加入Q群

获取最新文章: 扫一扫右上角的二维码加入“创客智造”公众号


标签: turbot-dl入门教程篇