본문 바로가기
C.W.K.
Stream
Lesson 02 of 07 · published

혼합 정밀도 학습

~8 min · advanced

Level 0Keras 도제
0 XP0/97 lessons0/20 achievements
0/120 XP to next level120 XP to go0% complete

두 숫자 형식을 하나의 학습 루프에서 사용해

혼합 정밀도는 행렬 곱과 합성곱처럼 계산량이 큰 연산을 float16이나 bfloat16으로 실행하고 넓은 범위가 필요한 부분은 float32로 유지해. 16비트 연산에서 속도 이득을 얻되 가중치의 주 복사본과 그래디언트 누적은 32비트로 보존해 작은 갱신이 반올림에 사라지지 않게 하지.

한 줄로 활성화할 수 있어

모델을 만들기 전에 set_global_policy('mixed_float16')를 호출하고 이후에는 평소처럼 구성하고 학습해. Keras가 알맞은 경계에 형 변환을 넣어 줘. 마지막 Dense는 softmax 없이 원시 로짓을 내고 from_logits=True와 짝지어. float16 softmax의 넘침을 피하고 손실을 수치적으로 안정되게 계산할 수 있어.

속도 이득은 하드웨어에 달려 있어

2~3배 가속은 알맞은 텐서 연산 장치가 있을 때만 가능해. Tensor Core가 없는 GPU나 CPU에서는 메모리는 줄어도 속도 이득이 거의 없고 형 변환 비용만 늘 수 있어. 가정하지 말고 반드시 실제 장비에서 측정해.

Code

mixed_float16을 켜고 로짓은 float32로 유지하기·python
# Enable mixed precision globally
keras.mixed_precision.set_global_policy("mixed_float16")

# Build and train normally — Keras handles the casting
model = keras.Sequential([
    keras.Input(shape=(784,)),
    layers.Dense(256, activation="relu"),  # Computes in float16
    layers.Dense(10),                        # Raw logits (no activation)
])

# Use from_logits for numerical stability with mixed precision
model.compile(
    optimizer="adam",
    loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
)

External links

Exercise

CIFAR-10을 mixed_float16 사용 전후로 학습해. 에포크 시간은 약 30~50% 감소하는지, 최종 정확도 차이는 약 0.5% 이내인지 비교해.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.