tensorflow2.0教程与操作示例( 三 )

CNN基础知识

  • 卷积神经网络(Convolutional Neural Network, CNN)是一种结构类似于人类或动物的 视觉系统 的人工神经网络 , 包含一个或多个卷积层(Convolutional Layer)、池化层(Pooling Layer)和全连接层(Fully-connected Layer) 。
示例代码# -*- coding:utf-8 -*-# /usr/bin/python'''-------------------------------------------------File Name:CNNDescription :AIM: 卷积神经网络Functions: 1. 卷积层2. 池化层Envs:python == 3.7pip install tensorflow==2.1.0 -iAuthor:errolDate:2020/5/115:15CodeStyle:规范,简洁,易懂,可阅读,可维护,可移植!-------------------------------------------------Change Activity:2020/5/1 : 手写字体识别实现卷积和池化层和全连接层的计算-------------------------------------------------'''import tensorflow as tfimport numpy as npclassMNISTLoader():def __init__(self,):mnist = tf.keras.datasets.mnist(self.train_data,self.train_label),(self.test_data,self.test_label) =mnist.load_data()# MNIST中的图像默认为uint8(0-255的数字) 。 以下代码将其归一化到0-1之间的浮点数 , 并在最后增加一维作为颜色通道self.train_data = http://kandian.youth.cn/index/np.expand_dims(self.train_data.astype(np.float32) / 255.0, axis=-1)# [60000, 28, 28, 1]self.test_data = np.expand_dims(self.test_data.astype(np.float32) / 255.0, axis=-1)# [10000, 28, 28, 1]self.train_label = self.train_label.astype(np.int32)# [60000]self.test_label = self.test_label.astype(np.int32)# [10000]self.num_train_data, self.num_test_data = self.train_data.shape[0], self.test_data.shape[0]def get_batch(self, batch_size):# 从数据集中随机取出batch_size个元素并返回index = np.random.randint(0, np.shape(self.train_data)[0], batch_size)return self.train_data[index, :], self.train_label[index]class CNN(tf.keras.Model):def __init__(self,):super().__init__()# 卷积层self.conv1 = tf.keras.layers.Conv2D(filters=32,# 卷积层神经元数目(卷积核)kernel_size = [5,5],# 感受野大小padding ='same',# padding策略(vaild 或者 same)activation =tf.nn.relu # 激活函数)# 池化层self.pool1 = tf.keras.layers.MaxPool2D(pool_size=[2,2],strides=2)# 卷积层self.conv2 = tf.keras.layers.Conv2D(filters=64,# 卷积层神经元数目(卷积核)kernel_size=[5, 5],# 感受野大小padding='same',# padding策略(vaild 或者 same)activation=tf.nn.relu# 激活函数)# 池化层self.pool2 = tf.keras.layers.MaxPool2D(pool_size=[2,2],strides=2)# 重新reshapself.flatten = tf.keras.layers.Reshape(target_shape=[7*7*64,])self.dense1 = tf.keras.layers.Dense(units=1024,activation=tf.nn.relu)self.dense2 = tf.keras.layers.Dense(units=10)def call(self,inputs):'''重载 , 目的 mode(x)'''x = self.conv1(inputs)# [batch_size, 28, 28, 32]x = self.pool1(x)# [batch_size, 14, 14, 32]x = self.conv2(x)# [batch_size, 14, 14, 64]x = self.pool2(x)# [batch_size, 7, 7, 64]x = self.flatten(x)# [batch_size, 7 * 7 * 64]x = self.dense1(x)# [batch_size, 1024]x = self.dense2(x)# [batch_size, 10]output = tf.nn.softmax(x)return output# 训练模型num_epochs = 5batch_size = 50learning_rate = 0.02model = CNN()data_loader = MNISTLoader()optimizer = tf.keras.optimizers.Adam(learning_rate=learning_rate)num_batches = int(data_loader.num_train_data // batch_size * num_epochs)for batch_index in range(num_batches):X, y = data_loader.get_batch(batch_size)with tf.GradientTape() as tape:y_pred = model(X)loss = tf.keras.losses.sparse_categorical_crossentropy(y_true=y, y_pred=y_pred)loss = tf.reduce_mean(loss)print("batch %d: loss %f" % (batch_index, loss.numpy()))grads = tape.gradient(loss, model.variables)optimizer.apply_gradients(grads_and_vars=zip(grads, model.variables))# 模型的评估sparse_categorical_accuracy = tf.keras.metrics.SparseCategoricalAccuracy()num_batches = int(data_loader.num_test_data // batch_size)for batch_index in range(num_batches):start_index, end_index = batch_index * batch_size, (batch_index + 1) * batch_sizey_pred = model.predict(data_loader.test_data[start_index: end_index])sparse_categorical_accuracy.update_state(y_true=data_loader.test_label[start_index: end_index], y_pred=y_pred)print("test accuracy: %f" % sparse_categorical_accuracy.result())