机器学习训练营最近的作业都是使用Keras,所以最近去翻了下文档,这里记录一下学习栗子。(官网有中文文档)
不多BB,直接上代码,注释已经写得很清楚了。
代码语言:javascript复制#!/usr/bin/env python
# -*- coding: utf-8 -*-
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.optimizers import RMSprop
batch_size = 128 # 每个梯度更新的样本数
num_classes = 10 # 类的总数
epochs = 20 # 迭代次数
# load the MNIST dataset
# 初次运行会先下载文件
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train.reshape(60000, 784)
x_test = x_test.reshape(10000, 784)
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 sample')
# 类向量到二元类矩阵的转换
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
# Keras 的核心数据结构是 model,一种组织网络层的方式。最简单的模型是 Sequential 顺序模型,它由多个网络层线性堆叠。
model = Sequential()
# 可以简单地使用 .add() 来堆叠模型:
# activation: 激活函数, relu: 线性修正单元, softmax 激活函数
model.add(Dense(512, activation='relu', input_shape=(784,)))
model.add(Dropout(0.2))
model.add(Dense(512, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(num_classes, activation='softmax'))
model.summary()
# 在完成了模型的构建后, 可以使用 .compile() 来配置学习过程
model.compile(loss='categorical_crossentropy',
optimizer=RMSprop(),
metrics=['accuracy'])
# x_train 和 y_train 是 Numpy 数组 -- 就像在 Scikit-Learn API 中一样。
history = model.fit(x_train, y_train,
batch_size=batch_size,
epochs=epochs,
verbose=1, # 日志显示模式
validation_data=(x_test, y_test)) # 验证数据
# 评估模型性能
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss: ', score[0])
print('Test accuracy: ', score[1])
# 对新的数据生成预测
classes = model.predict(x_test, batch_size=128)
下面是部分输出
代码语言:javascript复制Using TensorFlow backend.
60000 train samples
10000 test sample
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense_1 (Dense) (None, 512) 401920
_________________________________________________________________
dropout_1 (Dropout) (None, 512) 0
_________________________________________________________________
dense_2 (Dense) (None, 512) 262656
_________________________________________________________________
dropout_2 (Dropout) (None, 512) 0
_________________________________________________________________
dense_3 (Dense) (None, 10) 5130
=================================================================
Total params: 669,706
Trainable params: 669,706
Non-trainable params: 0
_________________________________________________________________
Train on 60000 samples, validate on 10000 samples
Epoch 1/20
###
Test loss: 0.11462802259046188
Test accuracy: 0.9826
对数据训练20次得到的结果,准确率高达98.26%,还是挺6的。
但是在运行模型中,这台13年的Mac CPU直接转满,处理器是2.4 GHz Intel Core i5,感觉好吃力,果然深度学习配置要求高啊。
如果代码看不清楚,可以去阅读原文中看。