简体   繁体   English

IndexError:维度超出范围(预期在 [-1, 0] 范围内,但得到 1)

[英]IndexError: Dimension out of range (expected to be in range of [-1, 0], but got 1)

As I've read through some previous questions, I'm getting this error that probably has something to do with a discrepancy between dimensions of tensors but since this is my very first attempt of running PyTorch so I'm coming here because I have far to little intuition about it.当我阅读了之前的一些问题时,我收到了这个错误,这可能与张量维度之间的差异有关,但由于这是我第一次尝试运行 PyTorch,所以我来这里是因为我有很远对它的一点直觉。 Wanted to run a non-standard dataset (which I'm pretty sure I'm loading fine) on a basic MNIST setup to mess with it and see what moves what.想要在基本的 MNIST 设置上运行一个非标准数据集(我很确定我加载得很好)以弄乱它并查看什么移动了什么。

Traceback (most recent call last):

File "C:/Users/Administrator/Desktop/pytong/proj/pytorch_cnnv2.py", line 110, in <module>
loss = loss_func(output, b_y)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\torch\nn\modules\module.py", line 547, in __call__
result = self.forward(*input, **kwargs)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\torch\nn\modules\loss.py", line 916, in forward
ignore_index=self.ignore_index, reduction=self.reduction)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\torch\nn\functional.py", line 1995, in cross_entropy
return nll_loss(log_softmax(input, 1), target, weight, None, ignore_index, None, reduction)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\torch\nn\functional.py", line 1316, in log_softmax
ret = input.log_softmax(dim)

IndexError: Dimension out of range (expected to be in range of [-1, 0], but got 1)

The line making the fuss is the loss function:大惊小怪的是损失函数:

loss = loss_func(output, b_y)

The rest of the code, sans imports and load:其余的代码,没有导入和加载:

class CNNModel(nn.Module):
def __init__(self):
    super(CNNModel, self).__init__()

    # Convolution 1
    self.cnn1 = nn.Conv2d(in_channels=3, out_channels=16, kernel_size=5, stride=1, padding=0)
    self.relu1 = nn.ReLU()

    # Max pool 1
    self.maxpool1 = nn.MaxPool2d(kernel_size=2)

    # Convolution 2
    self.cnn2 = nn.Conv2d(in_channels=16, out_channels=32, kernel_size=5, stride=1, padding=0)
    self.relu2 = nn.ReLU()

    # Max pool 2
    self.maxpool2 = nn.MaxPool2d(kernel_size=2)

    # Fully connected
    self.fc1 = nn.Linear(338 * 4 * 4, 5)

def forward(self, x):
    # Convolution 1
    out = self.cnn1(x)
    print(out.shape)
    out = self.relu1(out)
    print(out.shape)

    # Max pool 1
    out = self.maxpool1(out)
    print(out.shape)

    # Convolution 2
    out = self.cnn2(out)
    print(out.shape)
    out = self.relu2(out)
    print(out.shape)

    # Max pool 2
    out = self.maxpool2(out)
    print('++++++++++++++ out')
    print(out.shape)
    # out = out.reshape(-1, 169 * 4 * 4)
    out = out.view(out.size(0), -1)
    print(out.shape)
    print('-----------------------')
    # Linear function (readout)
    out = self.fc1(out)
    print(out.shape)
    print('=======================')

    return out



if __name__ == '__main__':

print("Number of train samples: ", len(train_data))
print("Number of test samples: ", len(test_data))
print("Detected Classes are: ", train_data.class_to_idx) # classes are detected by folder structure

model = CNNModel()
optimizer = torch.optim.Adam(model.parameters(), lr=LEARNING_RATE)
loss_func = nn.CrossEntropyLoss()

# Training and Testing
for epoch in range(EPOCHS):
    print(enumerate(train_data_loader))
    for step, (x, y) in enumerate(train_data_loader):
        b_x = Variable(x)   # batch x (image)
        b_y = Variable(y)   # batch y (target)
        # print('============ b_x')
        # print(len(b_x))
        # print(b_x.data)
        # print('============ b_y')
        # print(len(b_y))
        # print(b_y.data)

        output = model(b_x)[0]
        loss = loss_func(output, b_y)
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

        if step % 50 == 0:
            test_x = Variable(test_data_loader)
            test_output, last_layer = model(test_x)
            pred_y = torch.max(test_output, 1)[1].data.squeeze()
            accuracy = sum(pred_y == test_y) / float(test_y.size(0))
            print('Epoch: ', epoch, '| train loss: %.4f' % loss.data[0], '| test accuracy: %.2f' % accuracy)

Plus the output of my prints that I've tried to use for diagnostics:加上我试图用于诊断的打印输出:

torch.Size([100, 16, 60, 60])
torch.Size([100, 16, 60, 60])
torch.Size([100, 16, 30, 30])
torch.Size([100, 32, 26, 26])
torch.Size([100, 32, 26, 26])
++++++++++++++ out
torch.Size([100, 32, 13, 13])
torch.Size([100, 5408])
-----------------------
torch.Size([100, 5])
=======================

The problem is in this line:问题出在这一行:

output = model(b_x)[0]

The [0] changes the shape from [100, 5] to [5] , and the loss expects the other way. [0]将形状从[100, 5]更改为[5] ,并且损失预期相反。 Just remove it:只需删除它:

output = model(b_x)

Its most probably a mismatch between the output and truth label format.它很可能是输出和真实标签格式之间的不匹配。 You would need to include more details like loss function used, the truth label format etc. in order to be able to debug this您需要包含更多详细信息,例如使用的损失函数、真值标签格式等,以便能够对此进行调试

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 IndexError:维度超出范围(预计在 [-2, 1] 范围内,但得到 3) - IndexError: Dimension out of range (expected to be in range of [-2, 1], but got 3) IndexError:尺寸超出范围 - PyTorch 尺寸预计在 [-1, 0] 范围内,但得到 1 - IndexError: Dimension out of range - PyTorch dimension expected to be in range of [-1, 0], but got 1 在 Pytorch 中出现错误:IndexError: Dimension out of range (expected to be in range of [-1, 0], but got 1) - Getting an Error in Pytorch: IndexError: Dimension out of range (expected to be in range of [-1, 0], but got 1) IndexError:尺寸超出范围(预计在 [-1, 0] 范围内,但得到 -2)与 torch_geometry - IndexError: Dimension out of range (expected to be in range of [-1, 0], but got -2) with torch_geometry RuntimeError: dimension out of range(预期在 [-1, 0] 范围内,但得到 1) - RuntimeError: dimension out of range (expected to be in range of [-1, 0], but got 1) 维度超出范围(预计在 [-4, 3] 范围内,但得到 64) - Dimension out of range (expected to be in range of [-4, 3], but got 64) pytoch RuntimeError: Dimension out of range (expected to be in range of [-1, 0], but got 1 - pytoch RuntimeError: Dimension out of range (expected to be in range of [-1, 0], but got 1 文件“/model.py”,第 33 行,向前 x_out = torch.cat(x_out, 1) IndexError: Dimension out of range (expected to be in range of [-1, 0], but got 1) - File "/model.py", line 33, in forward x_out = torch.cat(x_out, 1) IndexError: Dimension out of range (expected to be in range of [-1, 0], but got 1) InvalidArgumentError:预期维度在 [-1, 1) 范围内,但得到 1 - InvalidArgumentError: Expected dimension in the range [-1, 1) but got 1 IndexError:列表索引超出范围2维列表 - IndexError: list index out of range 2 dimension list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM