Batch size และ Batch Normalization

Deep Learning เบื้องต้นด้วย Keras

Miguel Esteban

Data Scientist & Founder

Deep Learning เบื้องต้นด้วย Keras

Deep Learning เบื้องต้นด้วย Keras

Mini-batch

ข้อดี

  • เทรนโครงข่ายได้เร็วขึ้น (อัปเดตค่าน้ำหนักได้มากขึ้นในเวลาเท่ากัน)
  • ใช้ RAM น้อยลง สามารถเทรนกับชุดข้อมูลขนาดใหญ่ได้
  • Noise ช่วยให้โครงข่ายหลุดจาก local minima และได้ค่าความผิดพลาดที่ต่ำกว่า

ข้อเสีย

  • ต้องรันซ้ำหลายรอบมากขึ้น
  • ต้องปรับค่า batch size ให้เหมาะสม
Deep Learning เบื้องต้นด้วย Keras

1 Stack Exchange
Deep Learning เบื้องต้นด้วย Keras

Batch size ใน Keras

# Fitting an already built and compiled model
model.fit(X_train, y_train, epochs=100, batch_size=128)
                                        ^^^^^^^^^^^^^^

Deep Learning เบื้องต้นด้วย Keras

Deep Learning เบื้องต้นด้วย Keras

Deep Learning เบื้องต้นด้วย Keras

Deep Learning เบื้องต้นด้วย Keras

ข้อดีของ Batch Normalization

  • ช่วยให้ gradient ไหลได้ดีขึ้น
  • รองรับ learning rate ที่สูงขึ้น
  • ลดการพึ่งพาการกำหนดค่าเริ่มต้นของน้ำหนัก
  • ทำหน้าที่เป็น regularization โดยไม่ตั้งใจ
  • จำกัด internal covariate shift
Deep Learning เบื้องต้นด้วย Keras

Batch Normalization ใน Keras

# Import BatchNormalization from keras layers
from tensorflow.keras.layers import BatchNormalization

# Instantiate a Sequential model model = Sequential()
# Add an input layer model.add(Dense(3, input_shape=(2,), activation = 'relu'))
# Add batch normalization for the outputs of the layer above model.add(BatchNormalization())
# Add an output layer model.add(Dense(1, activation='sigmoid'))
Deep Learning เบื้องต้นด้วย Keras

มาฝึกกันเถอะ!

Deep Learning เบื้องต้นด้วย Keras

Preparing Video For Download...