提交 4f6fe4f9 编写于 作者: W wizardforcel

2020-09-08 18:35:52

上级 06e1add7
# 前言
TensorFlow 是 Python 中最受欢迎的机器学习框架之一。 通过这本书,您将提高对 TensorFlow 最新功能的了解,并能够使用 Python 执行有监督和无监督的机器学习。
# 这本书是给谁的
顾名思义,本书旨在向读者介绍 TensorFlow 及其最新功能,包括 2.0.0 版以内的 Alpha 版本,包括急切的执行`tf.data``tf.keras`,TensorFlow Hub,机器学习, 和神经网络应用。
本书适用于对机器学习及其应用有所了解的任何人:数据科学家,机器学习工程师,计算机科学家,计算机科学专业的学生和业余爱好者。
# 本书涵盖的内容
[第 1 章](../Text/01.html)*引入 TensorFlow 2* ,通过查看一些代码片段(说明一些基本操作)来介绍 TensorFlow。 我们将概述现代 TensorFlow 生态系统,并了解如何安装 TensorFlow。
[第 2 章](../Text/02.html)*Keras,TensorFlow 2* 的高级 API,介绍了 Keras API,包括一些一般性的评论和见解,其后以四种不同的形式表示了基本架构 MNIST 数据集训练的方法。
[第 3 章](../Text/03.html)*使用 TensorFlow 2* 的 ANN 技术,探讨了许多支持创建和使用神经网络的技术。 本章将介绍到 ANN 的数据表示,ANN 的层,创建模型,梯度下降算法的梯度计算,损失函数以及保存和恢复模型。
[第 4 章](../Text/04.html)*使用 TensorFlow 2* 进行监督的机器学习,描述了在涉及线性回归的两种情况下使用 TensorFlow 的示例,在这些情况下,特征映射到具有连续值的已知标签,从而可以进行预测 看不见的特征。
[第 5 章](../Text/05.html)*使用 TensorFlow 2* 的无监督学习着眼于自动编码器在无监督学习中的两种应用:首先用于压缩数据;其次用于压缩数据。 第二,用于降噪,换句话说,去除图像中的噪声。
[第 6 章](../Text/06.html)*使用 TensorFlow 2* 识别图像,首先查看 Google Quick Draw 1 图像数据集,其次查看 CIFAR 10 图像数据集。
[第 7 章](../Text/07.html)*使用 TensorFlow 2* 进行神经样式传递,说明如何拍摄内容图像和样式图像,然后生成混合图像。 我们将使用经过训练的 VGG19 模型中的图层来完成此任务。
[第 8 章](../Text/08.html)*使用 TensorFlow 2* 的递归神经网络首先讨论了 RNN 的一般原理,然后介绍了如何获取和准备一些文本以供模型使用。
[第 9 章](../Text/09.html)*TensorFlow 估算器和 TensorFlow Hub* 首先介绍了用于训练时装数据集的估算器。 我们将看到估计器如何为 TensorFlow 提供简单直观的 API。 我们还将研究用于分析电影反馈数据库 IMDb 的神经网络。
[附录](../Text/10.html)*从 tf1.12 转换为 tf2* 包含一些将 tf1.12 文件转换为 tf2 的技巧。
# 充分利用这本书
假定熟悉 Python 3.6,并且熟悉 Jupyter Notebook 的使用。
本书的编写是假定读者比以文本形式出现的冗长文本解释更高兴以代码段和完整程序的形式给出的解释,当然,后者以不同的风格出现在本书中。
强烈建议您对机器学习的概念和技术有所了解,但是如果读者愿意对这些主题进行一些阅读,则这不是绝对必要的。
# 下载示例代码文件
您可以从 [www.packt.com](http://www.packt.com) 的帐户中下载本书的示例代码文件。 如果您在其他地方购买了此书,则可以访问 [www.packt.com/support](http://www.packt.com/support) 并注册以将文件直接通过电子邮件发送给您。
您可以按照以下步骤下载代码文件:
1. 登录或注册 [www.packt.com](http://www.packt.com)
2. 选择支持选项卡。
3. 单击代码下载和勘误。
4. 在搜索框中输入书籍的名称,然后按照屏幕上的说明进行操作。
下载文件后,请确保使用以下最新版本解压缩或解压缩文件夹:
* Windows 的 WinRAR / 7-Zip
* Mac 版 Zipeg / iZip / UnRarX
* 适用于 Linux 的 7-Zip / PeaZip
本书的代码包也托管在 GitHub 的 [https://github.com/PacktPublishing/Tensorflow-2.0-Quick-Start-Guide](https://github.com/PacktPublishing/Tensorflow-2.0-Quick-Start-Guide) 上。 如果代码有更新,它将在现有 GitHub 存储库上进行更新。
我们还从 [https://github.com/PacktPublishing/](https://github.com/PacktPublishing/) 提供了丰富的书籍和视频目录中的其他代码包。 去看一下!
# 下载彩色图像
我们还提供了一个 PDF 文件,其中包含本书中使用的屏幕截图/图表的彩色图像。 您可以在此处下载: [http://www.packtpub.com/sites/default/files/downloads/9781789530759_ColorImages.pdf](http://www.packtpub.com/sites/default/files/downloads/9781789530759_ColorImages.pdf)
# 使用约定
本书中使用了许多文本约定。
`CodeInText`:指示文本,数据库表名称,文件夹名称,文件名,文件扩展名,路径名,虚拟 URL,用户输入和 Twitter 句柄中的代码字。 这是一个示例:“将下载的`WebStorm-10*.dmg`磁盘映像文件安装为系统中的另一个磁盘。”
代码块设置如下:
```py
image1 = tf.zeros([7, 28, 28, 3]) # example-within-batch by height by width by color
```
当我们希望引起您对代码块特定部分的注意时,相关的行或项目以粗体显示:
```py
r1 = tf.reshape(t2,[2,6]) # 2 rows 6 cols
r2 = tf.reshape(t2,[1,12]) # 1 rows 12 cols
r1
# <tf.Tensor: id=33, shape=(2, 6), dtype=float32,
numpy= array([[ 0., 1., 2., 3., 4., 5.], [ 6., 7., 8., 9., 10., 11.]], dtype=float32)>
```
任何命令行输入或输出的编写方式如下:
```py
var = tf.Variable([3, 3])
```
**粗体**:表示新术语,重要单词或您在屏幕上看到的单词。 例如,菜单或对话框中的单词会出现在这样的文本中。 这是一个示例:“从管理面板中选择系统信息。”
警告或重要提示如下所示。
提示和技巧如下所示。
# 保持联系
始终欢迎读者的反馈。
**一般反馈**:如果您对本书的任何方面都有疑问,请在您的消息主题中提及书名,并通过`customercare@packtpub.com`向我们发送电子邮件。
**勘误表**:尽管我们已尽一切努力确保内容的准确性,但还是会发生错误。 如果您在这本书中发现错误,请向我们报告,我们将不胜感激。 请访问 [www.packt.com/submit-errata](http://www.packt.com/submit-errata) ,选择您的书籍,点击勘误提交表格链接,然后输入详细信息。
**盗版**:如果您在互联网上以任何形式看到我们的作品的任何非法副本,请向我们提供位置地址或网站名称,我们将不胜感激。 请通过`copyright@packt.com`与我们联系,并提供材料链接。
**如果您有兴趣成为作家**:如果您有一个专业的话题,并且对写作或撰写书籍感兴趣,请访问 [authors.packtpub.com [](http://authors.packtpub.com/)
# 评论
请留下评论。 阅读和使用本书后,为什么不在您购买本书的网站上留下评论? 然后,潜在的读者可以查看并使用您的公正意见做出购买决定,Packt 的我们可以理解您对我们产品的看法,我们的作者可以在书中看到您的反馈。 谢谢!
有关 Packt 的更多信息,请访问 [packt.com](http://www.packt.com/)
\ No newline at end of file
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
# 使用 TensorFlow 2 的无监督学习
在本章中,我们将研究使用 TensorFlow 2 进行无监督学习。无监督学习的目的是在数据中发现以前未标记数据点的模式或关系; 因此,我们只有功能。 这与监督式学习形成对比,在监督式学习中,我们既提供了功能及其标签,又希望预测以前未见过的新功能的标签。 在无监督学习中,我们想找出我们的数据是否存在基础结构。 例如,可以在不事先了解其结构的情况下以任何方式对其进行分组或组织吗? 这被称为**聚类**。 例如,亚马逊在其推荐系统中使用无监督学习来建议您以书本方式可能购买的商品,例如,通过识别以前购买的商品类别来提出建议。
无监督学习的另一种用途是在数据压缩技术中,其中数据中的模式可以用更少的内存表示,而不会损害数据的结构或完整性。 在本章中,我们将研究两个自动编码器,以及如何将它们用于压缩数据以及如何消除图像中的噪声。
在本章中,我们将深入探讨自动编码器。
# 汽车编码器
自动编码是一种使用 ANN 实现的数据压缩和解压缩算法。 由于它是学习算法的无监督形式,因此我们知道只需要未标记的数据。 它的工作方式是通过强制输入通过瓶颈(即,宽度小于原始输入的一层或多层)来生成输入的压缩版本。 要重建输入(即解压缩),我们可以逆向处理。 我们使用反向传播在中间层中创建输入的表示形式,并重新创建输入作为表示形式的输出。
自动编码是有损的,也就是说,与原始输入相比,解压缩后的输出将变差。 这与 MP3 和 JPEG 压缩格式相似。
自动编码是特定于数据的,也就是说,只有与它们经过训练的数据相似的数据才可以正确压缩。 例如,训练有素的自动编码器在汽车图片上的表现会很差,这是因为其学习到的功能将是汽车特有的。
# 一个简单的自动编码器
让我们编写一个非常简单的自动编码器,该编码器仅使用一层 ANN。 首先,像往常一样,让我们​​从导入开始,如下所示:
```py
from tensorflow.keras.layers import Input, Dense
from tensorflow.keras.models import Model
from tensorflow.keras.datasets import fashion_mnist
from tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping
from tensorflow.keras import regularizers
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
```
# 预处理数据
然后,我们加载数据。 对于此应用程序,我们将使用`fashion_mnist`数据集,该数据集旨在替代著名的 MNIST 数据集。 本节末尾有这些图像的示例。 每个数据项(图像中的像素)都是 0 到 255 之间的无符号整数,因此我们首先将其转换为`float32`,然后将其缩放为零至一的范围,以使其适合以后的学习过程:
```py
(x_train, _), (x_test, _) = fashion_mnist.load_data() # we don't need the labels
x_train = x_train.astype('float32') / 255\. # normalize
x_test = x_test.astype('float32') / 255.
print(x_train.shape) # shape of input
print(x_test.shape)
```
这将给出形状,如以下代码所示:
```py
(60000, 28, 28)
(10000, 28, 28)
```
接下来,我们将图像展平,因为我们要将其馈送到一维的密集层:
```py
x_train = x_train.reshape(( x_train.shape[0], np.prod(x_train.shape[1:]))) #flatten
x_test = x_test.reshape((x_test.shape[0], np.prod(x_test.shape[1:])))
print(x_train.shape)
print(x_test.shape)
```
现在的形状如下:
```py
(60000, 784)
(10000, 784)
```
分配所需的尺寸,如以下代码所示:
```py
image_dim = 784 # this is the size of our input image, 784
encoding_dim = 32 # this is the length of our encoded items.Compression of factor=784/32=24.5
```
接下来,我们构建单层编码器和自动编码器模型,如下所示:
```py
input_image = Input(shape=(image_dim, )) # the input placeholder
encoded_image = Dense(encoding_dim, activation='relu',
activity_regularizer=regularizers.l1(10e-5))(input_image)# "encoded" is the encoded representation of the input
encoder = Model(input_image, encoded_image)
decoded_image = Dense(image_dim, activation='sigmoid')(encoded_image)# "decoded" is the lossy reconstruction of the input
autoencoder = Model(input_image, decoded_image) # this model maps an input to its reconstruction
```
然后,我们构造解码器模型,如下所示:
```py
encoded_input = Input(shape=(encoding_dim,))# create a placeholder for an encoded (32-dimensional) input
decoder_layer = autoencoder.layers[-1]# retrieve the last layer of the autoencoder model
decoder = Model(encoded_input, decoder_layer(encoded_input))# create the decoder model
```
接下来,我们可以编译我们的自动编码器。 由于数据几乎是二进制的,因此选择了`binary_crossentropy`损耗,因此,我们可以最小化每个像素的二进制交叉熵:
```py
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')
```
我们可以定义两个有用的检查点。 第一个在每个时期后保存模型。 如果`save_best_only=True`,根据监视的数量(验证损失),最新的最佳模型将不会被覆盖。
其签名如下:
```py
keras.callbacks.ModelCheckpoint(filepath, monitor='val_loss', verbose=0, save_best_only=False, save_weights_only=False, mode='auto', period=1)
```
我们声明如下:
```py
checkpointer1 = ModelCheckpoint(filepath= 'model.weights.best.hdf5' , verbose =2, save_best_only = True)
```
当监视器中的更改(验证损失)小于`min_delta`时,即小于`min_delta`的更改不算改善时,第二个检查点停止训练。 这对于`patience`时代必定会发生,然后停止训练。 其签名如下:
```py
EarlyStopping(monitor='val_loss', min_delta=0, patience=0, verbose=0, mode='auto', baseline=None)
```
我们声明如下:
```py
checkpointer2 = EarlyStopping(monitor='val_loss', min_delta=0.0005, patience=2, verbose=2, mode='auto')
```
# 训练
训练运行使用`.fit`方法,其签名如下:
```py
autoencoder.fit(x=None, y=None, batch_size=None, epochs=1, verbose=1, callbacks=None, validation_split=0.0, validation_data=None, shuffle=True, class_weight=None, sample_weight=None, initial_epoch=0, steps_per_epoch=None, validation_steps=None, max_queue_size=10, workers=1, use_multiprocessing=False, **kwargs)
```
香草训练运行如下。 注意,我们如何传递`x``y``x_train`,因为我们要使用`x`输入并尝试在输出(`y=x`)上再现它。 请注意以下代码:
```py
epochs = 50
autoencoder.fit(x_train, x_train, epochs=epochs, batch_size=256, verbose=2, shuffle=True, validation_data=(x_test, x_test))
```
这之后是一些代码,用于压缩和解压缩(编码和解码)`test`数据。 请记住,`encoder``decoder`都是模型,所以我们可以调用该方法。 在它们上使用`predict`方法生成其输出:
```py
encoded_images = encoder.predict(x_test) #compress
decoded_images = decoder.predict(encoded_images) #decompress
```
我们还可以使用`ModelCheckpoint`检查点,在这种情况下,我们的`.fit`调用如下:
```py
epochs = 50
autoencoder.fit(x_train, x_train, epochs=epochs, batch_size=256, verbose=2, callbacks=[checkpointer1], shuffle=True, validation_data=(x_test, x_test))
```
我们还需要按如下方式加载保存的权重,以获取最佳模型:
```py
autoencoder.load_weights('model.weights.best.hdf5' )
encoded_images = encoder.predict(x_test)
decoded_images = decoder.predict(encoded_images)
```
以类似的方式,我们可以使用`EarlyStopping`,在这种情况下,`.fit`调用如下:
```py
epochs = 50
autoencoder.fit(x_train, x_train, epochs=epochs, batch_size=256, verbose=2, callbacks=[checkpointer2], shuffle=True, validation_data=(x_test, x_test))
```
# 显示结果
下面是一些代码,可以在屏幕上前后打印一些内容。 我们正在使用以下代码:
```py
plt.subplot(nrows, ncols, index, **kwargs)
```
子图在具有`nrows`行和`ncols`列的网格上的`index`位置处,`index`位置从左上角的一个位置开始,并向右增加以定位时尚项目:
```py
number_of_items = 12 # how many items we will display
plt.figure(figsize=(20, 4))
for i in range(number_of_items):
# display items before compression
graph = plt.subplot(2, number_of_items, i + 1)
plt.imshow(x_test[i].reshape(28, 28))
plt.gray()
graph.get_xaxis().set_visible(False)
graph.get_yaxis().set_visible(False)
# display items after decompression
graph = plt.subplot(2, number_of_items, i + 1 + number_of_items)
plt.imshow(decoded_images[i].reshape(28, 28))
plt.gray()
graph.get_xaxis().set_visible(False)
graph.get_yaxis().set_visible(False)
plt.show()
```
压缩前的结果如下:
![](img/9758ae58-a6e9-401b-8c2a-cc46c6d4a00c.png)
减压后,结果如下所示:
![](img/e6499d2a-350c-4aff-bf67-72f8dd0338a8.png)
因此,压缩/解压缩的有损性很明显。 作为一种可能的健全性检查,如果我们使用`encoding_dim = 768`(与输入相同数量的隐藏层节点),我们将得到以下结果:
![](img/a271f6c6-dc36-446e-a527-44d5edf655be.png)
这可能与原始版本略有不同。 接下来,我们将看一下自动编码的应用程序。
# 自动编码器应用程序–去噪
自动编码器的一个很好的应用是去噪:去除图像(噪声)中小的随机伪像的过程。 我们将用多层卷积码代替简单的一层自动编码器。
我们将人造噪声添加到我们的时装中,然后将其消除。 我们还将借此机会研究使用 TensorBoard 来检查一些网络训练指标。
# 建立
我们最初的进口包括我们的卷积网络的进口。
注意,我们不必显式地使用 Keras,因为它是 TensorFlow 本身的模块,如以下代码所示:
```py
from tensorflow.keras.layers import Input, Dense, Conv2D, MaxPooling2D, UpSampling2D
from tensorflow.keras.models import Model
from tensorflow.keras.datasets import fashion_mnist
from tensorflow.keras.callbacks import TensorBoard
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
```
# 预处理数据
首先,加载图像数据; 我们不需要标签,因为我们只关注图像本身:
```py
(train_x, _), (test_x, _) = fashion_mnist.load_data()
```
接下来,像以前一样,将图像数据点转换为零至一范围内的`float32`值:
```py
train_x = train_x.astype('float32') / 255.
test_x = test_x.astype('float32') / 255.
```
检查形状,如以下代码所示:
```py
print(train_x.shape)
print(test_x.shape)
```
它给出以下结果:
```py
(60000, 28, 28) (10000, 28, 28)
```
输入卷积层需要以下形状:
```py
train_x = np.reshape(train_x, (len(train_x), 28, 28, 1))
test_x = np.reshape(test_x, (len(test_x), 28, 28, 1))
```
在这里,形状中的一个是用于灰度通道; 以下是形状的完整性检查:
```py
print(train_x.shape)
print(test_x.shape)
```
得到以下结果:
```py
(60000, 28, 28, 1) (10000, 28, 28, 1)
```
为了在图像中引入一些随机噪声,我们在训练和测试集中添加了`np.random.normal`(即高斯)值数组。 所需的签名如下:
```py
numpy.random.normal(loc=0.0, scale=1.0, size=None)
```
在这里,`loc`是分布的中心,`scale`是标准偏差,`size`是输出形状。 因此,我们使用以下代码:
```py
noise = 0.5
train_x_noisy = train_x + noise * np.random.normal(loc=0.0, scale=1.0, size=train_x.shape)
test_x_noisy = test_x + noise * np.random.normal(loc=0.0, scale=1.0, size=test_x.shape)
```
由于这可能会使我们的值超出零至一的范围,因此我们将值裁剪到该范围:
```py
train_x_noisy = np.clip(train_x_noisy, 0., 1.)
test_x_noisy = np.clip(test_x_noisy, 0., 1.)
```
# 嘈杂的图像
下面的代码从测试集中打印出一些嘈杂的图像。 注意如何调整图像的显示形状:
```py
plt.figure(figsize=(20, 2))
for i in range(number_of_items):
display = plt.subplot(1, number_of_items,i+1)
plt.imshow(test_x_noisy[i].reshape(28, 28))
plt.gray()
display.get_xaxis().set_visible(False)
display.get_yaxis().set_visible(False)
plt.show()
```
这是结果,如以下屏幕快照所示:
![](img/4456e23d-5c7b-4978-a348-ab3a9be3e662.png)
因此很明显,原始图像与噪点几乎没有区别。
# 创建编码层
接下来,我们创建编码和解码层。 我们将使用 Keras 功能 API 样式来设置模型。 我们从一个占位符开始,以(下一个)卷积层所需的格式输入:
```py
input_image = Input(shape=(28, 28, 1))
```
接下来,我们有一个卷积层。 回忆卷积层的签名:
```py
Conv2D(filters, kernel_size, strides=(1, 1), padding='valid', data_format=None, dilation_rate=(1, 1), activation=None, use_bias=True, kernel_initializer='glorot_uniform', bias_initializer='zeros', kernel_regularizer=None, bias_regularizer=None, activity_regularizer=None, kernel_constraint=None, bias_constraint=None, **kwargs)
```
我们将主要使用默认值; 接下来是我们的第一个`Conv2D`。 注意`(3,3)`的内核大小; 这是 Keras 应用于输入图像的滑动窗口的大小。 还记得`padding='same'`表示图像用 0 左右填充,因此卷积的输入和输出层是内核(过滤器)以其中心“面板”开始于图像中第一个像素时的大小。 。 默认步幅`(1, 1)`表示滑动窗口一次从图像的左侧到末尾水平移动一个像素,然后向下移动一个像素,依此类推。 接下来,我们将研究每个图层的形状,如下所示:
```py
im = Conv2D(filters=32, kernel_size=(3, 3), activation='relu', padding='same')(input_image)
print(x.shape)
```
得到以下结果:
```py
(?, 28, 28, 32)
```
`?`代表输入项目的数量。
接下来,我们有一个`MaxPooling2D`层。 回想一下,在此情况下,此操作将在图像上移动`(2, 2)`大小的滑动窗口,并采用在每个窗口中找到的最大值。 其签名如下:
```py
MaxPooling2D(pool_size=(2, 2), strides=None, padding='valid', data_format=None, **kwargs)
```
这是下采样的示例,因为生成的图像尺寸减小了。 我们将使用以下代码:
```py
im = MaxPooling2D((2, 2), padding='same')(im)
print(im.shape)
```
得到以下结果:
```py
(?, 14, 14, 32)
```
其余的编码层如下:
```py
im = Conv2D(32, (3, 3), activation='relu', padding='same')(im)
print(im.shape)
encoded = MaxPooling2D((2, 2), padding='same')(im)
print(encoded.shape)
```
所有这些都结束了编码。
# 创建解码层
为了进行解码,我们反转了该过程,并使用上采样层`UpSampling2D`代替了最大池化层。 上采样层分别按大小[0]和大小[1]复制数据的行和列。
因此,在这种情况下,*会取消*最大合并层的效果,尽管会损失细粒度。 签名如下:
```py
UpSampling2D(size=(2, 2), data_format=None, **kwargs)
```
我们使用以下内容:
```py
im = UpSampling2D((2, 2))(im)
```
以下是解码层:
```py
im = Conv2D(32, (3, 3), activation='relu', padding='same')(encoded)
print(im.shape)
im = UpSampling2D((2, 2))(im)
print(im.shape)
im = Conv2D(32, (3, 3), activation='relu', padding='same')(im)
print(im.shape)
im = UpSampling2D((2, 2))(im)
print(im.shape)
decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(im)
print(decoded.shape)
```
得到以下结果:
```py
(?, 7, 7, 32) (?, 14, 14, 32) (?, 14, 14, 32) (?, 28, 28, 32) (?, 28, 28, 1)
```
因此,您可以看到解码层如何逆转编码层的过程。
# 型号汇总
这是我们模型的摘要:
![](img/b33ae38e-dc32-4e06-bb11-b04215f913c1.png)
看看我们如何得出参数数字很有启发性。
公式是参数数量= *过滤器数量 x 内核大小 x 上一层的深度+过滤器数量(用于偏差)*
* input_1:这是一个占位符,没有可训练的参数
* conv2d:过滤器数量= 32,内核大小= *3 * 3 = 9* ,上一层的深度= 1,因此 *32 * 9 + 32 = 320*
* max_pooling2d:最大池化层没有可训练的参数。
* conv2d_1:过滤器数= 32,内核大小= *3 * 3 = 9* ,上一层的深度= 14,因此 *32 * 9 * 32 + 32 = 9,248*
* conv_2d_2,conv2d_3:与 conv2d_1 相同
* conv2d_4:*1 * 9 * 32 +1 = 289*
# 模型实例化,编译和训练
接下来,我们用输入层和输出层实例化模型,然后使用`.compile`方法设置模型以进行训练:
```py
autoencoder = Model(inputs=input_img, outputs=decoded)
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')
```
现在,我们准备训练模型以尝试恢复时尚商品的图像。 请注意,我们已经为 TensorBoard 提供了回调,因此我们可以看一下一些训练指标。 Keras TensorBoard 签名如下:
```py
keras.callbacks.TensorBoard(
["log_dir='./logs'", 'histogram_freq=0', 'batch_size=32', 'write_graph=True', 'write_grads=False', 'write_images=False', 'embeddings_freq=0', 'embeddings_layer_names=None', 'embeddings_metadata=None', 'embeddings_data=None', "update_freq='epoch'"],
)
```
我们将主要使用默认值,如下所示:
```py
tb = [TensorBoard(log_dir='./tmp/tb', write_graph=True)]
```
接下来,我们使用`.fit()`方法训练自动编码器。 以下代码是其签名:
```py
fit(x=None, y=None, batch_size=None, epochs=1, verbose=1, callbacks=None, validation_split=0.0, validation_data=None, shuffle=True, class_weight=None, sample_weight=None, initial_epoch=0, steps_per_epoch=None, validation_steps=None, validation_freq=1)
```
注意我们如何将`x_train_noisy`用于功能(输入),并将`x_train`用于标签(输出):
```py
epochs=100
batch_size=128
autoencoder.fit(x_train_noisy, x_train, epochs=epochs,batch_size=batch_size, shuffle=True, validation_data=(x_test_noisy, x_test), callbacks=tb)
```
# 去噪图像
现在,通过解码以下第一行中的所有测试集,然后循环遍历一个固定数字(`number_of_items`)并显示它们,来对测试集中的一些噪点图像进行去噪。 请注意,在显示每个图像(`im`)之前,需要对其进行重塑:
```py
decoded_images = autoencoder.predict(test_noisy_x)
number_of_items = 10
plt.figure(figsize=(20, 2))
for item in range(number_of_items):
display = plt.subplot(1, number_of_items,item+1)
im = decoded_images[item].reshape(28, 28)
plt.imshow(im, cmap="gray")
display.get_xaxis().set_visible(False)
display.get_yaxis().set_visible(False)
plt.show()
```
我们得到以下结果:
![](img/5fd8eb0d-a384-485f-a54a-d05fb4780f67.png)
考虑到图像最初模糊的程度,降噪器已经做了合理的尝试来恢复图像。
# TensorBoard 输出
要查看 TensorBoard 输出,请在命令行上使用以下命令:
```py
tensorboard --logdir=./tmp/tb
```
然后,您需要将浏览器指向`http://localhost:6006`
下图显示了作为训练和验证时间的函数( *x* -轴)的损失( *y* -轴):
下图显示了训练损失:
![](img/1e1ff356-312f-4c90-8f53-75798f678bc4.png)
验证损失如下图所示:
![](img/beed13e6-2d68-4db6-bd11-1d4fe2ea089d.png)
到此结束我们对自动编码器的研究。
# 摘要
在本章中,我们研究了自动编码器在无监督学习中的两种应用:首先用于压缩数据,其次用于降噪,这意味着从图像中去除噪声。
在下一章中,我们将研究如何在图像处理和识别中使用神经网络。
\ No newline at end of file
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
# 从 tf1.12 转换为 tf2
Google 提供了一个名为 `tf_upgrade_v2`的命令行脚本,该脚本会将 1.12 版文件(`.py``.ipynb`文件)转换为 TensorFlow 2 兼容文件。
此转换的语法如下:
```py
tf_upgrade_v2 --infile file_to_convert --outfile converted_file
```
[https://www.youtube.com/watch?v=JmSNUeBG-PQ & list = PLQY2H8rRoyvzoUYI26kHmKSJBedn3SQuB & index = 32 & t = 71s [](https://www.youtube.com/watch?v=JmSNUeBG-PQ&list=PLQY2H8rRoyvzoUYI26kHmKSJBedn3SQuB&index=32&t=71s) [,以及有关它的更多详细信息,请参见](https://github.com/tensorflow/docs/blob/master/site/en/r2/guide/upgrade.md) [https://github.com/tensorflow/docs/blob/master/site/en/r2/guide/upgrade.md](https://github.com/tensorflow/docs/blob/master/site/en/r2/guide/upgrade.md)
重要的是要注意,在运行脚本之前,不应该*手动更新*的代码部分。**
该脚本不会解决所有问题,但是它生成的报告将标识那些必须手动解决的问题。
特别是,`tf.contrib`已从 TF2 中删除,因此必须跟踪并手动修复以前驻留在其中的函数。
这是脚本生成的报告的示例:
```py
Processing file 'Chapter1_TF2_Snippets.ipynb'
outputting to 'Chapter1_TF2_alpha'
--------------------------------------------------------------------------------
37:4: INFO: Added keywords to args of function 'tf.size'
48:13: INFO: Added keywords to args of function 'tf.transpose'
74:0: INFO: Added keywords to args of function 'tf.reduce_mean'
75:0: INFO: Added keywords to args of function 'tf.reduce_mean'
76:0: INFO: Added keywords to args of function 'tf.reduce_mean'
77:0: INFO: Added keywords to args of function 'tf.reduce_mean'
78:0: INFO: Added keywords to args of function 'tf.reduce_mean'
110:4: INFO: Added keywords to args of function 'tf.argmax'
114:4: INFO: Added keywords to args of function 'tf.argmin'
121:4: INFO: Added keywords to args of function 'tf.argmax'
123:4: INFO: Added keywords to args of function 'tf.argmin'
127:4: INFO: Added keywords to args of function 'tf.argmax'
129:4: INFO: Added keywords to args of function 'tf.argmin'
136:0: ERROR: Using member tf.contrib.integrate.odeint in deprecated module tf.contrib. tf.contrib.integrate.odeint cannot be converted automatically. tf.contrib will not be distributed with TensorFlow 2.0, please consider an alternative in non-contrib TensorFlow, a community-maintained repository, or fork the required code.
162:10: INFO: Added keywords to args of function 'tf.transpose'
173:11: INFO: Added keywords to args of function 'tf.reduce_mean'
```
\ No newline at end of file
此差异已折叠。
此差异已折叠。
此差异已折叠。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册