def loss(inputs, targets): return F.crossentropy(inputs, targets,…
- User
def loss(inputs, targets): return F.cross_entropy(inputs, targets, reduction='none').mean(1).mean(1) devices = [torch.device('cpu')] #devices =d2l.try_all_gpus() num_epochs, lr, wd= 5, 0.001, 1e-3 trainer = torch.optim.SGD(net.parameters(), lr=lr, weight_decay=wd) d2l.train_ch13(net, train_iter, test_iter, loss, trainer, num_epochs, devices)出现--------------------------------------------------------------------------- RuntimeError Traceback (most recent call last) Cell In[28], line 7 5 num_epochs, lr, wd= 5, 0.001, 1e-3 6 trainer = torch.optim.SGD(net.parameters(), lr=lr, weight_decay=wd) ----> 7 d2l.train_ch13(net, train_iter, test_iter, loss, trainer, num_epochs, devices)
File d:\anaconda\Lib\site-packages\d2l\torch.py:1514, in train_ch13(net, train_iter, test_iter, loss, trainer, num_epochs, devices) 1512 for i, (features, labels) in enumerate(train_iter): 1513 timer.start() -> 1514 l, acc = train_batch_ch13( 1515 net, features, labels, loss, trainer, devices) 1516 metric.add(l, acc, labels.shape[0], labels.numel()) 1517 timer.stop()
File d:\anaconda\Lib\site-packages\d2l\torch.py:1492, in train_batch_ch13(net, X, y, loss, trainer, devices) 1490 trainer.zero_grad() 1491 pred = net(X) -> 1492 l = loss(pred, y) 1493 l.sum().backward() 1494 trainer.step()
Cell In[28], line 2 1 def loss(inputs, targets): ----> 2 return F.cross_entropy(inputs, targets, reduction='none').mean(1).mean(1) ... 3051 if size_average is not None or reduce is not None: 3052 reduction = _Reduction.legacy_get_string(size_average, reduce) -> 3053 return torch._C._nn.cross_entropy_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index, label_smoothing)
RuntimeError: only batches of spatial targets supported (3D tensors) but got targets of dimension: 4报错
- Assistant