gpt4 book ai didi

python - 运行时错误: Assertion `cur_target >= 0 && cur_target < n_classes' failed

转载 作者:行者123 更新时间:2023-11-30 08:48:19 27 4
gpt4 key购买 nike

我得到:

RuntimeError: Assertion `cur_target >= 0 && cur_target < n_classes' failed. at /opt/conda/conda-bld/pytorch_1550796191843/work/aten/src/THNN/generic/ClassNLLCriterion.c:93

运行此代码时:

    criterion = nn.CrossEntropyLoss()
#Define the optimizer
optimizer=optim.SGD(net.parameters(),lr=0.01,momentum=0.9)
epochs=20
for epoch in range(epochs):
print ("epoch #", epoch)
running_loss=0.0
for i, data in enumerate(train_loader,0):
inputs,labels=data
inputs,labels= inputs.to(device),labels.to(device)
optimizer.zero_grad()
#train
output=net(inputs)
loss=criterion(output,labels)

print ("loss: ", loss.item())
running_loss+=loss.item()
loss.backward()
optimizer.step()
print ('Finished Training')

最佳答案

异常表示您的标签之一超出范围。也许他们从 1 而不是 0 开始?尝试将它们打印出来。

关于python - 运行时错误: Assertion `cur_target >= 0 && cur_target < n_classes' failed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55981101/

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