如何使用 Python 中的编码器和解码器生成自动编码器?
TensorFlow 是 Google 提供的一个机器学习框架。它是一个开源框架,与 Python 结合使用,用于实现算法、深度学习应用程序等等。它用于研究和生产目的。
可以使用以下代码行在 Windows 上安装“tensorflow”包:
pip install tensorflow
张量是 TensorFlow 中使用的数据结构。它有助于连接流图中的边。此流图称为“数据流图”。张量只不过是多维数组或列表。
Keras 是作为 ONEIROS(开放式神经电子智能机器人操作系统)项目研究的一部分开发的。Keras 是一个用 Python 编写的深度学习 API。它是一个高级 API,具有高效的界面,有助于解决机器学习问题。它运行在 TensorFlow 框架之上。它是为了帮助快速实验而构建的。它提供了开发和封装机器学习解决方案必不可少的抽象和构建块。
Keras 已经存在于 TensorFlow 包中。可以使用以下代码行访问它。
import tensorflow from tensorflow import keras
Keras 函数式 API 有助于创建比使用顺序 API 创建的模型更灵活的模型。函数式 API 可以处理具有非线性拓扑的模型,可以共享层,并处理多个输入和输出。深度学习模型通常是一个包含多个层的定向无环图 (DAG)。函数式 API 有助于构建图层图。
我们正在使用 Google Colaboratory 来运行以下代码。Google Colab 或 Colaboratory 有助于通过浏览器运行 Python 代码,并且无需任何配置即可免费访问 GPU(图形处理单元)。Colaboratory 是在 Jupyter Notebook 之上构建的。以下是代码片段,请查看如何使用编码器和解码器生成自动编码器:
示例
encoder_input = keras.Input(shape=(28, 28, 1), name="img")
print("Adding layers to the model")
x = layers.Conv2D(16, 3, activation="relu")(encoder_input)
x = layers.Conv2D(32, 3, activation="relu")(x)
x = layers.MaxPooling2D(3)(x)
x = layers.Conv2D(32, 3, activation="relu")(x)
x = layers.Conv2D(16, 3, activation="relu")(x)
print("Performing global max pooling")
encoder_output = layers.GlobalMaxPooling2D()(x)
print("Creating a model using the layers")
encoder = keras.Model(encoder_input, encoder_output, name="encoder")
print("More information about the model")
encoder.summary()
print("Reshaping the layers in the model")
x = layers.Reshape((4, 4, 1))(encoder_output)
x = layers.Conv2DTranspose(16, 3, activation="relu")(x)
x = layers.Conv2DTranspose(32, 3, activation="relu")(x)
x = layers.UpSampling2D(3)(x)
x = layers.Conv2DTranspose(16, 3, activation="relu")(x)
decoder_output = layers.Conv2DTranspose(1, 3, activation="relu")(x)
autoencoder = keras.Model(encoder_input, decoder_output, name="autoencoder")
print("More information about the autoencoder")
autoencoder.summary()代码来源 - https://tensorflowcn.cn/guide/keras/functional
输出
Adding layers to the model Performing global max pooling Creating a model using the layers More information about the model Model: "encoder" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= img (InputLayer) [(None, 28, 28, 1)] 0 _________________________________________________________________ conv2d (Conv2D) (None, 26, 26, 16) 160 _________________________________________________________________ conv2d_1 (Conv2D) (None, 24, 24, 32) 4640 _________________________________________________________________ max_pooling2d (MaxPooling2D) (None, 8, 8, 32) 0 _________________________________________________________________ conv2d_2 (Conv2D) (None, 6, 6, 32) 9248 _________________________________________________________________ conv2d_3 (Conv2D) (None, 4, 4, 16) 4624 _________________________________________________________________ global_max_pooling2d (Global (None, 16) 0 ================================================================= Total params: 18,672 Trainable params: 18,672 Non-trainable params: 0 _________________________________________________________________ Reshaping the layers in the model More information about the autoencoder Model: "autoencoder" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= img (InputLayer) [(None, 28, 28, 1)] 0 _________________________________________________________________ conv2d (Conv2D) (None, 26, 26, 16) 160 _________________________________________________________________ conv2d_1 (Conv2D) (None, 24, 24, 32) 4640 _________________________________________________________________ max_pooling2d (MaxPooling2D) (None, 8, 8, 32) 0 _________________________________________________________________ conv2d_2 (Conv2D) (None, 6, 6, 32) 9248 _________________________________________________________________ conv2d_3 (Conv2D) (None, 4, 4, 16) 4624 _________________________________________________________________ global_max_pooling2d (Global (None, 16) 0 _________________________________________________________________ reshape (Reshape) (None, 4, 4, 1) 0 _________________________________________________________________ conv2d_transpose (Conv2DTran (None, 6, 6, 16) 160 _________________________________________________________________ conv2d_transpose_1 (Conv2DTr (None, 8, 8, 32) 4640 _________________________________________________________________ up_sampling2d (UpSampling2D) (None, 24, 24, 32) 0 _________________________________________________________________ conv2d_transpose_2 (Conv2DTr (None, 26, 26, 16) 4624 _________________________________________________________________ conv2d_transpose_3 (Conv2DTr (None, 28, 28, 1) 145 ================================================================= Total params: 28,241 Trainable params: 28,241 Non-trainable params: 0 _________________________________________________________________
解释
将层添加到模型中。
对这些层执行全局最大池化。
使用这些层创建一个模型。
可以使用“summary”方法显示有关模型的更多信息。
使用函数式 API,在指定图层图的输入和输出后创建模型。
这表明可以使用单个图生成多个模型。
这里,使用层堆栈实例化两个模型——一个编码器,它将图像输入转换为 16 维向量,以及一个用于训练的自动编码器模型。
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP