如何使用 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://www.tensorflow.org/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 維向量,以及一個用於訓練的自動編碼器模型。

更新於: 2021年1月18日

205 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始
廣告

© . All rights reserved.