TypeError: cross_entropy_loss()

siwon: <p>HI, Here is my code and I have some errors.<br>
I don’t know why the ‘y’ is dict.<br>
Thank you for your help in advnace <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>
<pre><code class="lang-auto">training_data = datasets.VOCDetection(
    root="/data/",
    image_set="train",
    #download=True,
    transform=transforms.Compose([transforms.Resize((1000,600)), transforms.ToTensor()])   
)

test_data = datasets.VOCDetection(
    root="/data/",
    image_set="val",
    #download=True,
    transform=transforms.Compose([transforms.Resize((1000,600)), transforms.ToTensor()])
)


train_dataloader = DataLoader(training_data, batch_size=2)
test_dataloader = DataLoader(test_data, batch_size=2)


class MyNet(nn.Module):
    def __init__(self):     
        super(MyNet, self).__init__()
        self.conv1 = nn.Conv2d(3,6,5)
        self.conv2 = nn.Conv2d(6,16,5)
        #self.conv1.in_channels = 1

        self.fc1 = nn.Linear(580944, 120) 
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 10)

    def forward(self, x): 
        x = F.max_pool2d(F.relu(self.conv1(x)), (2,2))
        x = F.max_pool2d(F.relu(self.conv2(x)), 2)
        x = torch.flatten(x, 1)     
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x    # output

model = MyNet()

learning_rate = 1e-3
batch_size = 64
epochs = 5

loss_fn = nn.CrossEntropyLoss()

# optimizer
optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)

# Define training and test
def train_loop(dataloader, model, loss_fn, optimizer):
    size = len(dataloader.dataset)
    for batch, (X, y) in enumerate(dataloader):
        # calculate prediction and loss
        pred = model(X)
        #y = torch.as_tensor(y)
        #y = torch.Tensor(y)
        loss = loss_fn(pred, y) # --&gt;&gt; this line occurs error

        # backward propagation
        optimizer.zero_grad()
        loss.backward()
        optimizer.step() 

        if batch % 100 == 0:
            loss, current = loss.item(), batch * len(X)
            print(f"loss : {loss:&gt;7f} [{current:&gt;5d}/{size:&gt;5d}]")

def test_loop(dataloader, model, loss_fn):
    size = len(dataloader.dataset)
    num_batches = len(dataloader)
    test_loss, correct = 0, 0

    with torch.no_grad():
        for X, y in dataloader:
            pred = model(X)
            test_loss += loss_fn(pred, y).item()
            correct += (pred.argmax(1) == y).type(torch.float).sum().item()

    test_loss /= num_batches
    correct /= size
    print(f"Test Error: \n Accuracy: {(100*correct):&gt;0.1f}%, Avg loss: {test_loss:&gt;8f} \n")


# reset
loss_fn = nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)


# training
epochs = 10
for t in range(epochs):
    print(f"Epoch {t+1}\n-------------------------------")
    train_loop(train_dataloader, model, loss_fn, optimizer)
    test_loop(test_dataloader, model, loss_fn)
</code></pre>
<p>Here is my error:</p>
<pre><code class="lang-auto">  File "model.py", line 105, in train_loop
    loss = loss_fn(pred, y)
TypeError: cross_entropy_loss(): argument 'target' (position 2) must be Tensor, not dict
</code></pre>

blueeagle: <aside class="quote no-group" data-username="siwon" data-post="1" data-topic="148702">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/siwon/48/48463_2.png" class="avatar"> siwon:</div>
<blockquote>
<p>I don’t know why the ‘y’ is dict.</p>
</blockquote>
</aside>
<p>If you take a look at the <a href="https://pytorch.org/vision/stable/generated/torchvision.datasets.VOCDetection.html" rel="noopener nofollow ugc">documentation</a> of the VOCDection dataset you use, you can see that it the <code>__getitem__</code> method returns the following values:</p>
<blockquote>
<p>(image, target) where target is a dictionary of the XML tree</p>
</blockquote>
<p>So after this line<br>
<code>for batch, (X, y) in enumerate(dataloader):</code><br>
<code>X</code> is an image and <code>y</code> a dictionary.</p>
<p>I am not sure what this dataset is exactly about, but I would recommend you take a detailed look at your dataset in order to fully understand what the labels are and how you should use them <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>

mMagmer: <p>hi,<br>
<code>VOCDetection</code> dataset is benchmark for object detection task.<br>
each image has multiple object in it.<br>
base on the code you’re trying  to do classifcation.</p>