Pytorch 索引器:维度超出范围(预期在[-1,0]范围内,但得到2)
为什么在运行上述代码时会弹出以下错误 然后重新运行上面的代码Pytorch 索引器:维度超出范围(预期在[-1,0]范围内,但得到2),pytorch,Pytorch,为什么在运行上述代码时会弹出以下错误 然后重新运行上面的代码 plt.imshow(torchvision.utils.make_grid(images[3], nrow=5).permute(1, 2, 0 ) --------------------------------------------------------------------------- IndexError Traceback (most recent
plt.imshow(torchvision.utils.make_grid(images[3], nrow=5).permute(1, 2, 0 )
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-78-ce90d0e2159a> in <module>
----> 1 plt.imshow(torchvision.utils.make_grid(images[3], nrow=5).permute(1, 2, 0))
~\Anaconda3\lib\site-packages\torchvision\utils.py in make_grid(tensor, nrow, padding, normalize, range, scale_each, pad_value)
74 xmaps = min(nrow, nmaps)
75 ymaps = int(math.ceil(float(nmaps) / xmaps))
---> 76 height, width = int(tensor.size(2) + padding), int(tensor.size(3) + padding)
77 num_channels = tensor.size(1)
78 grid = tensor.new_full((num_channels, height * ymaps + padding, width * xmaps + padding), pad_value)
IndexError: Dimension out of range (expected to be in range of [-1, 0], but got 2)
model= nn.Sequential(nn.Linear (150528, 1000),
nn.ReLU(),
nn.Linear(1000, 250) ,
nn.ReLU(),
nn.Linear(250, 32),
nn.LogSoftmax(dim=1),
)
criterion = nn.CrossEntropyLoss()
images = images.view(images.shape[0], -1)
logits = model (images)
loss = criterion(logits, labels)
print(loss)
plt.imshow(torchvision.utils.make_grid(images[3], nrow=5).permute(1, 2, 0 )