ValueError: slice index xxxx of dimension 0 out of bounds,详细分析。
出现这种问题的原因是,一般都是数据维度的原因,导致参数不匹配,一定要检查参数维度。
你比如,我报错的原因:
我的 image_placeholder声明的是[10,16,112,112,3],但是后面我在调用的时候,却这样用了
logits = c3d_model.inference(image_placeholder[10,:, :, :, :], 。。。。。)
这里的这样(注意10后面逗号)的话就会导致维度错误,有的同学会说,哦!我懂了,把逗号去了就行了,然而并不是这样的。
接下来我们来看一下代码:
import numpy as npa = np.ones([5,6,7,7,9])
#print(a)
print("加逗号:",a[0,:,:,:,:].shape) #[6,7,7,9]print("加逗号:",a[1,:,:,:,:].shape) #[6,7,7,9]print("加逗号:",a[2,:,:,:,:].shape) #[6,7,7,9]print("加逗号:",a[3,:,:,:,:].shape) #[6,7,7,9]print("加逗号:",a[4,:,:,:,:].shape) #[6,7,7,9]#print("加逗号:",a[5,:,:,:,:].shape) #报错:print("--------------------------")#上述都等价于a[i]print("不加逗号:",a[0:,:,:,:].shape) #[5,6,7,7,9]print("不加逗号:",a[1:,:,:,:].shape) #[4,6,7,7,9]print("不加逗号: ",a[2:,:,:,:].shape) #[3,6,7,7,9]print("不加逗号: ",a[3:,:,:,:].shape) #[2,6,7,7,9]print("不加逗号: ",a[4:,:,:,:].shape) #[1,6,7,7,9]print("不加逗号: ",a[9:,:,:,:].shape) #不报错 #[0,6,7,7,9],这样的维度的是什么东西,其实就是一个空list :[].
#上述都等价于啊a[i:]
ps:只要维度里出现0,则这都是一个空列表。
相信看到这里不少小伙伴大概明白了,我们稍加解释:
(1)加逗号:即当前维度与下一维度之间有逗号,表示提取第几个slice,这样产生的数据会降低一个维度,并且对数据有要求,只能取值为0-dims(当前维度) -1。否则会报错:ValueError: slice index xxx of dimension 0 out of bounds。
(2)不加逗号:即当前维度与下一维度之间无逗号,表示从当前维度的第几个值进行切片,不会改变原始数据的维度。当取值>dims(当前维度) -1 时,不报错,但是会产生空列表。(小技巧,查逗号,看数值出现在第几个逗号处,则对哪一个维度切片)
接下来有一些例子来加深我们的理解,大家可以先手动计算,然后再写代码验证:
import numpy as npa = np.ones([5,6,7,8,9])
#print(a)print("加逗号:",a[:,0,:,:,:].shape) print('加逗号:',a[3,:,:,1,:].shape)print('不加逗号:',a[3:,:,2:].shape) #小技巧,查逗号,看数值出现在第几个逗号处,则对哪一个维度取切片。
加逗号: (5, 7, 8, 9)
加逗号: (6, 7, 9)
不加逗号: (2, 6, 5, 8, 9)
熟悉:c3d_model的朋友,或许对个语句不陌生:
images_placeholder[gpu_index * FLAGS.batch_size:(gpu_index + 1) * FLAGS.batch_size,:,:,:,:],
其实很容易理解,这又涉及到另外一种情况:我们来看代码:
import numpy as npbatch_size = 8gpu_index = 0a = np.ones([4*8,6,7,8,9])print(a[gpu_index * batch_size:(gpu_index + 1) * batch_size,:,:,:,:].shape)
for i in range (4):print(": ",a[:(i + 1)*batch_size,:,:,:,:].shape)print("i*batch_size: ",a[i * batch_size:(i + 1)*batch_size,:,:,:,:].shape)
(8, 6, 7, 8, 9)
: (8, 6, 7, 8, 9)
i*batch_size: (8, 6, 7, 8, 9)
: (16, 6, 7, 8, 9)
i*batch_size: (8, 6, 7, 8, 9)
: (24, 6, 7, 8, 9)
i*batch_size: (8, 6, 7, 8, 9)
: (32, 6, 7, 8, 9)
i*batch_size: (8, 6, 7, 8, 9)
分析,这里表示对第最高维是(i + 1)*batch_size之前的切片,从i * batch_size处取切片。添加一个:且与当前数值维度无逗号间隔,相当于添加一个维度。
这里我们详细的讲解了上知识,具体的情况大家也可以编写代码验证,希望维度问题这种问题不再影响大家心情。
ValueError: slice index xxxx of dimension 0 out of bounds,详细分析。相关推荐
- 成功解决tensorflow.python.framework.errors_impl.InvalidArgumentError: slice index 1 of dimension 0 out o
成功解决tensorflow.python.framework.errors_impl.InvalidArgumentError: slice index 1 of dimension 0 out o ...
- InvalidArgumentError: slice index xxx of dimension xxx out of bounds
InvalidArgumentError: slice index xxx of dimension xxx out of bounds 發生原因 解決辦法 完整Traceback 參考連結 發生原因 ...
- Android 4.0 Launcher源码详细分析 傻蛋
http://wenku.baidu.com/view/80e280e2998fcc22bcd10dcd.html
- IndexError:boolean index did not match indexed array along dimension 0
转自:https://blog.csdn.net/pcy1127918/article/details/79975152 IndexError: boolean index did not match ...
- ValueError: matmul: Input operand 1 has a mismatch in its core dimension 0, with gufunc signature (n
ValueError: matmul: Input operand 1 has a mismatch in its core dimension 0, with gufunc signature (n ...
- mask-rcnn在训练过程中,突然中断报错,提示:boolean index did not match indexed array along dimension 0;dimension is..
一.环境: win10 + gpu 3090 + maskrcnn + tensorflow2.6.0; 二.报错信息如下: IndexError: boolean index did not mat ...
- IndexError: boolean index did not match indexed array along dimension 0; dimension is 82 but corresp
在训练maskrcnn时出现以上报错,通过一天的琢磨.debug.掉头发.才发现是标签数据的问题. 解决办法: 1.运行trian文件,程序一般会训练一个epoch才会停止,记录下出现问题的标签的id ...
- IndexError: boolean index did not match indexed array along dimension 0; dimension is 1 but correspo
IndexError: boolean index did not match indexed array along dimension 0; dimension is 1 but correspo ...
- mask-rcnn报错: IndexError: boolean index did not match indexed array along dimension 0; ......
mask-rcnn在执行train.py训练模型时,报错如下: image_id 769 ERROR:root:Error processing image {'id': 769, 'source': ...
最新文章
- percona mysql安装_mysql 安装 (percona)
- springboot启动过程_不要搞笑哈,你用了5年的SpringBoot框架,竟然不了解它的启动过程?...
- 【数据结构与算法】之深入解析“二叉搜索树中的插入操作”的求解思路与算法示例
- spring mvc学习(55):简单异常处理二
- Error installing bundler:bundler requires Ruby version >= 2.3.0.
- 同程旅行王晓波:如何改变 Redis 用不好的误区
- python 批量查询网页导出结果_李亚涛:python批量查询网页收录情况并计算收录率...
- mysql 账号安全_MySQL账号安全设置
- 如何做到每天都写代码
- appium+python 【Mac】Android夜神模拟器
- 同步I/O sync、fsync和fdatasync函数
- CentOS 8 基础命令
- 常用的英文单词2000
- 没有找到MSVCR100.dll解决方法
- CAD图形的缩放——放大镜
- HTML Canvas 刮刮卡抽奖效果的实现
- RCF—用于C++的进程间通讯(1)
- 挪威访学2:SOLA UIS 学生宿舍
- 联发科MT6750/MT6750T芯片处理器哪个性能比较好?区别在哪?
- 定风波·莫听穿林打叶声
热门文章
- 西天取经为节约成本该裁掉哪位?
- 【Weiss】【第03章】练习3.20:中缀表达式转后缀表达式
- 2010年写给自己的一封信
- [网络安全自学篇] 八.Web漏洞及端口扫描之Nmap、ThreatScan和DirBuster原理详解
- [python] LDA处理文档主题分布代码入门笔记
- 44. Wildcard Matching 通配符匹配
- 并发编程——线程——锁
- The 3n + 1 problem UVA - 100
- 【机器视觉】 dev_get_exception_data算子
- 【Tools】MarkDown教程(二)-MarkDown基本语法