keras 深度学习 mnist示例代码

'''
功能:通过深度学习,让机器学会自动识别手写数字。
源数据来源于:mnist官方库中手写数字的训练及测试数据。

附:
keras中文文档:
http://keras-cn.readthedocs.io/en/latest/for_beginners/concepts/

keras英文文档:
https://keras.io/
'''

#!/usr/bin/env python

import tensorflow as tf
import tensorflow.examples.tutorials.mnist.input_data as input_data
import numpy as np
import keras
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation
from keras.layers import Conv2D, MaxPool2D, Flatten
from keras.optimizers import SGD, Adam
from keras.utils import np_utils
from keras.datasets import mnist

# 定义数据加载函数
def load_data():
    (x_train, y_train), (x_test, y_test) = mnist.load_data()
    
    '''
    x_train.shape=[10000,28,28], x_train[0]=(一个二维数组)
    x_test.shape=[(10000, 28, 28)],x_test[0]=(一个二维数组)
    y_train.shape=[(60000,)],y_train[0]=[5]
    y_test.shape=[(10000,)],y_test[0]=[7]
    说明x_train的每一个元素, 都是一个28*28的数组, 下面我们要将数转换成一维向量
    '''

    number = 10000
    x_train = x_train[0:number]
    y_train = y_train[0:number]

    # 将三维数组转换为二维数组
    x_train = x_train.reshape(number, 28*28)
    x_test = x_test.reshape(x_test.shape[0], 28*28)

    # 将向里中每个元素的值从整形转为浮点
    x_train = x_train.astype('float32')
    x_test = x_test.astype('float32')

    # convert class vectors to binary class matrices
    # 即用one hot encoding方法将输出标签的向量(vector)转化为
    # 只在出现对应标签的那一列为1,其余为0的布尔矩阵, 10个列,有值的一列为1, 其余为0
    # keras.utils.to_categorical(labels, num_classes)
    y_train = np_utils.to_categorical(y_train, 10)
    y_test = np_utils.to_categorical(y_test, 10)

    '''
    x_train = x_train
    x_test = x_test
    '''

    x_train = x_train / 255
    x_test = x_test / 255

    return (x_train, y_train), (x_test, y_test)

'''
x_train.shape => (10000, 784) x_train的形状: 10000个784维的向量
x_train[0] => 第一个向量的值
'''

# 加载训练数据及测试数据
(x_train, y_train), (x_test, y_test) = load_data()
'''
print ('x_train.shape=[%s],x_train[0]=[%s]' %(x_train.shape, x_train[0]))
print ('x_test.shape=[%s],x_test[0]=[%s]'   %(x_test.shape, x_test[0]))
print ('y_train.shape=[%s],y_train[0]=[%s]' %(y_train.shape, y_train[0]))
print ('y_test.shape=[%s],y_test[0]=[%s]'   %(y_test.shape, y_test[0]))
print ('\n\n')
exit()

x_train.shape=[(10000, 784)],x_train[0]=(一个一维数组)
x_test.shape=[(10000, 784)],x_test[0]=(一个一维数组)
y_train.shape=[(10000, 10)],y_train[0]=[[0. 0. 0. 0. 0. 1. 0. 0. 0. 0.]]
y_test.shape=[(10000, 10)],y_test[0]=[[0. 0. 0. 0. 0. 0. 0. 1. 0. 0.]]
'''

# 定义一个序贯模型(Graph模型的一个特殊情况)
# 序贯模型是多个网络层的线性堆叠,训练时数据在网络层之间顺序执行。
# 序贯模型是相对于Graph模型而言。
# Graph模型支持多输入多输出,层与层之间想怎么边就怎么连,但是编译速度慢。
model = Sequential()

# 定义神经网络输入层
model.add(Dense(input_dim=28*28, units=633, activation='relu'))

# 定义神经网络的隐藏层
model.add(Dense(units=633, activation='relu'))
model.add(Dense(units=633, activation='relu'))

# 定义神经网络输出层
model.add(Dense(units=10, activation='softmax'))

# 指定损失函数:loss, 优化器:optimizer, 性能评估指标列表:metrics
model.compile(loss='categorical_crossentropy',optimizer=SGD(lr=0.1),metrics=['accuracy'])

# 开始训练模型
# model.fit(data, labels, batch_size=, epochs=)
model.fit(x_train, y_train, batch_size=1, epochs=1000)

# 打印训练得到最终的准确度
print('\nGet Train Accuracy')
result_train = model.evaluate(x_train,y_train)
print('Train Accuracy:[%f]\n' %(result_train[1]))

# 打印应用到测试数据得到的准确度
print('Get Test Accuracy')
result_test = model.evaluate(x_test,y_test)
print('Test Accuracy:[%f]' %(result_test[1]))

'''
# 使用训练得到的模型,对新的数据进行预测:
classes = model.predict(x_newdata, batch_size=128)
'''

 

MAC下安装TensorFlow

内容直接参考了官网: https://www.tensorflow.org/install/

方法1: 下载编译好的包直接安装

pip install https://storage.googleapis.com/tensorflow/mac/tensorflow-0.5.0-py2-none-any.whl

验证:

$ python

