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

 

Leave a Reply

Your email address will not be published.