判别器loss为0_使用TensorFlow v2.0构建卷积神经网络

使用TensorFlow v2.0构建卷积神经网络。

这个例子使用低级方法来更好地理解构建卷积神经网络和训练过程背后的所有机制。

CNN 概述

fb4e3495ac5b18d6e41ca8969b457416.png

MNIST 数据集概述

此示例使用手写数字的MNIST数据集。该数据集包含60,000个用于训练的示例和10,000个用于测试的示例。这些数字已经过尺寸标准化并位于图像中心,图像是固定大小(28x28像素),值为0到255。

在此示例中,每个图像将转换为float32并归一化为[0,1]。

d254b388cf2cbd2b18962b2f4ec42776.png

更多信息请查看链接: http://yann.lecun.com/exdb/mnist/

from __future__ import absolute_import, division, print_functionimport tensorflow as tfimport numpy as np# MNIST 数据集参数num_classes = 10 # 所有类别(数字 0-9)# 训练参数learning_rate = 0.001training_steps = 200batch_size = 128display_step = 10# 网络参数conv1_filters = 32 # 第一层卷积层卷积核的数目conv2_filters = 64 # 第二层卷积层卷积核的数目fc1_units = 1024 # 第一层全连接层神经元的数目# 准备MNIST数据from tensorflow.keras.datasets import mnist(x_train, y_train), (x_test, y_test) = mnist.load_data()# 转化为float32x_train, x_test = np.array(x_train, np.float32), np.array(x_test, np.float32)# 将图像值从[0,255]归一化到[0,1]x_train, x_test = x_train / 255., x_test / 255.# 使用tf.data API对数据进行随机排序和批处理train_data = tf.data.Dataset.from_tensor_slices((x_train, y_train))train_data = train_data.repeat().shuffle(5000).batch(batch_size).prefetch(1)# 为简单起见创建一些包装器def conv2d(x, W, b, strides=1): # Conv2D包装器, 带有偏置和relu激活 x = tf.nn.conv2d(x, W, strides=[1, strides, strides, 1], padding='SAME') x = tf.nn.bias_add(x, b) return tf.nn.relu(x)def maxpool2d(x, k=2): # MaxPool2D包装器 return tf.nn.max_pool(x, ksize=[1, k, k, 1], strides=[1, k, k, 1], padding='SAME')# 存储层的权重和偏置# 随机值生成器初始化权重random_normal = tf.initializers.RandomNormal()weights = { # 第一层卷积层: 5 * 5卷积,1个输入, 32个卷积核(MNIST只有一个颜色通道) 'wc1': tf.Variable(random_normal([5, 5, 1, conv1_filters])), # 第二层卷积层: 5 * 5卷积,32个输入, 64个卷积核 'wc2': tf.Variable(random_normal([5, 5, conv1_filters, conv2_filters])), # 全连接层: 7*7*64 个输入, 1024个神经元 'wd1': tf.Variable(random_normal([7*7*64, fc1_units])), # 全连接层输出层: 1024个输入, 10个神经元(所有类别数目) 'out': tf.Variable(random_normal([fc1_units, num_classes]))}biases = { 'bc1': tf.Variable(tf.zeros([conv1_filters])), 'bc2': tf.Variable(tf.zeros([conv2_filters])), 'bd1': tf.Variable(tf.zeros([fc1_units])), 'out': tf.Variable(tf.zeros([num_classes]))}# 创建模型def conv_net(x):  # 输入形状:[-1, 28, 28, 1]。一批28*28*1(灰度)图像 x = tf.reshape(x, [-1, 28, 28, 1]) # 卷积层, 输出形状:[ -1, 28, 28 ,32] conv1 = conv2d(x, weights['wc1'], biases['bc1'])  # 最大池化层(下采样) 输出形状:[ -1, 14, 14, 32] conv1 = maxpool2d(conv1, k=2) # 卷积层, 输出形状:[ -1, 14, 14, 64] conv2 = conv2d(conv1, weights['wc2'], biases['bc2'])  # 最大池化层(下采样) 输出形状:[ -1, 7, 7, 64] conv2 = maxpool2d(conv2, k=2) # 修改conv2的输出以适应完全连接层的输入, 输出形状:[-1, 7*7*64] fc1 = tf.reshape(conv2, [-1, weights['wd1'].get_shape().as_list()[0]])  # 全连接层, 输出形状: [-1, 1024] fc1 = tf.add(tf.matmul(fc1, weights['wd1']), biases['bd1']) # 将ReLU应用于fc1输出以获得非线性 fc1 = tf.nn.relu(fc1) # 全连接层,输出形状 [ -1, 10] out = tf.add(tf.matmul(fc1, weights['out']), biases['out']) # 应用softmax将输出标准化为概率分布 return tf.nn.softmax(out)# 交叉熵损失函数def cross_entropy(y_pred, y_true): # 将标签编码为独热向量 y_true = tf.one_hot(y_true, depth=num_classes) # 将预测值限制在一个范围之内以避免log(0)错误 y_pred = tf.clip_by_value(y_pred, 1e-9, 1.) # 计算交叉熵 return tf.reduce_mean(-tf.reduce_sum(y_true * tf.math.log(y_pred)))# 准确率评估def accuracy(y_pred, y_true): # 预测类是预测向量中最高分的索引(即argmax) correct_prediction = tf.equal(tf.argmax(y_pred, 1), tf.cast(y_true, tf.int64)) return tf.reduce_mean(tf.cast(correct_prediction, tf.float32), axis=-1)# ADAM 优化器optimizer = tf.optimizers.Adam(learning_rate)# 优化过程def run_optimization(x, y): # 将计算封装在GradientTape中以实现自动微分 with tf.GradientTape() as g: pred = conv_net(x) loss = cross_entropy(pred, y)  # 要更新的变量,即可训练的变量 trainable_variables = weights.values() + biases.values() # 计算梯度 gradients = g.gradient(loss, trainable_variables)  # 按gradients更新 W 和 b optimizer.apply_gradients(zip(gradients, trainable_variables))# 针对给定步骤数进行训练for step, (batch_x, batch_y) in enumerate(train_data.take(training_steps), 1): # 运行优化以更新W和b值 run_optimization(batch_x, batch_y)  if step % display_step == 0: pred = conv_net(batch_x) loss = cross_entropy(pred, batch_y) acc = accuracy(pred, batch_y) print("step: %i, loss: %f, accuracy: %f" % (step, loss, acc))

