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

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

CNN 概述

MNIST 数据集概述

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

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

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

from __future__ import absolute_import, division, print_function

import tensorflow as tf
import numpy as np
# MNIST 数据集参数
num_classes = 10 # 所有类别(数字 0-9) # 训练参数
learning_rate = 0.001
training_steps = 200
batch_size = 128
display_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()
# 转化为float32
x_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.851562
step: 20, loss: 53.936745, accuracy: 0.882812
step: 30, loss: 29.929554, accuracy: 0.921875
step: 40, loss: 28.075102, accuracy: 0.953125
step: 50, loss: 19.366310, accuracy: 0.960938
step: 60, loss: 20.398090, accuracy: 0.945312
step: 70, loss: 29.320951, accuracy: 0.960938
step: 80, loss: 9.121045, accuracy: 0.984375
step: 90, loss: 11.680668, accuracy: 0.976562
step: 100, loss: 12.413654, accuracy: 0.976562
step: 110, loss: 6.675493, accuracy: 0.984375
step: 120, loss: 8.730624, accuracy: 0.984375
step: 130, loss: 13.608270, accuracy: 0.960938
step: 140, loss: 12.859011, accuracy: 0.968750
step: 150, loss: 9.110849, accuracy: 0.976562
step: 160, loss: 5.832032, accuracy: 0.984375
step: 170, loss: 6.996647, accuracy: 0.968750
step: 180, loss: 5.325038, accuracy: 0.992188
step: 190, loss: 8.866342, accuracy: 0.984375
step: 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 = 5
test_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:



Model prediction: 7



Model prediction:2



Model prediction: 1



Model prediction: 0



Model prediction: 4

欢迎关注磐创博客资源汇总站:

http://docs.panchuang.net/

欢迎关注PyTorch官方中文教程站:

http://pytorch.panchuang.net/

使用TensorFlow v2.0构建卷积神经网络的更多相关文章

  1. 使用TensorFlow v2.0构建多层感知器

    使用TensorFlow v2.0构建一个两层隐藏层完全连接的神经网络(多层感知器). 这个例子使用低级方法来更好地理解构建神经网络和训练过程背后的所有机制. 神经网络概述 MNIST 数据集概述 此 ...

  2. 【深度学习与TensorFlow 2.0】卷积神经网络(CNN)

    注:在很长一段时间,MNIST数据集都是机器学习界很多分类算法的benchmark.初学深度学习,在这个数据集上训练一个有效的卷积神经网络就相当于学习编程的时候打印出一行“Hello World!”. ...

  3. tensorflow1.0 构建卷积神经网络

    import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data import os os.envi ...

  4. TensorFlow v2.0实现Word2Vec算法

    使用TensorFlow v2.0实现Word2Vec算法计算单词的向量表示,这个例子是使用一小部分维基百科文章来训练的. 更多信息请查看论文: Mikolov, Tomas et al. " ...

  5. TensorFlow v2.0实现逻辑斯谛回归

    使用TensorFlow v2.0实现逻辑斯谛回归 此示例使用简单方法来更好地理解训练过程背后的所有机制 MNIST数据集概览 此示例使用MNIST手写数字.该数据集包含60,000个用于训练的样本和 ...

  6. TensorFlow v2.0的基本张量操作

    使用TensorFlow v2.0的基本张量操作 from __future__ import print_function import tensorflow as tf # 定义张量常量 a = ...

  7. TensorFlow构建卷积神经网络/模型保存与加载/正则化

    TensorFlow 官方文档:https://www.tensorflow.org/api_guides/python/math_ops # Arithmetic Operators import ...

  8. TensorFlow 实战之实现卷积神经网络

    本文根据最近学习TensorFlow书籍网络文章的情况,特将一些学习心得做了总结,详情如下.如有不当之处,请各位大拿多多指点,在此谢过. 一.相关性概念 1.卷积神经网络(ConvolutionNeu ...

  9. 使用 Estimator 构建卷积神经网络

    来源于:https://tensorflow.google.cn/tutorials/estimators/cnn 强烈建议前往学习 tf.layers 模块提供一个可用于轻松构建神经网络的高级 AP ...

随机推荐

  1. 理解 Java 内存模型的因果性约束

    目录 理解 Java 内存模型的因果性约束 欢迎讨论 规范理解 例子练习 例子1 例子2 总结 理解 Java 内存模型的因果性约束 欢迎讨论 欢迎加入技术交流群186233599讨论交流,也欢迎关注 ...

  2. Python在计算内存时应该注意的问题?

    我之前的一篇文章,带大家揭晓了 Python 在给内置对象分配内存时的 5 个奇怪而有趣的小秘密.文中使用了sys.getsizeof()来计算内存,但是用这个方法计算时,可能会出现意料不到的问题. ...

  3. C++冒险攻略(持续更新中。。。)

    C++语言程序设计 我的C++冒险之旅 绪论 计算机系统基本概念 计算机硬件 计算机程序语言 计算机解决问题是程序控制的 程序就是操作步骤 程序要使用语言来表达 机器语言 计算机能识别的是机器语言 机 ...

  4. Springboot与Maven多环境配置文件夹解决方案

    Profile用法 我们在application.yml中为jdbc.name赋予一个值,这个值为一个变量 jdbc: username: ${jdbc.username} Maven中的profil ...

  5. js的Set和Map集合

    目录 1.js的Set介绍 1-1.Set基础用法 1-2.Set对象的操作方法 1-3.Set对象的遍历方法 2.js的Set扩展WeakSet篇 3.js的Map介绍 3-1.Map基础用法 3- ...

  6. Cisco asa组建IPSEC for ikev1

    IPSec的实现主要由两个阶段来完成:--第一阶段,双方协商安全连接,建立一个已通过身份鉴别和安全保护的通道.--第二阶段,安全协议用于保护数据的和信息的交换. IPSec有两个安全协议:AH和ESP ...

  7. 一起了解 .Net Foundation 项目 No.18

    .Net 基金会中包含有很多优秀的项目,今天就和笔者一起了解一下其中的一些优秀作品吧. 中文介绍 中文介绍内容翻译自英文介绍,主要采用意译.如与原文存在出入,请以原文为准. Protobuild Pr ...

  8. CSS 文本截断方案

    单行截断 .ellipsis { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } 此方法兼容到ie6过.不过只能单行 ...

  9. Docker极简部署Kafka+Zookeeper+ElasticStack

    之前写ELK部分时有朋友问有没有能一键部署的Kafka+ELK,写本文主要是填这个坑,基本上配置已经集中在一两个文件中了,理论上此配置支持ElasticStack 7.x所有版本 本文所有配置与代码均 ...

  10. 在 macOS 下备份/还原/重置 LaunchPad 布局

    原文链接:https://billc.io/2019/07/launchpad-layout-backup/ 前几天升级到 Catalina Public Beta 后,LauchPad 会出现无法刷 ...