-
BatchNormalization layerDeepLearning 2023. 5. 4. 19:33
https://keras.io/api/layers/normalization_layers/batch_normalization/ Layer that normalizes its inputs.
입력을 정규화하는 계층입니다.
Batch normalization applies a transformation that maintains the mean output close to 0 and the output standard deviation close to 1.
평균 출력을 0에 가깝게, 출력 표준 편차를 1에 가깝게 유지하는 변환을 적용합니다.
Importantly, batch normalization works differently during training and during inference.
중요한 점은 배치 정규화가 학습 시(model.fit())와 추론(model.predict()) 시 다르게 작동한다는 점입니다.
During training(학습 시 == model.fit())
the layer normalizes its output using the mean and standard deviation of the current batch of inputs.
레이어는 현재 입력 배치의 평균과 표준 편차를 사용하여 출력을 정규화합니다.
During inference(추론 시 == 예측 시 or 평가 시 == model.predict() or evaluate())
the layer normalizes its output using a moving average of the mean and standard deviation of the batches it has seen during training.
레이어는 학습 중에 본 배치의 평균과 표준 편차의 이동 평균을 사용하여 출력을 정규화합니다.
이 작업이 어떻게 진행될런지 직관적으로 이해가 안 됨.
BatchNormalization
- Batch Normalization은 기본적으로 Gradient Vanishing / Gradient Exploding 이 일어나지 않도록 하는 아이디어 중의 하나이다.
- 지금까지는 이 문제를 Activation 함수의 변화 (ReLU 등), Careful Initialization, small learning rate 등으로 해결
- Batch Normalization에서는 각 layer에 들어가는 input을 normalize 시킴 (* normalize 시킨 다는 것은 데이터의 평균은 0, 표준편차는 1을 따르는 데이터로 만드는 것을 의미)으로써 layer의 학습을 가속하는데, 이 때 각 mini-batch의 mean과 variance를 구하여 normalize한다.
- 실제로 이 Batch Normalization을 네트워크에 적용시킬 때는, 특정 Hidden Layer에 들어가기 전에 Batch Normalization Layer를 더해주어 input을 modify해준 뒤 새로운 값을 activation function으로 넣어주는 방식으로 사용한다.
- 아래 코드를 보면, BatchNormalization layer를 쌓은 후에 Activation layer를 쌓았다. 그러니까, Dense layer 안에 activation 파라미터를 받으면 위에서 설명한 방식으로 모델을 구성할 수가 없겠지. 물론 Dense layer 안에 actiavtion 파라미터를 받은 후에 BatchNormalization layer를 쌓아도 모델이 돌아가기는 한다. (아래에서 계속)
input = tf.keras.layers.Input(shape=(2,)) net = tf.keras.layers.Dense(units=32)(input) net = tf.keras.layers.BatchNormalization()(net) net = tf.keras.layers.Activation(activation='relu')(net) net = tf.keras.layers.Dense(units=32)(net) net = tf.keras.layers.BatchNormalization()(net) net = tf.keras.layers.Activation(activation='relu')(net) net = tf.keras.layers.Dense(units=1)(net) model = tf.keras.models.Model(input, net)
BatchNormalization layer 쌓는 순서 관련해서
사라지거나 폭발하는 그라디언트 문제(vanishing and exploding gardiens issue : 기울기가 소멸하거나 폭발하여 가중치가 0 또는 무한대로 가는 현상)를 해결하는 가장 강력한 도구는 일괄 정규화입니다. 배치 노멀라이제이션은 다음과 같이 작동합니다. 주어진 레이어의 각 유닛에 대해 먼저 z 점수를 계산한 다음 두 개의 학습된 변수 𝛾와 𝛽를 사용하여 선형 변환을 적용(선형 변환을 어떻게 하는 거지..?)합니다. 일괄 정규화는 일반적으로 비선형 활성화 함수(아래 그림 참조) 전에 수행하지만, 활성화 함수 이후에 적용하는 것도 도움이 될 수 있습니다.
tf.keras and TensorFlow: Batch Normalization to train deep neural networks faster
Training deep neural networks can be time consuming. In particular, training can be significantly impeded by vanishing gradients, which…
towardsdatascience.com
위 게시글? 에서 포함돼있는 내용인데, 일반적으로 BatchNormalization layer 를 쌓은 후에 Activation layer를 쌓는다고 한다. 근데 고정된 건 아닌듯.
BatchNormalization layer
https://eehoeskrap.tistory.com/430#%EB%B0%B0%EC%B9%98_%EC%A0%95%EA%B7%9C%ED%99%94_(Batch_Normalization)_%EB%9E%80?
여기가 배치 정규화를 정말 잘 설명해두었다.
나중에 꼭 봐야지,,,, 이번엔 여기까지!
'DeepLearning' 카테고리의 다른 글
LearningRateScheduler (0) 2023.06.20 Dense Layer 파라미터 개수 세기 (0) 2023.05.06