【深度学习】基于Keras的手写体识别

from keras import models
from keras import layers
from keras.datasets import mnist

# 搭建网络
network = models.Sequential()
network.add(layers.Dense(512, activation='relu', input_shape=(28*28,)))
network.add(layers.Dense(10,activation='softmax')) # 返回由10个概率值组成的数组

# 编译网络
network.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])

# 加载数据
(train_images, train_labels),(test_images, test_labels) = mnist.load_data()

# 对数据进行预处理
train_images = train_images.reshape(60000, 28*28)
train_images.astype('float32') / 255

test_images = test_images.reshape(10000, 28*28)
test_images = test_images.astype('float32') / 255

# 准备标签 -- One-Hot编码
from keras.utils import to_categorical
train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)

print(train_labels[0]) # array([0., 0., 0., 0., 0., 1., 0., 0., 0., 0.], dtype=float32)

# 开始训练网络:拟合数据
network.fit(train_images, train_labels, epochs=10, batch_size=128)
'''
Epoch 1/10
60000/60000 [==============================] - 2s 39us/step - loss: 5.3171 - acc: 0.6697
Epoch 2/10
60000/60000 [==============================] - 2s 39us/step - loss: 5.2466 - acc: 0.6743
Epoch 3/10
60000/60000 [==============================] - 2s 39us/step - loss: 5.2882 - acc: 0.6716
Epoch 4/10
60000/60000 [==============================] - 2s 38us/step - loss: 5.2737 - acc: 0.6726
Epoch 5/10
60000/60000 [==============================] - 2s 38us/step - loss: 5.2659 - acc: 0.6730
Epoch 6/10
60000/60000 [==============================] - 2s 38us/step - loss: 5.2511 - acc: 0.6740
Epoch 7/10
60000/60000 [==============================] - 2s 38us/step - loss: 5.2200 - acc: 0.6758
Epoch 8/10
60000/60000 [==============================] - 2s 38us/step - loss: 5.2329 - acc: 0.6751
Epoch 9/10
60000/60000 [==============================] - 2s 37us/step - loss: 5.2335 - acc: 0.6750
Epoch 10/10
60000/60000 [==============================] - 2s 37us/step - loss: 5.2414 - acc: 0.6746
'''

案例中训练5轮即可达到98.9%的精度,我实际用起来效果并不是很好,还没看哪里有不同。

训练完后,直接用于预测:

test_loss, test_acc = network.evaluate(test_images, test_labels)
print('test_acc: ', test_acc)
'''
10000/10000 [==============================] - 1s 52us/step
test_acc:  0.6772
'''

重点还是在于回顾Keras的使用,算是一个简单的模板,后续再丰富使用场景。

END.

Code provided by Ruslan Salakhutdinov and Geoff Hinton Permission is granted for anyone to copy, use, modify, or distribute this program and accompanying programs and documents for any purpose, provided this copyright notice is retained and prominently displayed, along with a note saying that the original programs are available from our web page. The programs and documents are distributed without any warranty, express or implied. As the programs were written for research purposes only, they have not been tested to the degree that would be advisable in any important application. All use of these programs is entirely at the user's own risk. How to make it work: 1. Create a separate directory and download all these files into the same directory 2. Download from http://yann.lecun.com/exdb/mnist the following 4 files: o train-images-idx3-ubyte.gz o train-labels-idx1-ubyte.gz o t10k-images-idx3-ubyte.gz o t10k-labels-idx1-ubyte.gz 3. Unzip these 4 files by executing: o gunzip train-images-idx3-ubyte.gz o gunzip train-labels-idx1-ubyte.gz o gunzip t10k-images-idx3-ubyte.gz o gunzip t10k-labels-idx1-ubyte.gz If unzipping with WinZip, make sure the file names have not been changed by Winzip. 4. Download Conjugate Gradient code minimize.m 5. Download Autoencoder_Code.tar which contains 13 files OR download each of the following 13 files separately for training an autoencoder and a classification model: o mnistdeepauto.m Main file for training deep autoencoder o mnistclassify.m Main file for training classification model o converter.m Converts raw MNIST digits into matlab format o rbm.m Training RBM with binary hidden and binary visible units o rbmhidlinear.m Training RBM with Gaussian hidden and binary visible units o backprop.m Backpropagation for fine-tuning an autoencoder o backpropclassify.m Backpropagation for classification using "encoder" network o CG_MNIST.m Conjugate Gradient optimization for fine-tuning an autoencoder o CG_CLASSIFY_INIT.m Co
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值