output:

step: 10, loss: 72.370056, accuracy: 0.851562step: 20, loss: 53.936745, accuracy: 0.882812step: 30, loss: 29.929554, accuracy: 0.921875step: 40, loss: 28.075102, accuracy: 0.953125step: 50, loss: 19.366310, accuracy: 0.960938step: 60, loss: 20.398090, accuracy: 0.945312step: 70, loss: 29.320951, accuracy: 0.960938step: 80, loss: 9.121045, accuracy: 0.984375step: 90, loss: 11.680668, accuracy: 0.976562step: 100, loss: 12.413654, accuracy: 0.976562step: 110, loss: 6.675493, accuracy: 0.984375step: 120, loss: 8.730624, accuracy: 0.984375step: 130, loss: 13.608270, accuracy: 0.960938step: 140, loss: 12.859011, accuracy: 0.968750step: 150, loss: 9.110849, accuracy: 0.976562step: 160, loss: 5.832032, accuracy: 0.984375step: 170, loss: 6.996647, accuracy: 0.968750step: 180, loss: 5.325038, accuracy: 0.992188step: 190, loss: 8.866342, accuracy: 0.984375step: 200, loss: 2.626245, accuracy: 1.000000# 在验证集上测试模型pred = conv_net(x_test)print("Test Accuracy: %f" % accuracy(pred, y_test))

output:

Test Accuracy: 0.980000# 可视化预测import matplotlib.pyplot as plt# 从验证集中预测5张图像n_images = 5test_images = x_test[:n_images]predictions = conv_net(test_images)# 显示图片和模型预测结果for i in range(n_images): plt.imshow(np.reshape(test_images[i], [28, 28]), cmap='gray') plt.show() print("Model prediction: %i" % np.argmax(predictions.numpy()[i]))

output:

17a5a30b1070730ed9054ebb137da772.png

Model prediction: 7

eeabb2a3860fbe95e76d87818d5c9b74.png

Model prediction:2

06ee12331425929998217db085e44a39.png

Model prediction: 1

e430e7b84a919693309eb42df75d5b71.png

Model prediction: 0

2ea9fbcd95a8e3cd2179ad1fa12f7fc3.png

Model prediction: 4

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值