딥러닝에서 적절한 모델 복잡도 지점 찾기: train err와 validation err 비교
조절 대상
- Epochs
- learning_rate
- Early Stopping
- Drop out
- 가중치 규제
EalryStopping
- min_delta: 손실 범위 설정, 해당 값 이하로 떨어지지 않으면 count
- patience: min_delta에 충족하는 게 patience 값만큼 반복되면 stop
# 함수 불러오기
frim keras.callbacks import EarlyStopping
# 미리 멈춤 지정
es = EarlyStopping(monitor = 'val_loss', min_delta = 0.001, patience = 5)
# 훈련
model.fit(x_trian, y_train, epochs = 100, validation_split = 0.2, callbacks = [es])
Dropout: 과적합을 줄이는 정규화 기법 중 하나, 훈련 과정에서 일부 뉴런을 임의로 비활성화, 0.2~0.5로 설정(변수가 작으면 낮추고 많으면 높임)
# 메모리 정리
clear_session()
# Sequential 타입
model3 = Sequential( [Input(shape = (nfeatures,)),
Dense(128, activation= 'relu'),
Dropout(0.4),
Dense(64, activation= 'relu'),
Dropout(0.4),
Dense(32, activation= 'relu'),
Dropout(0.4),
Dense(1, activation= 'sigmoid')] )
# 컴파일
model3.compile(optimizer= Adam(learning_rate = 0.001), loss='binary_crossentropy')
모델 저장
# 모델 저장
model1.save('hanky.keras')
# 모델 불러오기
from keras.models import load_model
model2 = load_model('hanky.keras')
# 모델 사용
pred = model2.predict(x_val)
체크포인트
# 함수 불러오기
from keras.callbacks import ModelCheckpoint
# 미리 멈춤 설정
es = EarlyStopping(monitor = 'val_loss', min_delta = 0.001, patience = 5)
# 모델 체크포인트 설정
cp_path = '/content/{epoch:03d}.keras' # Keras 2.11 이상 버전에서 모델 확장자 .keras
mcp = ModelCheckpoint(cp_path, monitor='val_loss', verbose = 1, save_best_only=True)
# cp_path: 모델 저장 경로와 파일 이름
# monitor='val_loss': validation loss를 기준
# save_best_only=True: 이전 체크포인트 모델들보다 성능이 개선되면 저장
# 학습
model1.fit(x_train, y_train, epochs = 50, validation_split=.2, callbacks=[mcp, es])
가중치 규제
- L1: Lasso, 가중치 절대값의 합 최소화 --> 가중치가 작은 값들은 0으로 만드는 경향
- L2: Ridge, 가중치 제곱의 합을 최소화 --> 규제 강도에 따라 가중치 영향력 제어, 작은 가중치는 0에 수렴
# 규제를 위해 필요한 함수 불러오기
from keras.regularizers import l1, l2
# 메모리 정리
clear_session()
# L1 규제
model4 = Sequential( [Input(shape = (nfeatures,)),
Dense(128, activation= 'relu',
kernel_regularizer = l1(0.01)),
Dense(64, activation= 'relu',
kernel_regularizer = l1(0.01)),
Dense(32, activation= 'relu',
kernel_regularizer = l1(0.01)),
Dense(1, activation= 'sigmoid')] )
# L2 규제
model5 = Sequential( [Input(shape = (nfeatures,)),
Dense(128, activation= 'relu',
kernel_regularizer = l2(0.05)),
Dense(64, activation= 'relu',
kernel_regularizer = l2(0.05)),
Dense(32, activation= 'relu',
kernel_regularizer = l2(0.05)),
Dense(1, activation= 'sigmoid')] )