资源简介
利用基于tensorflow2的keras框架,搭建CNN卷积神经网络模型,对手写数字识别数据集mnist进行分类,网络规模小,训练精度高。网络包括三个卷积层,两个池化层和全连接层,在测试集上实现了99%左右的识别率。
代码片段和文件信息
from keras.datasets import mnist
from keras.layers import Dense Conv2D MaxPooling2D Flatten
from keras.models import Sequential
from keras.utils import to_categorical
“““数据准备:训练集、验证集、测试集“““
(x_train y_train) (x_test y_test) = mnist.load_data()
x_train = x_train.reshape((-1 28 28 1))
x_train = x_train.astype(‘float32‘)/255
y_train = to_categorical(y_train)
x_test = x_test.reshape((-1 28 28 1))
x_test = x_test.astype(‘float32‘)/255
y_test = to_categorical(y_test)
## 训练集的前10000个样本划分为验证集
x_val = x_train[:10000]
y_val = y_train[:10000]
partial_x_train = x_train[10000:]
partial_y_train = y_train[10000:]
“““网络设计“““
network = Sequential()
network.add(Conv2D(32 (3 3) padding=‘same‘ activation=‘relu‘ input
评论
共有 条评论