When I run the program below, it gives me an error. The problem seems to be in the loss function but I can’t find it. I have read the Pytorch Documentation for nn.CrossEntropyLoss but still can’t find the problem.
Image size is (1 x 256 x 256), Batch size is 1
I am new to PyTorch, thanks.
JavaScript
x
39
39
1
import torch
2
import torch.nn as nn
3
from PIL import Image
4
import numpy as np
5
torch.manual_seed(0)
6
7
x = np.array(Image.open("cat.jpg"))
8
x = np.expand_dims(x, axis = 0)
9
x = np.expand_dims(x, axis = 0)
10
x = torch.from_numpy(x)
11
x = x.type(torch.FloatTensor) # shape = (1, 1, 256, 256)
12
13
def Conv(in_channels, out_channels, kernel=3, stride=1, padding=0):
14
return nn.Conv2d(in_channels, out_channels, kernel, stride, padding)
15
16
class model(nn.Module):
17
def __init__(self):
18
super(model, self).__init__()
19
20
self.sequential = nn.Sequential(
21
Conv(1, 3),
22
Conv(3, 5),
23
nn.Flatten(),
24
nn.Linear(317520, 1),
25
nn.Sigmoid()
26
)
27
28
def forward(self, x):
29
y = self.sequential(x)
30
return y
31
32
def compute_loss(y_hat, y):
33
return nn.CrossEntropyLoss()(y_hat, y)
34
35
model = model()
36
y_hat = model(x)
37
38
loss = compute_loss(y_hat, torch.tensor([1]))
39
Error:
JavaScript
1
16
16
1
Traceback (most recent call last):
2
File "D:/Me/AI/Models/test.py", line 38, in <module>
3
**loss = compute_loss(y, torch.tensor([1]))**
4
File "D:/Me/AI/Models/test.py", line 33, in compute_loss
5
return nn.CrossEntropyLoss()(y_hat, y)
6
File "D:SoftwaresAnacondaenvsdeeplearninglibsite-packagestorchnnmodulesmodule.py", line 1054, in _call_impl
7
return forward_call(*input, **kwargs)
8
File "D:SoftwaresAnacondaenvsdeeplearninglibsite-packagestorchnnmodulesloss.py", line 1120, in forward
9
return F.cross_entropy(input, target, weight=self.weight,
10
File "D:SoftwaresAnacondaenvsdeeplearninglibsite-packagestorchnnfunctional.py", line 2824, in cross_entropy
11
return torch._C._nn.cross_entropy_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index)
12
**IndexError: Target 1 is out of bounds.**
13
14
Process finished with exit code 1
15
16
Advertisement
Answer
This looks like a binary classifier model: cat or not cat. But you are using CrossEntropyLoss which is used when you have more than 2 target classes. So what you should use is Binary Cross Entropy Loss.
JavaScript
1
3
1
def compute_loss(y_hat, y):
2
return nn.BCELoss()(y_hat, y)
3