Computer >> 컴퓨터 >  >> 프로그램 작성 >> Python

기능적 API를 사용하여 Python에서 잔여 연결을 작업하려면 어떻게 해야 합니까?

<시간/>

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를 사용하면 비선형 연결 토폴로지로 쉽게 작업할 수 있습니다.

  • 이 레이어가 있는 모델은 순차적으로 연결되지 않아 '시퀀셜' API가 작동하지 않습니다.

  • 여기서 잔여 연결이 그림으로 나타납니다.

  • CIFAR10을 사용하는 샘플 ResNet 모델은 동일한 것을 보여주기 위해 구축되었습니다.