这篇文章主要讲解了“CNN怎么实现数字识别并改变参数”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“CNN怎么实现数字识别并改变参数”吧! 1.网络层级结构概述 Input layer:
这篇文章主要讲解了“CNN怎么实现数字识别并改变参数”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“CNN怎么实现数字识别并改变参数”吧!
1.网络层级结构概述
Input layer: 输入数据为原始训练图像
Conv1:6 个 5 * 5 的卷积核,步长 Stride 为 1
Pooling1:卷积核 size 为 2 * 2,步长 Stride 为 2
Conv2:12 个 5 * 5 的卷积核,步长 Stride 为 1
Pooling2:卷积核 size 为 2 * 2,步长 Stride 为 2
Output layer:输出为 10 维向量
2.实验基本流程
(1)获取训练数据和测试数据
直接使用keras里面的手写数据集
from keras.datasets import mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
(2)定义网络层级结构
代码:
def get_model():
model = Sequential()
model.add(Conv2D(filters=6, kernel_size=(5, 5),strides=1,activation='relu',input_shape=(28, 28, 1)))
model.add(MaxPooling2D(pool_size=(2, 2),strides=2))
model.add(Conv2D(filters=12, kernel_size=(5, 5),strides=1,activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2),strides=2))
model.add(Flatten())
#model.add(Conv2D(filters=120, kernel_size=(5, 5),activation='relu'))
model.add(Dense(120, activation='relu'))
model.add(Dense(84, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))
# 编译模型,采用多分类的损失函数,优化器是Adadelta
model.compile(loss='cateGorical_crossentropy',
optimizer='Adadelta',
metrics=['accuracy'])
return model
(3)交叉验证
直接附上代码
def k_cross(data,target,bsize,epoch,sp):
print("------进行交叉验证------")
ans=0 #交叉验证正确率的和
kf = KFold(n_splits=sp, shuffle = True)
for train, test in kf.split(data):
model.fit(data[train], target[train],
batch_size=bsize,
epochs=epoch,
verbose=0,
validation_data=(data[test], target[test]))
score = model.evaluate(data[test], target[test], verbose=0)
ans+=score[1]
return ans/sp
3完整代码
我这里直接就3折了,太多了运行时间太长。
最后完整代码:
# -*- coding: utf-8 -*-
"""
Created on Tue Dec 10 15:42:27 2019
@author: pff
"""
from __future__ import print_function
import numpy as np
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from sklearn.model_selection import KFold
import matplotlib.pyplot as plt
def getdata():
#提取出训练集和测试集
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
x_train = x_train.reshape(x_train.shape[0], 28, 28, 1)
x_test = x_test.reshape(x_test.shape[0], 28, 28, 1)
# 采用one-hot编码
y_train = keras.utils.to_categorical(y_train, 10)
y_test = keras.utils.to_categorical(y_test, 10)
#将测试集和训练集合并,便于后面交叉验证
data = np.row_stack((x_train,x_test))
target = np.row_stack((y_train,y_test))
return data, target
# 构建模型
def get_model():
model = Sequential()郑州做无痛人流手术费用 Http://www.zzzykdfk.com/
model.add(Conv2D(filters=6, kernel_size=(5, 5),strides=1,activation='relu',input_shape=(28, 28, 1)))
model.add(MaxPooling2D(pool_size=(2, 2),strides=2))
model.add(Conv2D(filters=12, kernel_size=(5, 5),strides=1,activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2),strides=2))
model.add(Flatten())
#model.add(Conv2D(filters=120, kernel_size=(5, 5),activation='relu'))
model.add(Dense(120, activation='relu'))
model.add(Dense(84, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))
# 编译模型,采用多分类的损失函数,用 Adadelta 算法做优化方法
model.compile(loss='categorical_crossentropy',
optimizer='Adadelta',
metrics=['accuracy'])
return model
def kcross(data,target,bsize,epoch,sp):
print("------进行交叉验证------")
ans=0
kf = KFold(n_splits=sp, shuffle = True)
for train, test in kf.split(data):
#print("第{}次开始".fORMat(i+1))
model.fit(data[train], target[train],
batch_size=bsize,
epochs=epoch,
verbose=0,
validation_data=(data[test], target[test]))
score = model.evaluate(data[test], target[test], verbose=0)
ans+=score[1]
return ans/sp
#画结果图
def draw(batch_size,y,epoch):
plt.figure()
plt.rcParams['font.sans-serif']='SimHei'
plt.ylabel('正确率')
plt.xlabel('batch_size')
plt.title('不同参数下卷积神经网络数字识别图')
for i in range(len(y)):
plt.scatter(batch_size, y[i], s=30, c='r', marker='x', linewidths=1)
plt.plot(batch_size,y[i],label="epoch:"+str(epoch[i]))
plt.legend()
plt.show()
if __name__=="__main__":
data,target=getdata()
model=get_model()
'''
设置epoch和baitch_size参数
y:存储每一次的结果
'''
epoch=[1,3,5,7]
size=[50,100,150,200,250]
y=np.zeros([4,5])
for i in range(len(epoch)):
for j in range(len(size)):
print("now:",i,j)
y[i,j]=kcross(data,target,size[j],epoch[i],3)
draw(size,y,epoch)
最后得出运行结果
感谢各位的阅读,以上就是“CNN怎么实现数字识别并改变参数”的内容了,经过本文的学习后,相信大家对CNN怎么实现数字识别并改变参数这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是编程网,小编将为大家推送更多相关知识点的文章,欢迎关注!
--结束END--
本文标题: CNN怎么实现数字识别并改变参数
本文链接: https://lsjlt.com/news/228349.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0