Wait, shouldnt loss fucntion take straigh output without argmax? Because now i got error:
ValueError Traceback (most recent call last)
Cell In[22], line 5
3 for epoch in range(epochs):
4 start = time.time()
----> 5 train_loss, train_acc = train(train_loader, device, model, optimizer, criterion)
6 stop = time.time()
7 print(f"Epoch: {epoch+1}. Time: {stop-start:.2f}")
Cell In[21], line 21, in train(train_loader, device, model, optimizer, criterion)
19 optimizer.zero_grad()
20 out = model(data) # [N, num_classes]
---> 21 loss = criterion(out, data.y)
22 loss.backward()
23 optimizer.step()
File E:\AI\Grafy.venv\Lib\site-packages\torch\nn\modules\module.py:1751, in Module._wrapped_call_impl(self, *args, **kwargs)
1749 return self._compiled_call_impl(*args, **kwargs) # type: ignore[misc]
1750 else:
-> 1751 return self._call_impl(*args, **kwargs)
File E:\AI\Grafy.venv\Lib\site-packages\torch\nn\modules\module.py:1762, in Module._call_impl(self, *args, **kwargs)
1757 # If we don't have any hooks, we want to skip the rest of the logic in
1758 # this function, and just call forward.
1759 if not (self._backward_hooks or self._backward_pre_hooks or self._forward_hooks or self._forward_pre_hooks
1760 or _global_backward_pre_hooks or _global_backward_hooks
1761 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1762 return forward_call(*args, **kwargs)
1764 result = None
1765 called_always_called_hooks = set()
Cell In[20], line 26, in FocalLoss.forward(self, inputs, targets)
18 def forward(self, inputs, targets):
19 """
20 Args:
21 inputs: predictions of shape (N, *) (logits).
(...) 24 Loss value.
25 """
---> 26 BCE_loss = F.binary_cross_entropy_with_logits(inputs, targets, reduction='none')
27 probas = torch.sigmoid(inputs)
28 p_t = probas * targets + (1 - probas) * (1 - targets)
File E:\AI\Grafy.venv\Lib\site-packages\torch\nn\functional.py:3639, in binary_cross_entropy_with_logits(input, target, weight, size_average, reduce, reduction, pos_weight)
3636 reduction_enum = _Reduction.get_enum(reduction)
3638 if not (target.size() == input.size()):
-> 3639 raise ValueError(
3640 f"Target size ({target.size()}) must be the same as input size ({input.size()})"
3641 )
3643 return torch.binary_cross_entropy_with_logits(
3644 input, target, weight, pos_weight, reduction_enum
3645 )
ValueError: Target size (torch.Size([11823])) must be the same as input size (torch.Size([11823, 5]))
And i belive it should be okay since cross entropy loss worked fine before