같은 계산을 중간 결과를 저장해 두고, 같은 계산을 반복하지 않는다.
머신러닝 실험에서는 다음 항목에서 사용될 수 있고, 이런것들이 매번 반복되면 매우 비효율적이다.
다음의 문제 상황에 cache를 적용 해 볼 수 있다.
이로 인해서, 실험 하나 돌리는 데 몇 시간~며칠이 소요된다.
또한,
“변하지 않는 것은 무조건 저장하고 재사용한다.”
일반적으로:
splits/
├── train_idx.npy
├── val_idx.npy
└── cv_folds.pkl
embeddings/
├── encoderA.npy
├── encoderB.npy
tuning_results.csv
예:
cache/
├── embed_pubhealth_encoder=bge_base.npy
├── retr_pubhealth_encoder=bge_base_top100.pkl
이 패턴으로 실험 속도 체감이 완전 달라진 것을 알 수 있음
import os
import pickle
def load_or_compute(path, compute_fn):
if os.path.exists(path):
with open(path, "rb") as f:
return pickle.load(f)
else:
result = compute_fn()
with open(path, "wb") as f:
pickle.dump(result, f)
return result
#사용 예:
embeddings = load_or_compute(
"cache/embeddings_encoderA.pkl",
lambda: compute_embeddings(data, encoderA)
)
튜닝할 때는:
중간 결과를 cache, 점수 계산만 반복
튜닝 비용이 N배 → 1배 + α
예:
embedding (1회)
→ retrieval (1회)
→ scoring (N회)
(논문 관점)
Cache를 잘 쓰면: