简体   繁体   English

如何修复此错误:列表索引必须是整数或切片,而不是元组

[英]How to fix this error: list indices must be integers or slices, not tuple

Suppose I have a list of tensors called outputs假设我有一个称为outputs的张量列表

>> outputs[2][0][0,:,:]
Out[20]: 
tensor([[ 14.0448,  -5.1494,  -0.1780,  ...,  10.1937,  -8.9158,  -5.3964],
        [ 32.0382,  -0.5201,  29.9942,  ..., -18.8268, -23.1068,  23.9745],
        [-24.5911,  14.7233,  -6.3053,  ...,  -5.8131,  -3.3088,   0.5685],
        [  0.8842, -14.8318,   6.7204,  ...,  17.7127,   7.3332,   3.7249],
        [  2.0654, -16.5236,  38.3582,  ..., -23.1663,  -5.1202,  13.6506]],
       grad_fn=<SliceBackward>)


>> outputs[2][1][0,:,:]
Out[24]: 
tensor([[-0.1260,  0.0463, -0.3362,  ...,  0.1089, -0.2454,  0.0140],
        [ 0.5050, -0.0750, -0.1639,  ..., -0.0020, -0.0521, -0.3224],
        [-0.5311,  0.4526,  0.0079,  ..., -0.0654, -0.1255, -0.0012],
        [ 0.0728, -0.1219,  0.0905,  ...,  0.1354,  0.2730, -0.1186],
        [-0.0680, -0.5570,  0.0295,  ..., -0.2411, -0.1690,  0.0331]],
       grad_fn=<SliceBackward>)

When I try to do:当我尝试这样做时:

>> outputs[2][0:1][0,:,:]

python generates an error, and the error message is python 生成错误,错误信息为

TypeError: list indices must be integers or slices, not tuple

How can I fix this error?我该如何解决这个错误?

Thank you,谢谢,

outputs[2][0] and outputs[2][1] both return an object (tensor I suppose). output outputs[2][0]outputs[2][1]都返回一个 object (我想是张量)。
outputs[2][0:1] returns a list of those objects. outputs[2][0:1]返回这些对象的列表。
What I think you are looking for is something like outputs[2][0:1][:,0,:,:] or [a[0,:,:] for a in outputs[2][0:1]]我认为您正在寻找的是类似outputs[2][0:1][:,0,:,:][a[0,:,:] for a in outputs[2][0:1]]

As you asked for code examples, here they go:正如您要求的代码示例,这里是 go:

import torch

# using small shape to make it printable
outputs_2 = [torch.rand((3,2,1)) for _ in range(2)]

print(outputs_2[0][0,:,:])
# tensor([[0.3294],
#         [0.0031]])

print(outputs_2[1][0,:,:])
# tensor([[0.1910],
#         [0.1547]])

# this is the source of your problem
type(outputs_2[0:1])
# <class 'list'>

I don't know what you expect as a result.我不知道你期望的结果是什么。 Here's what I think it is (I'm going to use the [0:2] example to be clearer):这就是我的想法(我将使用[0:2]示例更清楚):

torch.stack(outputs_2[0:2])[:, 0, :, :]
# tensor([[[0.3294],
#          [0.0031]],
# 
#         [[0.1910],
#          [0.1547]]])

torch.stack(outputs_2[0:2]).shape
# torch.Size([2, 3, 2, 1])

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

相关问题 如何修复错误“元组索引必须是整数或切片,而不是列表” - how to fix error "tuple indices must be integers or slices, not list" 如何修复“类型错误:列表索引必须是整数或切片,而不是元组” - How to fix "TypeError: list indices must be integers or slices, not tuple" “列表索引必须是整数或切片,而不是元组”错误 - "List indices must be integers or slices, not tuple" error 错误:列表索引必须是整数或切片,而不是元组 - Error: list indices must be integers or slices, not tuple 错误是“列表索引必须是整数或切片,而不是元组” - Error is 'list indices must be integers or slices, not tuple' 如何解决“列表索引必须是整数或切片,而不是列表”错误? - How to fix ' list indices must be integers or slices, not list' error? 垂直切片:列表索引必须是整数或切片,而不是元组错误 - Vertical Slices: list indices must be integers or slices, not tuple error 如何修复“列表索引必须为整数或切片”错误 - How to fix “list indices must be integers or slices” error 处理列表会出现错误“列表索引必须是整数或切片,而不是元组” - processing a list gives the error "list indices must be integers or slices, not tuple" 类型错误“列表索引必须是整数或切片,而不是元组”(第6行) - Type error ' list indices must be integers or slices, not tuple' (line 6)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM