当前位置:   article > 正文

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

typeerror: list indices must be integers or slices, not list

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

原因:

传入参数搞错了
计划通过一个下标list,通过rand.shuffle实现训练数据的随机化,结果因为传入的数据是没有tokenized的(就是一堆原始的字符串,并且是用list保存的,tokenize之后应该是一个torch.tensor类型的张量)

修复方法:

对应原因,传入正确的参数即可

总结:

python原生的list不支持使用一个int型list作为下标进行访问,
pytorch的张量支持这样访问
可能torch是基于numpy设计的,numpy中的数组也支持这样访问

如图,list会报错
list下标报错
而np数组和 torch的张量均不会报错
在这里插入图片描述

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号