>>> import tensorflow as tf
>>> hello = tf.constant('Hello, TensorFlow!')
>>> sess = tf.Session()
>>> print sess.run(hello)
Hello, TensorFlow!
>>> a = tf.constant(10)
>>> b = tf.constant(32)
>>> print sess.run(a+b)
42
>>>

方法2: 通过Anaconda进行安装

1.先下载并安装Anaconda, 中国大陆可以从: https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/ 下载, 速度很快. 注意选择对应版本, 安装包里的数字对应的就是python的版本号.

2.创建一个 conda 中名字为 tensorflow的运行环境:
$ conda create -n tensorflow pip python=2.7 # or python=3.3, etc.

3.激活conda的tensorflow环境:
$ source activate tensorflow
 (targetDirectory)$ # Your prompt should change
 (逆向操作:source deactivate)

4.使用下列命令在conda里的tensorflow环境中安装tensorflow:
(targetDirectory)$ pip install --ignore-installed --upgrade TF_PYTHON_URL
where TF_PYTHON_URL is the URL of the TensorFlow Python package.

例如, 下述命令将安装一个python 3, 只支持CPU运算的tensorflow版本:(targetDirectory)$ pip install --ignore-installed --upgrade \
https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-1.5.0-py3-none-any.whl

5.验证安装结果:
执行一个tensorflow小程序验证.

6.在conda tenforflow环境下额外安装pandas:
(targetDirectory)$ # pip install pandas

方法3: 下载源码到本地根据本地环境编译安装

环境准备:

Mac OS X 下环境搭建: 
1.从 app store 安装 xcode 
2.安装 Homebrew /usr/bin/ruby -e "$(curl -fsSL \  https://raw.githubusercontent.com/Homebrew/install/master/install)" 
3. 安装Bazel (Google开源的软件构建工具) brew install bazel 验证: bazel version 更新版本: brew upgrade bazel 
4. 安装SWIG 
brew install swig 注意: 你需要安装PCRE, 而不是 PCRE2. 
5. 安装Numpy,six,wheel
sudo pip install numpy six wheel

tenforflow的GPU计算只支持NVIDIA的显卡, 故MAC的机器不用考虑该部分内容.
(MAC)编译过程:

2.1 克隆TensorFlow仓库到本地

$ git clone --recurse-submodules https://github.com/tensorflow/tensorflow 

2.2 进入源码目录
$ cd tensorflow 

2.3 生成环境配置
$ ./configure
  (中文网站没有说到这一步, 但如果不执行的话, 会报找不到numpy)

2.4 编译并安装:

$ bazel build -c opt //tensorflow/tools/pip_package:build_pip_package

2.5 创建 pip 包
$ bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg

2.6 安装pip包
# .whl 文件的实际名字与你所使用的平台有关
$ pip install /tmp/tensorflow_pkg/tensorflow-0.5.0-cp27-none-linux_x86_64.whl

安装遇到的问题:

1. 从源码编译安装, 执行:

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

命令成功完成, 在执行命令的这个shell窗口, 可以以python里import tensorflow, 但是在其它的shell窗口, import tensorflow提示:

$ python

Python 2.7.13 (default, Mar 19 2017, 21:40:17) 

[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin

Type "help", "copyright", "credits" or "license" for more information.

>>> import tensorflow as tf

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

ImportError: No module named tensorflow

观察发现, 能成功import tensorflow的窗口打印的python版本信息为:

$ python
Python 2.7.13 (default, Apr 1 2017, 13:31:12) 
[GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.38)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>>

也就是说虽然看上去python版本都是2.7.13, 其实执行的环境并不一样.

pyenv打印出来的信息对比:

成功的shell窗口:

$ pyenv versions

  system

  2.7.10

* 2.7.13 (set by /Users/xxx/.pyenv/version)

失败的shell窗口: 

$ pyenv versions

* system (set by /Users/xxx/.python-version)

  2.7.10

  2.7.13

查看pyenv help, 发现通过pyenv local 来指定环境信息:

$ pyenv help local

Usage: pyenv local <version>

       pyenv local --unset

Sets the local application-specific Python version by writing the

version name to a file named `.python-version'.

When you run a Python command, pyenv will look for a `.python-version'

file in the current directory and each parent directory. If no such

file is found in the tree, pyenv will use the global Python version

specified with `pyenv global'. A version specified with the

`PYENV_VERSION' environment variable takes precedence over local

and global versions.

<version> should be a string matching a Python version known to pyenv.

The special version string `system' will use your default system Python.

Run `pyenv versions' for a list of available Python versions.

根据帮助内容, 简单粗暴地执行了如下命令:

$ export PYENV_VERSION=2.7.13

强制设置一个环境变量, 让python执行时使用: 

2.7.13 (set by /Users/xxx/.pyenv/version)

接下来再执行import tensorflow就正常了

$ pyenv versions

  system

  2.7.10

* 2.7.13 (set by PYENV_VERSION environment variable)

$ python

Python 2.7.13 (default, Apr  1 2017, 13:31:12) 

[GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.38)] on darwin

Type "help", "copyright", "credits" or "license" for more information.

>>> import tensorflow as tf

>>>