[TensorFlow] Wrapper Layers 예제

Wrapper 레이어는 내부 레이어에 추가적인 동작을 부여하는 역할을 합니다. TimeDistributed, Bidirectional 등이 Wrapper 레이어의 예입니다. 여기에서는 TimeDistributed 레이어를 사용한 예제를 보여드리겠습니다.

import tensorflow as tf
from tensorflow.keras.layers import Input, Dense, TimeDistributed
from tensorflow.keras.models import Model

## 더미 시퀀스 데이터 생성
dummy_sequence = tf.random.normal(shape=(4, 10, 32))  # 4개의 시퀀스, 각 시퀀스 길이: 10, 특성 개수: 32

## 입력 레이어 정의
input_layer = Input(shape=(10, 32))

## TimeDistributed 밀집 레이어 정의
time_distributed_layer = TimeDistributed(Dense(64, activation='relu'))(input_layer)

## 모델 생성
model = Model(inputs=input_layer, outputs=time_distributed_layer)

## 모델 컴파일
model.compile(optimizer='adam', loss='mse')

## 모델 요약
model.summary()

## 모델 실행
output = model.predict(dummy_sequence)

print("Input shape:", dummy_sequence.shape)
print("Output shape:", output.shape)` 

위 예제에서는 TimeDistributed 레이어를 사용하여 시퀀스 내 각 시간 단계에 Dense 레이어를 적용합니다. 이렇게 함으로써 각 시간 단계마다 독립적인 Dense 레이어가 생성됩니다.

실행 결과 예시:

Input shape: (4, 10, 32)
Output shape: (4, 10, 64)` 

TimeDistributed 레이어는 시퀀스 내 각 시간 단계에 동일한 레이어를 적용할 때 사용되며, 입력 시퀀스의 각 시간 단계마다 다른 연산을 수행할 수 있도록 합니다

Input shape: (1000, 20)
Output shape: (1000, 10)` 

GaussianNoise 레이어를 사용하여 입력 데이터에 잡음을 추가하여 모델의 강건성을 높이는 방법을 알아보았습니다.