深度学习-keras项目demo实战(mnist_mlp)

背景

继续走读一下keras的demo:mnist_mlp

git地址:https://github.com/keras-team/keras.git
目录:examples

代码诠释

'''Trains a simple deep NN on the MNIST dataset.

Gets to 98.40% test accuracy after 20 epochs
(there is *a lot* of margin for parameter tuning).
2 seconds per epoch on a K520 GPU.
'''

from __future__ import print_function

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 = 5

# the data, split between train and test sets
(x_train, y_train), (x_test, y_test) = mnist.load_data()
# (60000, 28, 28)
# (60000,)
print(x_train.shape)
print(y_train.shape)

# 把数据压平
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')
# 对每个小数进行255划分
x_train /= 255
x_test /= 255
# 60000 train samples
print(x_train.shape[0], 'train samples')
# 10000 test samples
print(x_test.shape[0], 'test samples')

# convert class vectors to binary class matrices
# 把1,2,3这种分类的label变成了one-hot类型
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)

print(y_train.shape)
print(y_test.shape)

# 创建一个神经网络
model = Sequential()
# 添加全连接
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'))
# 添加训练过程打印
#   128/60000 [..............................] - ETA: 2s - loss: 0.1832 - acc: 0.9375
#  1536/60000 [..............................] - ETA: 2s - loss: 0.1243 - acc: 0.9655
#  2816/60000 [>.............................] - ETA: 2s - loss: 0.1173 - acc: 0.9656
#  4096/60000 [=>............................] - ETA: 2s - loss: 0.1165 - acc: 0.9653
#  5376/60000 [=>............................] - ETA: 2s - loss: 0.1146 - acc: 0.9656
#  6656/60000 [==>...........................] - ETA: 2s - loss: 0.1203 - acc: 0.9648
#  7936/60000 [==>...........................] - ETA: 2s - loss: 0.1152 - acc: 0.9665
#  9216/60000 [===>..........................] - ETA: 2s - loss: 0.1140 - acc: 0.9671
model.summary()
# 形成网络(同时添加优化器)
model.compile(loss='categorical_crossentropy',
              optimizer=RMSprop(),
              metrics=['accuracy'])

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)
# Test loss: 0.10618342893495278
# Test accuracy: 0.9835
print('Test loss:', score[0])
print('Test accuracy:', score[1])
# {'val_loss': [0.09885338546447456, 0.08402335036471487, 0.08479513939674943, 0.0732438673723489, 0.07142173023019859],
# 'val_acc': [0.9682, 0.9746, 0.9774, 0.9807, 0.9814],
# 'loss': [0.2471682965596517, 0.10111491056879361, 0.07347997357050577, 0.058548206921915216, 0.051447893174240984],
# 'acc': [0.9242833333651225, 0.9690833333333333, 0.9776666666348776, 0.9825333333333334, 0.9850166666348775]}
print(history.history)

评价

keras写神经网络方便很多,同时需要多做各种练习,才能更深入的熟悉和理解它的API

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值