C.W.K.
Stream
Lesson 03 of 07 · published

Weight 만 저장 / 로드

~8 min · serialize

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

architecture 빼고 weight 만

가끔은 모델 전체가 아니라 학습된 숫자만 필요할 때가 있어. save_weights 는 tensor 만 쓰고 architecture 는 통째로 빼. 그래서 파일이 작고 저장이 빨라 — 긴 학습 도중에 몇 epoch 마다 checkpoint 찍을 때 딱 이거야.

함정: architecture 는 직접 다시 빌드

파일에 architecture 가 없으니까 로드는 2 단계 춤이야: 코드로 모델을 먼저 만들고, 거기에 load_weights 로 weight 를 부어. 이때 만드는 모델이 저장된 거랑 layer 하나하나 똑같아야 해 — shape 도 순서도. 안 맞으면 로드가 시끄럽게 터지거나, 더 나쁘게는 조용히 tensor 가 어긋나. 이게 .keras 대비 trade-off 야: self-describing 안전성을 크기·속도랑 맞바꾸고, build 코드를 동기화 상태로 유지할 책임을 네가 떠안는 거지.

거대 모델은 shard 로

아주 큰 모델은 weight 파일 하나가 감당이 안 돼. Keras 3.10+ 는 max_shard_size 로 shard 크기를 제한할 수 있어 — "2GB" 넘기면 weight 가 여러 파일로 자동 분할돼서 파일시스템·업로드 한도 밑으로 떨어져. 로드할 땐 알아서 다시 합쳐.

Code

weight 저장 / 재빌드 / 로드 + 큰 모델 shard·python
# Save weights
model.save_weights("weights.weights.h5")

# Load weights into an identical architecture
new_model = build_model()  # Must match original architecture
new_model.load_weights("weights.weights.h5")

# Weight sharding for large models (Keras 3.10+)
model.save_weights("large_model.weights.h5", max_shard_size="2GB")

External links

Exercise

model 10 epoch 학습. ModelCheckpoint 으로 매 2 epoch weight 저장. 학습 후 epoch 6 weight 를 같은 architecture 의 새 instance 에 로드. epoch 6 에 logged 된 accuracy 와 일치 확인.

Progress

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

댓글 0

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

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