Python 运行时错误:“;尺寸超出范围(预计在[-1,0]范围内,但得到1)”;
我希望你们一切都好。我是PyTorch的新手,我在网上遇到了一个代码,其中包含以下代码:Python 运行时错误:“;尺寸超出范围(预计在[-1,0]范围内,但得到1)”;,python,deep-learning,conv-neural-network,pytorch,Python,Deep Learning,Conv Neural Network,Pytorch,我希望你们一切都好。我是PyTorch的新手,我在网上遇到了一个代码,其中包含以下代码: cuda = True if torch.cuda.is_available() else False num_class = opt.num_class num_repeats = opt.num_repeats total=0 correct=0 acc=[] acc1=[] model = SHMnet(num_class) # model = AlexNet1D(num_class) learnin
cuda = True if torch.cuda.is_available() else False
num_class = opt.num_class
num_repeats = opt.num_repeats
total=0
correct=0
acc=[]
acc1=[]
model = SHMnet(num_class)
# model = AlexNet1D(num_class)
learning_rate = opt.lr
print(model)
loss = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate,betas=(opt.b1,opt.b2))
if cuda:
model=model.cuda()
loss=loss.cuda()
FloatTensor = torch.cuda.FloatTensor if cuda else torch.FloatTensor
LongTensor = torch.cuda.LongTensor if cuda else torch.LongTensor
traindataset = []
for label in range(num_class):
epdataset = epDataset(label,num_repeats)
traindataset = ConcatDataset([traindataset,epdataset])
testdataset =[]
for label in range(num_class):
eptdataset = eptDataset(label,6)
testdataset = ConcatDataset([testdataset,eptdataset])
dataloader = DataLoader(traindataset, batch_size=opt.batch_size)
viddataloader = DataLoader(testdataset,batch_size = opt.batch_size)
loss_r=[]
start = time.time()
for epoch in range(opt.n_epochs):
print('Epoch: %d' % epoch)
total = 0
correct = 0
total1 = 0
correct1 = 0
for i, (sdata, label) in enumerate(dataloader):
batch_size = sdata.shape[0]
sdata = Variable(sdata.type(FloatTensor))
noise=Variable(torch.from_numpy(np.random.normal(1, 0.2, sdata.shape))).type(FloatTensor)
x = torch.mul(sdata,noise).view(sdata.size(0), 1, sdata.size(1))
print(x)
print(x.size())
label = Variable (label.type(LongTensor))
print(label)
optimizer.zero_grad()
output = model(x)
print(output)
print(output.size())
loss1 = loss(input=output, target=label)
loss1.backward()
optimizer.step()
_, predicted = torch.max(model(x).data, 1)
temp = (predicted == label).sum()
correct += temp
total += batch_size
print(i, loss1.item())
loss_r.append(loss1.item())
print('Accuracy of training data is: %d %%' % (100 * correct / total))
acc.append(100 * correct / total)
我设法找到了一个数据集,但在运行代码时出现了以下错误:
RuntimeError: “dimension out of range (expected to be in range of [-1, 0], but got 1)”
所以,我不理解这个错误。原因是什么?如何解决
批量大小为60,类数为6
编辑1
输出和标签变量的输出为:
Labels: tensor([0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5])
Labels Size/Shape: torch.Size([12])
torch.Size([12, 512, 2])
Output: tensor([0.0288, 0.0125, 0.0104, 0.0222, 0.0106, 0.0121], grad_fn=<AddBackward0>)
Output Size/Shape: torch.Size([6])
Input Size/Shape: torch.Size([6, 1, 2192])
Labels: tensor([0, 1, 2, 3, 4, 5])
Labels Size/Shape: torch.Size([6])
torch.Size([6, 512, 2])
Output: tensor([ 0.0228, -0.0088, 0.0047, 0.0224, 0.0146, 0.0357], grad_fn=<AddBackward0>)
Output Size/Shape: torch.Size([6])
标签:张量([0,0,1,1,2,2,3,3,4,4,5,5])
标签尺寸/形状:火炬。尺寸([12])
火炬尺寸([12,512,2])
输出:张量([0.0288,0.0125,0.0104,0.0222,0.0106,0.0121],梯度fn=)
输出尺寸/形状:火炬。尺寸([6])
编辑2
我设法将批量大小更改为6,但是,输出和标签变量的输出为:
Labels: tensor([0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5])
Labels Size/Shape: torch.Size([12])
torch.Size([12, 512, 2])
Output: tensor([0.0288, 0.0125, 0.0104, 0.0222, 0.0106, 0.0121], grad_fn=<AddBackward0>)
Output Size/Shape: torch.Size([6])
Input Size/Shape: torch.Size([6, 1, 2192])
Labels: tensor([0, 1, 2, 3, 4, 5])
Labels Size/Shape: torch.Size([6])
torch.Size([6, 512, 2])
Output: tensor([ 0.0228, -0.0088, 0.0047, 0.0224, 0.0146, 0.0357], grad_fn=<AddBackward0>)
Output Size/Shape: torch.Size([6])
输入大小/形状:火炬大小([6,1,2192])
标签:张量([0,1,2,3,4,5])
标签尺寸/形状:火炬。尺寸([6])
火炬尺寸([65125,2])
输出:张量([0.0228,-0.0088,0.0047,0.0224,0.0146,0.0357],梯度fn=)
输出尺寸/形状:火炬。尺寸([6])
但是,我得到了相同的错误,批大小是6,类的数量是6
谢谢您在哪一行收到错误?@WasiAhmad这一行:loss1=loss(输入=输出,目标=标签)打印
输出的形状(output.size()
)和label
(label.size()
)。@WasiAhmad请查看问题中的编辑,为什么您在loss时呼叫cuda?在pytorch或python中,尽量不要用保留的关键字来命名变量。现在回到你的错误,我想了一下为什么会发生这种情况,交叉熵损失期望第一个输入是形状:(batch_size,nb_类),第二个输入是形状(batch_size,1)。也许你需要检查一下