Pytorch
-
[이거 어떡하지] Tensor for ' ' is on CPU, Tensor for argument #1 is on CPU, but expected them to be on GPUPytorch 2022. 8. 3. 23:13
증상: GPU환경에서 pytorch로 학습하는데 모델 선언하고 학습하는 때에 제목과 같은 에러가 발생합니다. 아마 주로 RNN 계열을 학습시킬 때 자주 실수하실 부분이라 생각됩니다. 아래와 같은 문제가 발생했을 때 어떻게 하면 될까요? 이 에러가 무슨 말이냐면 1. model 입력은(tensor) CPU에 있는데 2. model은 GPU에 있어! 아마 RNN 계열을 학습시킬 때 hidden state 변수를 선언하실 때 실수하는 경우가 많아서(제가) 이 에러를 종종 보곤 합니다. 해결 방법 입력 tensor를 .to(device)나 .cuda()로 GPU에 올려주시면 됩니다.
-
[이거 어떡하지] torch.load 했는데 'model' object has no attribute 'copy' 뜰 때Pytorch 2021. 8. 6. 12:49
증상: raise ModuleAttributeError("'{}' object has no attribute '{}'".format(torch.nn.modules.module.ModuleAttributeError: 'model' object has no attribute 'copy' 해결 방법 요약: 1. torch.save() 오사용 2. torch.save(model.state_dict(), "path.pt")로 model을 save 해야 함. torch.save(model, "path.pt")로 모델을 저장하지 않았는지 확인해서 재학습 해설: 'error를 살펴보면, model은 copy라는 attribute(method 혹은 class 변수)가 없다.'라고 합니다. pytorch로 딥러닝 모델을 ..
-
[이거 어떡하지] Expected target size (, ), got torch.Size([, ]) (nn.CrossEntropyLoss 에서)Pytorch 2021. 6. 18. 22:23
증상: ValueError: Expected target size(256, 84), got torch.Size([256, 64]) image classification이나 language model을 학습시키는 과정에서 nn.CrossEntropyLoss 를 사용하실 때 위와 같은 에러가 발생할 때가 있습니다. (특히 language model처럼 batch 한 샘플마다 sequence별로 classification을 해야 하는 경우) sequence sample별로 output으로 num_classes만큼의 dimension이 뽑히게 냈는데 dimension이 안맞으면 에러가 발생할 수 있습니다. language model의 경우에는 입력으로 [batch_size, len_input] 의 shape를 ..
-
[이거 어떡하지] optim in step: expected device cpu but got device cuda:0Pytorch 2021. 6. 11. 23:08
증상: optim.step() - RuntimeError: expected device cpu but got device cuda:0 1. pytorch에서 코딩하는데 난 model도 cuda에 올렸고 (Model().cuda() 혹은 Model().to(device)를 사용) 2. torch.load({CHECKPOINT_PATH})로 optimzer의 state를 불러왔는데 아래와 같이 optimizer의 step()에서 이러한 증상이 나타납니다. 왜 그럴까요? 해결 방법: 1] optim_state = torch.load({CHECKPOINT_PATH})["optimizer"] 2] optim.load_state_dict(optim_state) 3] for state in optim.state.va..
-
[이거 어떡하지] RuntimeError: CUDA error: CUBLAS_STATUS_ALLOC_FAILED when calling `cublasCreate(handle)`원인 및 해결법(pytorch)Pytorch 2021. 6. 5. 03:02
pytorch로 딥러닝 모델을 설계후 학습을 진행하다보면 RuntimeError: CUDA error: CUBLAS_STATUS_ALLOC_FAILED when calling `cublasCreate(handle)` 라는 메시지가 발생할 때가 있습니다. CUDA 관련된 문제로는 속썩을 만한 타이밍이 아닐때 해당 코드를 보게 되어 난처하실 경우가 많을 것입니다. 원인 및 해결책: GPU로 학습하는 환경에서 설계하신 모델에 torch.nn.Embedding()이 포함되어 있으실 것입니다. torch.nn.Embedding(num_input, output_dimension) num_input이 입력하는 token의 종류 수가 될 것인데 예를 들어 num_input이 128인경우 입력으로 0~127까지의 int..