본문 바로가기

오늘의 코딩/머신러닝,Deep Learning

pytorch transfer learning 모델 설정 중 에러..

1.

 

Traceback (most recent call last):
  File "/home/pirl/PycharmProjects/untitled2/SVHN_transfer_test.py", line 285, in 
    num_epochs=25)
  File "/home/pirl/PycharmProjects/untitled2/SVHN_transfer_test.py", line 189, in train_model
    loss = criterion(outputs, labels)
  File "/home/pirl/PycharmProjects/untitled2/venv/lib/python3.6/site-packages/torch/nn/modules/module.py", line 532, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/pirl/PycharmProjects/untitled2/venv/lib/python3.6/site-packages/torch/nn/modules/loss.py", line 916, in forward
    ignore_index=self.ignore_index, reduction=self.reduction)
  File "/home/pirl/PycharmProjects/untitled2/venv/lib/python3.6/site-packages/torch/nn/functional.py", line 2021, in cross_entropy
    return nll_loss(log_softmax(input, 1), target, weight, None, ignore_index, None, reduction)
  File "/home/pirl/PycharmProjects/untitled2/venv/lib/python3.6/site-packages/torch/nn/functional.py", line 1836, in nll_loss
    .format(input.size(0), target.size(0)))
ValueError: Expected input batch_size (64) to match target batch_size (4).

 

layer0 fin
tensor reshape
layer1 fin
torch.Size([64, 24])
outputs torch.Size([64, 24])
label torch.Size([4])

 

loss 계산할 때, data batch 사이즈 중에서 계산해야 하는데, 데이터 사이즈가 맞지 않아 발생하는 문제.

 

-> 해결: data loader 할 때 배치 사이즈를 train 할 때의 배치 사이즈와 통일한다.

 

2.

Traceback (most recent call last):
  File "/home/pirl/PycharmProjects/untitled2/SVHN_transfer_test.py", line 285, in 
    num_epochs=25)
  File "/home/pirl/PycharmProjects/untitled2/SVHN_transfer_test.py", line 184, in train_model
    outputs = model(inputs)
  File "/home/pirl/PycharmProjects/untitled2/venv/lib/python3.6/site-packages/torch/nn/modules/module.py", line 532, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/pirl/PycharmProjects/untitled2/SVHN_transfer_test.py", line 77, in forward
    out = self.layer1(out)
  File "/home/pirl/PycharmProjects/untitled2/venv/lib/python3.6/site-packages/torch/nn/modules/module.py", line 532, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/pirl/PycharmProjects/untitled2/venv/lib/python3.6/site-packages/torch/nn/modules/container.py", line 100, in forward
    input = module(input)
  File "/home/pirl/PycharmProjects/untitled2/venv/lib/python3.6/site-packages/torch/nn/modules/module.py", line 532, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/pirl/PycharmProjects/untitled2/venv/lib/python3.6/site-packages/torch/nn/modules/linear.py", line 87, in forward
    return F.linear(input, self.weight, self.bias)
  File "/home/pirl/PycharmProjects/untitled2/venv/lib/python3.6/site-packages/torch/nn/functional.py", line 1370, in linear
    ret = torch.addmm(bias, input, weight.t())
RuntimeError: size mismatch, m1: [64 x 512], m2: [32 x 500] at /pytorch/aten/src/THC/generic/THCTensorMathBlas.cu:290

 

모델에 들어가는 레이어 크기가 일치하지 않아서 생기는 문제.

 

layer0  : m1 is [a x b] which is [batch size x in features]

layer1 : m2 is [c x d] which is [in features x out features]

 

해결: b 와 c를 맞춰줘야 함.

 

3.