全文参考 《 基于 python 的深度学习实战》

import numpy as np
from keras.datasets import mnist from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers.convolutional import Conv2D, MaxPooling2D (x_train, y_train), (x_test, y_test) = mnist.load_data() print(x_train[0].shape)
print(y_train) ########################### x 处理 ##################################
# 将训练集合中的数字变成标准的四维张量形式(样本数量、长、宽、深(灰度图 1))
# 并将像素值变成浮点格式
width = 28
height = 28
depth = 1
x_train = x_train.reshape(x_train.shape[0], width, height, depth).astype('float32')
x_test = x_test.reshape(x_test.shape[0], width, height, depth).astype('float32') # 归一化处理,将像素值控制在 0 - 1
x_train /= 255
x_test /= 255
classes = 10 ####################### y 处理 #######################################
# one host 编码
def tran_y(y):
y_ohe = np.zeros(10)
y_ohe[y] = 1
return y_ohe # 标签将 one-hot 编码重排
y_train_ohe = np.array([tran_y(y_train[i]) for i in range(len(y_train))])
y_test_ohe = np.array([tran_y(y_train[i]) for i in range(len(y_test))]) ###################### 搭建卷积神经网络 ###############################
model = Sequential()
# 添加卷积层,构造 64 个过滤器,过滤器范围 3x3x1, 过滤器步长为 1, 图像四周补一圈 0, 并用 relu 非线性变换
model.add(Conv2D(filters=64, kernel_size=(3,3), strides=(1,1), padding='same', input_shape=(width, height, 1), activation='relu'))
# 添加 Max_Pooling , 2 x 2 取最大值
model.add(MaxPooling2D(pool_size=(2, 2)))
# 设立 Dropout , 将概率设为 0.5
model.add(Dropout(0.5)) #重复构造, 搭建神经网络
model.add(Conv2D(128, kernel_size=(3, 3), strides=(1,1), padding='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.5))
model.add(Conv2D(256, kernel_size=(3,3), strides=(1, 1), padding='same', activation='relu'))
model.add((MaxPooling2D(pool_size=(2, 2))))
model.add(Dropout(0.5)) # 将当前节点展平, 构造全连神经网络
model.add(Flatten()) # 构造全连接神经网络
model.add(Dense(128, activation='relu'))
model.add(Dense(64, activation='relu'))
model.add(Dense(32, activation='reul'))
model.add(Dense(classes, activation='softmax')) ################################ 编译模型 ##########################
# 一般,分类问题的损失函数才有交叉熵 (Cross Entropy)
model.compile(loss='categorical_crossentropy', optimizer='adagrad', metrics=['accuracy']) ######################### 训练模型 ################################
model.fit(x_train, y_train_ohe, validation_data=(x_test, y_test_ohe), epochs=20, batch_size=128) ######################## 评价模型 ################################
scores = model.evaluate(x_test, y_test_ohe, verbose=0) ######################## 保持模型与权重 ################################
# 保持整个模型(包括结构、权重)
model.save("mnist_model.h5")

最新文章

  1. jQuery弹出美女大图片
  2. mysql下存储文件问题
  3. ThreadLocal实现方式&使用介绍—无锁化线程封闭
  4. c中的基本运算
  5. 02Spring_Ioc和DI介绍
  6. Web性能API——帮你分析Web前端性能
  7. Careercup - Facebook面试题 - 5412018236424192
  8. (原)Struts 相关资源下载
  9. WebService之Axis2
  10. ecshop网站建设手机版wap版出现lib.debug.php on line 303
  11. django获取ajax的post复杂对象
  12. 【Tomcat】重新打war包
  13. Java的二分搜索树
  14. 高斯消元(Gauss消元)
  15. bzoj2870最长道路tree——边分治
  16. Xcode dSYM 文件
  17. python3中pymysql模块安装及连接数据库(同‘python安装HTMLTestRunner’)
  18. kettle的资源库
  19. 金蝶K3物料选择问题(感觉Ctrl被按住了一样)
  20. <编程之美>经典面试题:求二叉树节点的最大距离(我的解法,最容易理解的版本?)

热门文章

  1. Apache struts2远程命令执行_CVE-2017-9805(S2-052)漏洞复现
  2. 赛前集训的第二个小总结(OI生涯盛极必衰orNOIP前最后试炼?)+关于学OI目的的思考
  3. python通过TimedRotatingFileHandler按时间切割日志
  4. .NET多线程之线程安全,Lock(锁)、Monitor(同步访问)、LazyInitializer(延迟初始化)、Interlocked(原子操作)、static(静态)构造函数、volatile、
  5. 字符串如何实现反转?python实现
  6. P4071 [SDOI2016]排列计数 题解
  7. vue-cli3.x中使用axios发送请求,配合webpack中的devServer编写本地mock数据接口(get/post/put/delete)
  8. JedisClient操作redis 单机版和集群版
  9. bat命令闪退问题
  10. React中创建组件的3种方式