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
Issue resolved. My mistake, I was missing the parenthesis
criterion = nn.CrossEntropyLoss()
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With