解决IndexError: too many indices for tensor of dimension 3

如果不小心搞错了tensor的维度,就会出现以下错误

> IndexError: too many indices for tensor of dimension 3

很有可能是你是一个三维的tensor,但是你在调用索引时使用了第四维的索引作切片,
这时应该检查是不是因为粗心而导致tensor的维度出错
例如:

x = torch.randn(1, 2, 3)
x[0, 0, 0, 0]

这样就会报 IndexError: too many indices for tensor of dimension 3 的错误

你可能感兴趣的:(Python学习,深度学习,pytorch,深度学习,python)