Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
31 |
Tags
- C
- label
- 기타 연주
- Visual Studio
- C#
- nvidia-smi
- windows forms
- paramiko
- pandas
- SSH
- error
- 컨테이너
- JSON
- OpenCV
- pip
- Numpy
- 프로그래머스
- C++
- 핑거스타일
- 채보
- mysql
- VS Code
- Selenium
- 오류
- ubuntu
- Python
- YOLO
- Linux
- Docker
- pytorch
Archives
- Today
- Total
기계는 거짓말하지 않는다
PyTorch Seed Settings 본문
딥러닝 실험을 할 때, 결과의 재현성은 매우 중요하다.
실험의 일관성을 유지하기 위해서는 동일한 조건에서 실험을 반복할 수 있어야 한다.
이를 위해 랜덤 시드를 고정하는 방법을 사용할 수 있고, 아래는 랜덤 시드를 고정하는 함수와 모델을 로드하는 예시이다.
import torch
import random
import numpy as np
def set_seed(seed=42):
random.seed(seed) # random module 시드 고정
np.random.seed(seed) # numpy 랜덤 시드 고정
torch.manual_seed(seed) # PyTorch CPU 랜덤 시드 고정
torch.cuda.manual_seed_all(seed) # 멀티 GPU 환경 동일한 결과 보장
torch.backends.cudnn.deterministic = True # CNN 사용 시 재현성 보장
torch.backends.cudnn.benchmark = False # 성능 최적화 대신 동일한 결과를 우선
set_seed(42) # 시드 값 설정
# 모델 로드
model = torch.load("model.pth", map_location="cpu")
model.eval() # 평가 모드
딥러닝에서 랜덤성은 훈련 과정에서 여러 부분에 영향을 미친다.
가중치 초기화, 데이터 셔플, 배치 처리 등의 과정에서 랜덤성이 포함된다.
이러한 요소들은 실험을 다시 실행할 때마다 결과가 달라질 수 있다.
따라서 시드를 고정하는 것은 실험을 재현 가능한 상태로 만들어 주며,
연구나 논문 작성 시 결과의 신뢰성을 높이는 데 중요한 역할을 한다.
'AI' 카테고리의 다른 글
PyTorch Windows 환경 DataLoader num_workers (0) | 2025.05.22 |
---|---|
PyTorch forward 병렬 처리, 연산 그래프 (0) | 2025.03.31 |
torchvision transforms Resize의 size 매개변수 (0) | 2024.09.26 |
PyTorch 경고 Creating a tensor from a list of numpy.ndarrays is extremely slow (0) | 2024.07.18 |
torch Could not load library libcudnn_cnn_train.so.8. Error 오류 (0) | 2024.07.03 |
Comments