我正在尝试在CIFAR10数据集上训练一个非常基本的CNN并收到以下错误: AttributeError:' CrossEntropyLoss'对象没有属性'向后'
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
答案 0 :(得分:0)
问题已解决。我的错,我错过了括号
criterion =nn.CrossEntropyLoss()
答案 1 :(得分:0)
通常,如果您使用 l = loss(net(X), y)
,那么您会调用 l.backward()
而不是 loss.backward()
,这是我的错误。
#defining loss
loss = nn.CrossEntropyLoss()
...
#inside training loop
l = loss(net(X), y)
...
# for backpropogation
l.backward()