AttributeError: 'CrossEntropyLoss' object has no attribute 'backward'
Asked Answered
H

2

9

i am trying to train a very basic CNN on CIFAR10 data set and getting the following error : AttributeError: 'CrossEntropyLoss' object has no attribute 'backward'

criterion =nn.CrossEntropyLoss
optimizer=optim.SGD(net.parameters(),lr=0.001,momentum=0.9)

for epoch in range(2):  # loop over the dataset multiple times
        running_loss = 0.0
        for i, data in enumerate(trainloader, 0):
            # get the inputs
            inputs, labels = data
             # wrap them in Variable
            inputs, labels = Variable(inputs), Variable(labels)

            # zero the parameter gradients
            optimizer.zero_grad()
            # forward + backward + optimize
            outputs = net(inputs)
            loss = criterion(outputs, labels)
            loss.backward()
            optimizer.step()
            # print statistics
            running_loss += loss.data[0]
            if i % 2000 == 1999:    # print every 2000 mini-batches
                print('[%d, %5d] loss: %.3f' %
                      (epoch + 1, i + 1, running_loss / 2000))
                running_loss = 0.0
Highoctane answered 25/11, 2017 at 17:15 Comment(0)
H
12

Issue resolved. My mistake, I was missing the parenthesis

criterion = nn.CrossEntropyLoss()

 

Highoctane answered 25/11, 2017 at 17:27 Comment(1)
That got me too. I really dont like the auto-complete of pytorchCecilececiley
V
4

Usually if you use l = loss(net(X), y) then you call l.backward() and not loss.backward() that was the error in my case.

#defining loss
loss = nn.CrossEntropyLoss()
...
#inside training loop
l = loss(net(X), y)
...
# for backpropogation
l.backward()
Vinosity answered 6/8, 2021 at 1:25 Comment(1)
This was it for me xD. Ugh. Thank you!Intellectual

© 2022 - 2024 — McMap. All rights reserved.