当前位置:首页 » C语言&C++

TensorFlow MNIST(手写识别 softmax)实例运行

2017-12-15 09:20 本站整理 浏览(18)

TensorFlow MNIST(手写识别 softmax)实例运行

首先要有编译环境,并且已经正确的编译安装,关于环境配置参考:http://www.cnblogs.com/dyufei/p/8027517.html

一、MNIST 运行

1)首先下载训练数据

http://yann.lecun.com/exdb/mnist/ 将四个包都下载下来,在下面代码的运行目录下创建MNIST_data目录,将四个包放进去

train-images-idx3-ubyte.gz: training set images (9912422 bytes)
train-labels-idx1-ubyte.gz: training set labels (28881 bytes)
t10k-images-idx3-ubyte.gz: test set images (1648877 bytes)
t10k-labels-idx1-ubyte.gz: test set labels (4542 bytes)

当然也可以不下载,前提是运行TensorFlow的服务器可以正常访问下载目录,如果出问题参照 【问题1)】解决)

2) MNIST 代码

A: 比较旧的版本(官方教程里面的)

https://tensorflow.google.cn/get_started/mnist/beginners
中文:http://www.tensorfly.cn/tfdoc/tutorials/mnist_beginners.html

完整代码如下:mnist.py

import input_data
import  tensorflow as tf
FLAGS = None
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)

x = tf.placeholder("float",[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_entroy = -tf.reduce_sum(y_ * tf.log(y))

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

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

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})

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})

input_data.py

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import gzip
import os
import tempfile

import numpy
from six.moves import urllib
from six.moves import xrange  # pylint: disable=redefined-builtin
import tensorflow as tf
from tensorflow.contrib.learn.python.learn.datasets.mnist import read_data_sets

运行

python mnist.py

2) 新版本mnist_softmax.py

input_data.py 文件内容相同,mnist_softmax.py文件不同
mnist_softmax.py 文件目录:

tensorflow\tensorflow\examples\tutorials\mnist\mnist_softmax.py

数据路径不同,将训练数据copy过去:

cp MNIST_data/*.gz /tmp/tensorflow/mnist/input_data/

运行:

python mnist_softmax.py

二、遇到问题解决方法

1)IOError: [Errno socket error] [Errno 101] Network is unreachable

A: 手动下载:
http://yann.lecun.com/exdb/mnist/
下面的四个包

train-images-idx3-ubyte.gz: training set images (9912422 bytes)
train-labels-idx1-ubyte.gz: training set labels (28881 bytes)
t10k-images-idx3-ubyte.gz: test set images (1648877 bytes)
t10k-labels-idx1-ubyte.gz: test set labels (4542 bytes)

B: 将下载到的包拷贝到如下目录(注意新版本已经不是之前的MNIST_data 目录了)

cp *.gz /tmp/tensorflow/mnist/input_data/

2) 2017-12-14 16:19:59.347498: I tensorflow/core/platform/cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX0.9166

A: mnist_softmax.py 中加入如下代码:

在 import sys 后加入如下代码

import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
import tensorflow as tf

如果出现如下警告: 'TF_CPP_MIN_LOG_LEVEL' represents the Tensorflow environment variable responsible for logging.
则 在ubuntu 上执行如下:

export TF_CPP_MIN_LOG_LEVEL=2

B: 彻底解决此问题

(1)卸载已安装的tensorflow

sudo pip uninstall tensorflow

(2) 用下面的命令重新编译 tensorflow
GPU & CPU

bazel build -c opt --copt=-march=native --copt=-mfpmath=both --config=cuda -k //tensorflow/tools/pip_package:build_pip_package

仅CPU

bazel build -c opt --copt=-march=native --copt=-mfpmath=both -k //tensorflow/tools/pip_package:build_pip_package

(3)生成安装包

bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg

(4) 安装

sudo -H pip install /tmp/tensorflow_pkg/tensorflow-1.4.0-cp27-cp27mu-linux_x86_64.whl

参考:
https://stackoverflow.com/questions/42463594/cpu-instructions-not-compiled-with-tensorflow

3) 安装时出现警告

安装命令:

sudo pip install /tmp/tensorflow_pkg/tensorflow-1.4.0-cp27-cp27mu-linux_x86_64.whl

警告:

The directory '/home/dyufei/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.

用sudo -H 命令安装:

sudo -H pip install /tmp/tensorflow_pkg/tensorflow-1.4.0-cp27-cp27mu-linux_x86_64.whl

参考:https://stackoverflow.com/questions/33437798/osx-10-11-1-cant-install-gevent

Written with StackEdit.