机器学习入门--MNIST(一)

最近在看机器学习TensorFlow,就像其他任何一门语言(当然机器学习不仅仅是语言)都有一个"hello world",可以说MNIST是机器学习的"hello world"。

极客学院有TensorFlow的官方文档(中文版),里面对MNIST做了详细的介绍,比如模型的建立、原理等等(网址:http://wiki.jikexueyuan.com/project/tensorflow-zh/tutorials/mnist_beginners.html)。


我在根据这个文档做成Demo的时候,遇到了一些坑,特整理记录一下,为后面掉坑里同仁提供一些参考。


其实,总结一下,MNIST Demo做成需要如下几个步骤:


1.MINST数据集下载

下载网址: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)


我上传了下载好的压缩包,地址:http://download.csdn.net/download/sarsscofy/10130439

不知道为啥资源分不能设置成0 T_T,如果可以上官网还是去官网下吧.


2.下载自动安装和下载MINST数据集的Python代码,并存储成文件

1)网址:https://raw.githubusercontent.com/tensorflow/tensorflow/master/tensorflow/examples/tutorials/mnist/input_data.py

具体Python代码如下:

# Copyright 2015 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================

"""Functions for downloading and reading MNIST data."""
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

2)新建一个文件,直接拷贝代码上述路径下打开的Python代码,并存储成input_data.py,注意存储的格式为utf-8


3.由于后续文件中还需要引用读取下载的文件和代码,所以上述内容最好放在一个目录下(即可以看成是一个Module),并分类。

下图是我的结构:


其中:

MNIST_data文件夹 存放的是第一步中下载的四个MNIST数据集文件。

__init__.py 是空文件。

input_data.py 是自动安装和下载MINST数据集的Python代码

mnist_softmax1.py 是主程序。

具体的下面说。


4.新建文件,我命名为mnist_softmax1.py,即MNIST用Softmax 回归来进行训练的代码,具体如下:

#!/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不是一个特定的值,而是一个占位符placeholder,我们在TensorFlow运行计算时输入这个值。
#我们希望能够输入任意数量的MNIST图像,每一张图展平成784维的向量。
#我们用2维的浮点数张量来表示这些图,这个张量的形状是[None,784 ]。
#(这里的None表示此张量的第一个维度可以是任何长度的。)
x = tf.placeholder(tf.float32, [None, 784])
#权重值,初始值全为0
W = tf.Variable(tf.zeros([784,10]))
#偏置量,初始值全为0
b = tf.Variable(tf.zeros([10]))

#建立模型,y是匹配的概率
#tf.matmul(x,W)表示x乘以W
#y是预测,y_是实际
y = tf.nn.softmax(tf.matmul(x,W) + b)

#为计算交叉熵,添加的placeholder
y_ = tf.placeholder("float", [None,10])

#交叉熵
cross_entropy = -tf.reduce_sum(y_*tf.log(y))
#用梯度下降算法(gradient descent algorithm)以0.01的学习速率最小化交叉熵
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)

#初始化我们创建的变量
init = tf.global_variables_initializer()

#在Session里面启动模型
sess = tf.Session()
sess.run(init)

#训练模型
#循环的每个步骤中,都会随机抓取训练数据中的100个批处理数据点,然后用这些数据点作为参数替换之前的占位符来运行train_step
#即:使用的随机梯度下降训练方法
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})

#-------------------模型评估----------------------
#判断预测标签和实际标签是否匹配 
#tf.argmax 找出某个tensor对象在某一维上的其数据最大值所在的索引值
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}))

我是根据官方文档的思路一行代码一行代码拷贝的,建议新学的XDJM也这样,可以理解更深刻些。


需要注意的是:

1)读取MNIST数据集相对路径要跟自己实际定义的存放路径一致:

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

”MNIST_data“是存放MNIST数据集的文件夹名称



2)初始化方法不同,我用的TesorFlow是0.12版本,官方文档中的方法”tf.initialize_all_variables()“已废弃,换成”tf.global_variables_initializer()“。


5.cmd打开命令行窗口,进入demo路径,执行主程序即可看到结果。

当然这个结果不是既定的,多运行几次得到的结果可能不一致:



6.FAQ

1)错误 UnicodeDecodeError: 'utf-8' codec can'tdecode byte 0xb2 in position 168: invalidstart byte

具体如下:

Traceback (most recent call last):
  File "mnist_softmax1.py", line 4, in <module>
  File "E:\CInfos\tensortflow\testcode\mnist\input_data.py", line 28, in <module>
  
  import tensorflow as tf
.......
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb2 in position 168: invali
d start byte
解决方法:

input_data.py 、mnist_softmax1.py 存储格式为utf-8即可。


2)错误 InternalError (see above for traceback): Blas GEMM launch failed : a.shape=(100, 784), b.shape=(784, 10), m=100, n=10, k=784

具体如下:

Caused by op 'MatMul', defined at:
  File "./mnist_softmax1.py", line 22, in <module>
    y = tf.nn.softmax(tf.matmul(x,W) + b)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\ops\math_ops.py", line 2014, in matmul
    a, b, transpose_a=transpose_a, transpose_b=transpose_b, name=name)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\ops\gen_math_ops.py", line 4567, in mat_mul
    name=name)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 787, in _apply_op_helper
    op_def=op_def)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\ops.py", line 3414, in create_op
    op_def=op_def)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\ops.py", line 1740, in __init__
    self._traceback = self._graph._extract_stack()  # pylint: disable=protected-access

InternalError (see above for traceback): Blas GEMM launch failed : a.shape=(100, 784), b.shape=(784, 10), m=100, n=10, k=784
         [[Node: MatMul = MatMul[T=DT_FLOAT, transpose_a=false, transpose_b=false, _device="/job:localhost/replica:0/task:0/device:GPU:0"](_arg_Placeholder_0_0/_3, Variable/read)]]

解决方法:

这种问题多现于GPU版本,一般情况是由于同时打开了多个Session,没有及时关闭,导致系统资源和GPU的占用资源没有释放。 查看是否在别的窗口打开了其他的Session没有关闭,关闭后重新运行即可。

我的情况是在一个cmd窗口(GPU版本)中直接输入tf.Session()后,忘记了,又另起一个cmd窗口运行文件出错。

如果不是上述问题,网上还有一种解决方案,大家可以参考下(https://www.aliyun.com/jiaocheng/523037.html):

如果你是使用 GPU 版 TensorFlow 的话,并且你想在显卡高占用率的情况下(比如玩游戏)训练模型,那你要注意在初始化 Session 的时候为其分配固定数量的显存,否则可能会在开始训练的时候直接报错退出: 

这时你需要用下面的方法创建 Session: 

gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.333)
sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) 

(其他的想起来再追加)



7.参考资料

http://wiki.jikexueyuan.com/project/tensorflow-zh/tutorials/mnist_beginners.html

http://blog.csdn.net/willduan1/article/details/52024254


本节为MNIST入门级模型训练,本模型可以继续优化,具体可参考“机器学习入门---MNIST(二)”。

-----转载请说明出处,谢谢~



  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值