文章目录:
一.卷积神经网络原理
1.什么是CNN
2.CNN原理
二.Keras实现CNN
1.代码实现
2.完整代码
3.运行结果
三.总结
https://github.com/eastmountyxz/
AI-for-TensorFlow
https://github.com/eastmountyxz/
AI-for-Keras
学Python近十年,认识了很多大佬和朋友,感恩。作者的本意是帮助更多初学者入门,因此在github开源了所有代码,也在公众号同步更新。深知自己很菜,得拼命努力前行,编程也没有什么捷径,干就对了。希望未来能更透彻学习和撰写文章,也能在读博几年里学会真正的独立科研。同时非常感谢参考文献中的大佬们的文章和分享。
- https://blog.csdn.net/eastmount
import numpy as npfrom keras.datasets import mnistfrom keras.utils import np_utilsfrom keras.models import Sequentialfrom keras.layers import Dense, Activation, Convolution2D, MaxPooling2D, Flattenfrom keras.optimizers import Adam
第三步,载入MNIST数据及预处理。
# 下载MNIST数据# training X shape (60000, 28x28), Y shape (60000, )# test X shape (10000, 28x28), Y shape (10000, )(X_train, y_train), (X_test, y_test) = mnist.load_data()# 数据预处理# 参数-1表示样例的个数 1表示灰度照片(3对应RGB彩色照片) 28*28表示像素长度和宽度X_train = X_train.reshape(-1, 1, 28, 28) / 255 # normalizeX_test = X_test.reshape(-1, 1, 28, 28) / 255 # normalize# 将类向量转化为类矩阵 数字 5 转换为 0 0 0 0 0 1 0 0 0 0 矩阵y_train = np_utils.to_categorical(y_train, num_classes=10)y_test = np_utils.to_categorical(y_test, num_classes=10)
第四步,创建神经网络第一层及池化层。
model = Sequential()model.add(Convolution2D(filters = 32,nb_row = 5,nb_col = 5,border_mode = 'same',input_shape = (1, 28, 28),))model.add(Activation('relu'))model.add(MaxPooling2D(pool_size = (2, 2),strides = (2,2),padding='same',))
model.add(Convolution2D(64, 5, 5, border_mode='same'))model.add(Activation('relu'))model.add(MaxPooling2D(pool_size = (2, 2), border_mode='same'))
model.add(Flatten())model.add(Dense(1024))model.add(Activation('relu'))model.add(Dense(10))model.add(Activation('softmax'))
# 优化器 optimizeradam = Adam(lr=1e-4)# We add metrics to get more results you want to see# 激活神经网络model.compile(optimizer=adam, # 加速神经网络loss='categorical_crossentropy', # 损失函数metrics=['accuracy']) # 计算误差或准确率print('Training')model.fit(X_train, y_train, epochs=1, batch_size=64,) # 训练次数及每批训练大小print('Testing')loss, accuracy = model.evaluate(X_test, y_test)print('\ntest loss: ', loss)print('\ntest accuracy: ', accuracy)
# -*- coding: utf-8 -*-"""Created on Wed Feb 19 19:47:37 2020@author: xiuzhang Eastmount CSDNWuhan Fighting!"""import numpy as npfrom keras.datasets import mnistfrom keras.utils import np_utilsfrom keras.models import Sequentialfrom keras.layers import Dense, Activation, Convolution2D, MaxPooling2D, Flattenfrom keras.optimizers import Adam#---------------------------载入数据及预处理---------------------------# 下载MNIST数据# training X shape (60000, 28x28), Y shape (60000, )# test X shape (10000, 28x28), Y shape (10000, )(X_train, y_train), (X_test, y_test) = mnist.load_data()# 数据预处理# 参数-1表示样例的个数 1表示灰度照片(3对应RGB彩色照片) 28*28表示像素长度和宽度X_train = X_train.reshape(-1, 1, 28, 28) / 255 # normalizeX_test = X_test.reshape(-1, 1, 28, 28) / 255 # normalize# 将类向量转化为类矩阵 数字 5 转换为 0 0 0 0 0 1 0 0 0 0 矩阵y_train = np_utils.to_categorical(y_train, num_classes=10)y_test = np_utils.to_categorical(y_test, num_classes=10)#---------------------------创建第一层神经网络---------------------------# 创建CNNmodel = Sequential()# Conv layer 1 output shape (32, 28, 28)# 第一层利用Convolution2D卷积model.add(Convolution2D(filters = 32, # 32个滤波器nb_row = 5, # 宽度nb_col = 5, # 高度border_mode = 'same', # Padding methodinput_shape = (1, 28, 28), # 输入形状 channels height width))# 增加神经网络激活函数model.add(Activation('relu'))# Pooling layer 1 (max pooling) output shape (32, 14, 14)# 池化层利用MaxPooling2Dmodel.add(MaxPooling2D(pool_size = (2, 2), # 向下取样strides = (2,2), # 取样跳2个padding='same', # Padding method))#---------------------------创建第二层神经网络---------------------------# Conv layer 2 output shape (64, 14, 14)model.add(Convolution2D(64, 5, 5, border_mode='same'))model.add(Activation('relu'))# Pooling layer 2 (max pooling) output shape (64, 7, 7)model.add(MaxPooling2D(pool_size = (2, 2), border_mode='same'))#-----------------------------创建全连接层------------------------------# Fully connected layer 1 input shape (64 * 7 * 7) = (3136), output shape (1024)model.add(Flatten()) # 将三维层拉直model.add(Dense(1024)) # 全连接层model.add(Activation('relu')) # 激励函数# Fully connected layer 2 to shape (10) for 10 classesmodel.add(Dense(10)) # 输出10个单位model.add(Activation('softmax')) # 分类激励函数#--------------------------------训练和预测------------------------------# 优化器 optimizeradam = Adam(lr=1e-4)# We add metrics to get more results you want to see# 激活神经网络model.compile(optimizer=adam, # 加速神经网络loss='categorical_crossentropy', # 损失函数metrics=['accuracy']) # 计算误差或准确率print('Training')model.fit(X_train, y_train, epochs=1, batch_size=64,) # 训练次数及每批训练大小print('Testing')loss, accuracy = model.evaluate(X_test, y_test)print('\ntest loss: ', loss)print('\ntest accuracy: ', accuracy)
十八.Keras搭建卷积神经网络及CNN原理详解
天行健,君子以自强不息。
地势坤,君子以厚德载物。