可以将 Keras 模型视为一个层并使用 Python 调用吗?如果是,证明它

Tensorflow 是 Google 提供的机器学习框架。它是一个与 Python 结合使用以实现算法、深度学习应用程序等的开源框架。它用于研究和生产目的。

Keras 是作为 ONEIROS(开放式神经电子智能机器人操作系统)项目研究的一部分而开发的。Keras 是一个深度学习 API,它是用 Python 编写的。它是一种高级 API,具有有助于解决机器学习问题的高效界面。它运行在 Tensorflow 框架之上。它旨在帮助快速进行实验。它提供了在开发和封装机器学习解决方案中必不可少的基本抽象和构建块。

它具有高度可扩展性,并具有跨平台功能。这意味着 Keras 可以在 TPU 或 GPU 集群上运行。Keras 模型也可以导出以在 Web 浏览器或手机中运行。

Keras 已经存在于 Tensorflow 包中。可以使用以下代码行访问它。

import tensorflow
from tensorflow import keras

是的,Keras 模型仅被视为一个层并使用 Python 调用。与使用顺序 API 创建的模型相比,Keras 函数式 API 有助于创建更灵活的模型。函数式 API 可以处理具有非线性拓扑结构的模型,可以共享层并处理多个输入和输出。深度学习模型通常是包含多个层的有向无环图 (DAG)。函数式 API 有助于构建层图。

我们正在使用 Google Colaboratory 运行以下代码。Google Colab 或 Colaboratory 帮助在浏览器上运行 Python 代码,并且需要零配置和免费访问 GPU(图形处理单元)。Colaboratory 建立在 Jupyter Notebook 之上。以下是将 Keras 模型视为层并使用 Python 调用的代码片段 -

示例

Encoder_input = keras.Input(shape=(28, 28, 1), name=”original_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 golbal 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()
decoder_input = keras.Input(shape=(16,), name="encoded_img")
print("Reshaping the layers in the model")
x = layers.Reshape((4, 4, 1))(decoder_input)
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)
print("Creating a model using the layers")
decoder = keras.Model(decoder_input, decoder_output, name="decoder")
print("More information about the model")
decoder.summary()

autoencoder_input = keras.Input(shape=(28, 28, 1), name="img")
encoded_img = encoder(autoencoder_input)
decoded_img = decoder(encoded_img)
autoencoder = keras.Model(autoencoder_input, decoded_img, name="autoencoder")
print("More information about the model")
autoencoder.summary()

代码信用 - https://www.tensorflow.org/guide/keras/functional

输出结果

original_img (InputLayer)    [(None, 28, 28, 1)]       0
_________________________________________________________________
conv2d_28 (Conv2D)          (None, 26, 26, 16)       160
_________________________________________________________________
conv2d_29 (Conv2D)          (None, 24, 24, 32)       4640
_________________________________________________________________
max_pooling2d_7 (MaxPooling2 (None, 8, 8, 32)          0
_________________________________________________________________
conv2d_30 (Conv2D)          (None, 6, 6, 32)          9248
_________________________________________________________________
conv2d_31 (Conv2D)          (None, 4, 4, 16)          4624
_________________________________________________________________
global_max_pooling2d_3       (Glob (None, 16)          0
=================================================================
Total params: 18,672
Trainable params: 18,672
Non-trainable params: 0
_________________________________________________________________
Reshaping the layers in the model
Creating a model using the layers
More information about the model
Model: "decoder"
_________________________________________________________________
Layer (type)                Output Shape             Param #
=================================================================
encoded_img (InputLayer)    [(None, 16)]             0
_________________________________________________________________
reshape_1 (Reshape)          (None, 4, 4, 1)         0
_________________________________________________________________
conv2d_transpose_4 (Conv2DTr (None, 6, 6, 16)       160
_________________________________________________________________
conv2d_transpose_5 (Conv2DTr (None, 8, 8, 32)       4640
_________________________________________________________________
up_sampling2d_1 (UpSampling2 (None, 24, 24, 32)       0
_________________________________________________________________
conv2d_transpose_6 (Conv2DTr (None, 26, 26, 16)      4624
_________________________________________________________________
conv2d_transpose_7 (Conv2DTr (None, 28, 28, 1)       145
=================================================================
Total params: 9,569
Trainable params: 9,569
Non-trainable params: 0
_________________________________________________________________
More information about the model
Model: "autoencoder"
_________________________________________________________________
Layer (type)             Output Shape          Param #
=================================================================
img (InputLayer)       [(None, 28, 28, 1)]       0
_________________________________________________________________
encoder (Functional)    (None, 16)             18672
_________________________________________________________________
decoder (Functional)    (None, 28, 28, 1)       9569
=================================================================
Total params: 28,241
Trainable params: 28,241
Non-trainable params: 0
_________________________________________________________________

解释

  • 通过在另一层的“输入”或输出上调用它,任何模型都可以被视为一个层。

  • 当模型被调用时,架构被重用。

  • 此外,权重也被重用。

  • 可以使用编码器模型(解码器模型)创建自动编码器模型。

  • 这两个模型被链接到两个调用中以获取自动编码器模型。