# GPU 할당 변경하기 GPU_NUM = 2# 원하는 GPU 번호 입력 device = torch.device(f'cuda:{GPU_NUM}'if torch.cuda.is_available() else'cpu') torch.cuda.set_device(device) # change allocation of current GPU
print ('# Current cuda device: ', torch.cuda.current_device()) # check
실행 결과
1
# Current cuda device: 2
4. GPU 사용을 기본값으로 설정
변수를 만들때 자동으로 GPU를 사용하게 설정합니다.
앞서 GPU_NUM으로 지정한 GPU에 할당됩니다.
1 2 3
if torch.cuda.is_available(): torch.set_default_tensor_type(torch.cuda.FloatTensor) print(f"using cuda: {GPU_NUM}, {torch.cuda.get_device_name(GPU_NUM)}")