如何在 Python 中使用函式式 API 處理殘差連線?


Keras 存在於 Tensorflow 包中。可以使用以下程式碼行訪問它。

import tensorflow
from tensorflow import keras

Keras 函式式 API 有助於建立比使用順序式 API 建立的模型更靈活的模型。函式式 API 可以處理具有非線性拓撲的模型,可以共享層,並可以處理多個輸入和輸出。深度學習模型通常是一個包含多個層的無環有向圖 (DAG)。函式式 API 有助於構建圖層圖。

我們正在使用 Google Colaboratory 來執行以下程式碼。Google Colab 或 Colaboratory 有助於在瀏覽器上執行 Python 程式碼,無需任何配置,並且可以免費訪問 GPU(圖形處理單元)。Colaboratory 建立在 Jupyter Notebook 之上。以下是程式碼片段:

示例

print("Toy ResNet model for CIFAR10")
print("Layers generated for model")
inputs = keras.Input(shape=(32, 32, 3), name="img")
x = layers.Conv2D(32, 3, activation="relu")(inputs)
x = layers.Conv2D(64, 3, activation="relu")(x)
block_1_output = layers.MaxPooling2D(3)(x)

x = layers.Conv2D(64, 3, activation="relu", padding="same")(block_1_output)
x = layers.Conv2D(64, 3, activation="relu", padding="same")(x)
block_2_output = layers.add([x, block_1_output])

x = layers.Conv2D(64, 3, activation="relu", padding="same")(block_2_output)
x = layers.Conv2D(64, 3, activation="relu", padding="same")(x)
block_3_output = layers.add([x, block_2_output])

x = layers.Conv2D(64, 3, activation="relu")(block_3_output)
x = layers.GlobalAveragePooling2D()(x)
x = layers.Dense(256, activation="relu")(x)
x = layers.Dropout(0.5)(x)
outputs = layers.Dense(10)(x)

model = keras.Model(inputs, outputs, name="toy_resnet")
print("More information about the model")
model.summary()

程式碼來源 − https://www.tensorflow.org/guide/keras/functional

輸出

Toy ResNet model for CIFAR10
Layers generated for model
More information about the model
Model: "toy_resnet"
________________________________________________________________________________
__________________
Layer (type)          Output Shape          Param #       Connected to
================================================================================
==================
img (InputLayer)       [(None, 32, 32, 3)]    0
________________________________________________________________________________
__________________
conv2d_32 (Conv2D)    (None, 30, 30, 32)     896          img[0][0]
________________________________________________________________________________
__________________
conv2d_33 (Conv2D)    (None, 28, 28, 64)    18496         conv2d_32[0][0]
________________________________________________________________________________
__________________
max_pooling2d_8 (MaxPooling2D) (None, 9, 9, 64) 0          conv2d_33[0][0]
________________________________________________________________________________
__________________
conv2d_34 (Conv2D)       (None, 9, 9, 64)       36928       max_pooling2d_8[0][0]
________________________________________________________________________________
__________________
conv2d_35 (Conv2D)       (None, 9, 9, 64)       36928       conv2d_34[0][0]
________________________________________________________________________________
__________________
add_12 (Add)             (None, 9, 9, 64)          0       conv2d_35[0][0]
                                          max_pooling2d_8[0][0]
________________________________________________________________________________
__________________
conv2d_36 (Conv2D)          (None, 9, 9, 64)    36928       add_12[0][0]
________________________________________________________________________________
__________________
conv2d_37 (Conv2D)          (None, 9, 9, 64)    36928       conv2d_36[0][0]
________________________________________________________________________________
__________________
add_13 (Add)                (None, 9, 9, 64)       0       conv2d_37[0][0]
                                       add_12[0][0]
________________________________________________________________________________
__________________
conv2d_38 (Conv2D)          (None, 7, 7, 64)    36928       add_13[0][0]
________________________________________________________________________________
__________________
global_average_pooling2d_1    (Glo (None, 64)      0       conv2d_38[0][0]
________________________________________________________________________________
__________________
dense_40 (Dense)             (None, 256)          16640    global_average_pooling2d_1[0][0]
________________________________________________________________________________
__________________
dropout_2 (Dropout)          (None, 256)          0          dense_40[0][0]
________________________________________________________________________________
__________________
dense_41 (Dense)             (None, 10)          2570       dropout_2[0][0]
================================================================================
==================
Total params: 223,242
Trainable params: 223,242
Non-trainable params: 0
________________________________________________________________________________
__________________

解釋

  • 該模型具有多個輸入和輸出。

  • 函式式 API 簡化了與非線性連線拓撲的工作。

  • 此模型的各層並非按順序連線,因此“Sequential”API 無法處理它。

  • 這就是殘差連線發揮作用的地方。

  • 構建了一個使用 CIFAR10 的示例 ResNet 模型來演示這一點。

更新於: 2021年1月18日

211 次瀏覽

啟動您的職業生涯

完成課程獲得認證

開始
廣告
© . All rights reserved.