# -*- coding: utf-8 -*-
"""
Created on Mon Oct 30 19:44:02 2017
@author: user
"""
from __future__ import print_function
# 导入numpy库, numpy是一个常用的科学计算库,优化矩阵的运算
import numpy as np
np.random.seed(1337)
# 导入mnist数据库, mnist是常用的手写数字库
# 导入顺序模型
from keras.models import Sequential
# 导入全连接层Dense, 激活层Activation 以及 Dropout层
from keras.layers.core import Dense, Dropout, Activation
# 设置batch的大小
batch_size = 100
# 设置类别的个数
nb_classes = 10
# 设置迭代的次数
nb_epoch = 20
'''
下面这一段是加载mnist数据,网上用keras加载mnist数据都是用
(X_train, y_train), (X_test, y_test) = mnist.load_data()
但是我用这条语句老是出错:OSError: [Errno 22] Invalid argument
'''
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
X_train, Y_train = mnist.train.images,mnist.train.labels
X_test, Y_test = mnist.test.images, mnist.test.labels
X_train = X_train.reshape(-1, 28, 28,1).astype('float32')
X_test = X_test.reshape(-1,28, 28,1).astype('float32')
#打印训练数据和测试数据的维度
print(X_train.shape,X_test.shape,Y_train.shape,Y_test.shape)
#修改维度
X_train = X_train.reshape(55000,784)
X_test = X_test.reshape(10000,784)
print(X_train.shape,X_test.shape,Y_train.shape,Y_test.shape)
# keras中的mnist数据集已经被划分成了55,000个训练集,10,000个测试集的形式,按以下格式调用即可
# X_train原本是一个60000*28*28的三维向量,将其转换为60000*784的二维向量
# X_test原本是一个10000*28*28的三维向量,将其转换为10000*784的二维向量
# 将X_train, X_test的数据格式转为float32存储
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
# 归一化
X_train /= 255
X_test /= 255
# 打印出训练集和测试集的信息
print(X_train.shape[0], 'train samples')
print(X_test.shape[0], 'test samples')
#Y_train = np_utils.to_categorical(Y_train, nb_classes)
#Y_test = np_utils.to_categorical(Y_test, nb_classes)
# 建立顺序型模型
model = Sequential()
'''
模型需要知道输入数据的shape,
因此,Sequential的第一层需要接受一个关于输入数据shape的参数,
后面的各个层则可以自动推导出中间数据的shape,
因此不需要为每个层都指定这个参数
'''
# 输入层有784个神经元
# 第一个隐层有512个神经元,激活函数为ReLu,Dropout比例为0.2
model.add(Dense(500, input_shape=(784,)))
model.add(Activation('relu'))
model.add(Dropout(0.2))
# 第二个隐层有512个神经元,激活函数为ReLu,Dropout比例为0.2
model.add(Dense(500))
model.add(Activation('relu'))
model.add(Dropout(0.2))
# 输出层有10个神经元,激活函数为SoftMax,得到分类结果
model.add(Dense(10))
model.add(Activation('softmax'))
# 输出模型的整体信息
# 总共参数数量为784*512+512 + 512*512+512 + 512*10+10 = 669706
model.summary()
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
history = model.fit(X_train, Y_train,
batch_size = 200,
epochs = 20,
verbose = 1,
validation_data = (X_test, Y_test))
score = model.evaluate(X_test, Y_test, verbose=0)
# 输出训练好的模型在测试集上的表现
print('Test score:', score[0])
print('Test accuracy:', score[1])
输出结果:
Using TensorFlow backend.
Extracting MNIST_data/train-images-idx3-ubyte.gz
Extracting MNIST_data/train-labels-idx1-ubyte.gz
Extracting MNIST_data/t10k-images-idx3-ubyte.gz
Extracting MNIST_data/t10k-labels-idx1-ubyte.gz
(55000, 28, 28, 1) (10000, 28, 28, 1) (55000, 10) (10000, 10)
(55000, 784) (10000, 784) (55000, 10) (10000, 10)
55000 train samples
10000 test samples
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense_28 (Dense) (None, 500) 392500
_________________________________________________________________
activation_28 (Activation) (None, 500) 0
_________________________________________________________________
dropout_19 (Dropout) (None, 500) 0
_________________________________________________________________
dense_29 (Dense) (None, 500) 250500
_________________________________________________________________
activation_29 (Activation) (None, 500) 0
_________________________________________________________________
dropout_20 (Dropout) (None, 500) 0
_________________________________________________________________
dense_30 (Dense) (None, 10) 5010
_________________________________________________________________
activation_30 (Activation) (None, 10) 0
=================================================================
Total params: 648,010.0
Trainable params: 648,010.0
Non-trainable params: 0.0
_________________________________________________________________
Train on 55000 samples, validate on 10000 samples
Epoch 1/20
55000/55000 [==============================] - 8s - loss: 0.9839 - acc: 0.7180 - val_loss: 0.4231 - val_acc: 0.8754
Epoch 2/20
55000/55000 [==============================] - 6s - loss: 0.3968 - acc: 0.8811 - val_loss: 0.3209 - val_acc: 0.9050
Epoch 3/20
55000/55000 [==============================] - 7s - loss: 0.3220 - acc: 0.9049 - val_loss: 0.2795 - val_acc: 0.9187
Epoch 4/20
55000/55000 [==============================] - 7s - loss: 0.2745 - acc: 0.9183 - val_loss: 0.2363 - val_acc: 0.9280
Epoch 5/20
55000/55000 [==============================] - 7s - loss: 0.2335 - acc: 0.9303 - val_loss: 0.1984 - val_acc: 0.9410
Epoch 6/20
55000/55000 [==============================] - 7s - loss: 0.2029 - acc: 0.9387 - val_loss: 0.1727 - val_acc: 0.9481
Epoch 7/20
55000/55000 [==============================] - 7s - loss: 0.1781 - acc: 0.9463 - val_loss: 0.1533 - val_acc: 0.9548
Epoch 8/20
55000/55000 [==============================] - 8s - loss: 0.1585 - acc: 0.9529 - val_loss: 0.1375 - val_acc: 0.9585
Epoch 9/20
55000/55000 [==============================] - 7s - loss: 0.1419 - acc: 0.9569 - val_loss: 0.1310 - val_acc: 0.9599
Epoch 10/20
55000/55000 [==============================] - 7s - loss: 0.1278 - acc: 0.9618 - val_loss: 0.1194 - val_acc: 0.9640
Epoch 11/20
55000/55000 [==============================] - 7s - loss: 0.1149 - acc: 0.9658 - val_loss: 0.1100 - val_acc: 0.9663
Epoch 12/20
55000/55000 [==============================] - 8s - loss: 0.1067 - acc: 0.9681 - val_loss: 0.1026 - val_acc: 0.9677
Epoch 13/20
55000/55000 [==============================] - 7s - loss: 0.0987 - acc: 0.9704 - val_loss: 0.0985 - val_acc: 0.9695
Epoch 14/20
55000/55000 [==============================] - 7s - loss: 0.0905 - acc: 0.9719 - val_loss: 0.0942 - val_acc: 0.9715
Epoch 15/20
55000/55000 [==============================] - 7s - loss: 0.0832 - acc: 0.9750 - val_loss: 0.0916 - val_acc: 0.9701
Epoch 16/20
55000/55000 [==============================] - 7s - loss: 0.0786 - acc: 0.9761 - val_loss: 0.0878 - val_acc: 0.9726
Epoch 17/20
55000/55000 [==============================] - 7s - loss: 0.0720 - acc: 0.9781 - val_loss: 0.0862 - val_acc: 0.9715
Epoch 18/20
55000/55000 [==============================] - 7s - loss: 0.0685 - acc: 0.9787 - val_loss: 0.0852 - val_acc: 0.9733
Epoch 19/20
55000/55000 [==============================] - 7s - loss: 0.0625 - acc: 0.9809 - val_loss: 0.0773 - val_acc: 0.9762
Epoch 20/20
55000/55000 [==============================] - 7s - loss: 0.0596 - acc: 0.9812 - val_loss: 0.0761 - val_acc: 0.9771
Test score: 0.0761470827273
Test accuracy: 0.9771