gpt4 book ai didi

python - 属性错误 : 'CrossEntropyLoss' object has no attribute 'backward'

转载 作者:太空宇宙 更新时间:2023-11-04 00:25:52 24 4
gpt4 key购买 nike

我正在尝试在 CIFAR10 数据集上训练一个非常基本的 CNN,但出现以下错误:AttributeError: 'CrossEntropyLoss' 对象没有属性 '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

最佳答案

问题已解决。我的错误,我漏掉了括号

criterion = nn.CrossEntropyLoss()

关于python - 属性错误 : 'CrossEntropyLoss' object has no attribute 'backward' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47488598/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com