Только что появился мой старый шаблон для базовых c MLP, и я немного заржавел. Я использую CELoss в MNIST (преобразованные изображения в тензорах).
Я пытался откатить, чтобы увеличить размер пакета, но он действительно настаивает на dim = 3 для целевого тензора. Если бы кто-то мог также объяснить, почему он теперь требует этого, это будет оценено.
PS. Похоже, связанный с постом ниже, который не ответил здесь.
Мой код
train_dataset = datasets.MNIST('../', download=True, transform=transforms.ToTensor())
test_dataset = datasets.MNIST('../', train=False, download=True, transform=transforms.ToTensor())
indices = list(range(len(train_dataset.data)))
indices = list(range(len(test_dataset.data)))
random.shuffle(indices)
train_loader = torch.utils.data.DataLoader(train_dataset,
batch_size=batchSize,
num_workers=1)
test_loader = torch.utils.data.DataLoader(test_dataset,
batch_size=batchSize,
num_workers=1)
# Surrogate loss used for training
loss_fn = nn.CrossEntropyLoss()
test_loss_fn = nn.CrossEntropyLoss(reduction='sum')
optimizer = optim.Adam(model.parameters(), lr=lr)
#optimizer = optim.SGD(model.parameters(), lr=lr ,weight_decay=weight_decay)
print('Training beginning...')
start_time = time.time()
for epoch in range(1, nbr_epochs+1):
print('Epoch ', epoch, ':')
train(model, train_loader, optimizer, epoch,loss_fn)
loss, acc = test(model, test_loader)
test_accuracy.append(acc)
train_loss.append(loss)
def train(model,train_loader, optimizer, epoch, loss_fn):
model.train()
for batch_idx, (inputs, target) in enumerate(train_loader):
#inputs = inputs.view(batchSize, 1,100,100)
inputs, target = inputs.to(device), target.to(device)
optimizer.zero_grad()
output = model(inputs)
loss = loss_fn(output, target.unsqueeze(1))
# Backprop
loss.backward()
optimizer.step()
И ошибка:
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-31-c6a8cb2b8f18> in <module>
22 for epoch in range(1, nbr_epochs+1):
23 print('Epoch ', epoch, ':')
---> 24 train(model, train_loader, optimizer, epoch,loss_fn)
25 loss, acc = test(model, test_loader)
26
<ipython-input-29-d78d7fdaeb4d> in train(model, train_loader, optimizer, epoch, loss_fn)
10 optimizer.zero_grad()
11 output = model(inputs)
---> 12 loss = loss_fn(output, target.unsqueeze(1))
13
14 # Backprop
~/.local/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
545 result = self._slow_forward(*input, **kwargs)
546 else:
--> 547 result = self.forward(*input, **kwargs)
548 for hook in self._forward_hooks.values():
549 hook_result = hook(self, input, result)
~/.local/lib/python3.6/site-packages/torch/nn/modules/loss.py in forward(self, input, target)
914 def forward(self, input, target):
915 return F.cross_entropy(input, target, weight=self.weight,
--> 916 ignore_index=self.ignore_index, reduction=self.reduction)
917
918
~/.local/lib/python3.6/site-packages/torch/nn/functional.py in cross_entropy(input, target, weight, size_average, ignore_index, reduce, reduction)
1993 if size_average is not None or reduce is not None:
1994 reduction = _Reduction.legacy_get_string(size_average, reduce)
-> 1995 return nll_loss(log_softmax(input, 1), target, weight, None, ignore_index, None, reduction)
1996
1997
~/.local/lib/python3.6/site-packages/torch/nn/functional.py in nll_loss(input, target, weight, size_average, ignore_index, reduce, reduction)
1824 ret = torch._C._nn.nll_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index)
1825 elif dim == 4:
-> 1826 ret = torch._C._nn.nll_loss2d(input, target, weight, _Reduction.get_enum(reduction), ignore_index)
1827 else:
1828 # dim == 3 or dim > 4
RuntimeError: invalid argument 3: only batches of spatial targets supported (3D tensors) but got targets of dimension: 2 at /pytorch/aten/src/THNN/generic/SpatialClassNLLCriterion.c:61
Где ввод в train()
для l oop есть форма (32,1,28,28) и проблематично c target is (32,1)