通过列表索引对数组进行切片的Pythonic方法

问题描述

我想知道执行批处理切片(torch / numpy)的最佳方法是什么? 我知道对于恒定切片索引,可以执行此操作

batch_size = 2
data = torch.zeros((batch_size,1,256,256))
x_start = 10
x_stop = 20

y_start = 10
y_stop = 20
data[torch.arange(batch_size),:,y_start:y_stop,x_start:x_stop] = 1

但是问题是,如果起始值和终止值不同,该如何处理? 例如。

batch_size = 2
data = torch.zeros((batch_size,256))
x_start = [10,5]
x_stop  = [20,30]

y_start = [10,5]
y_stop = [20,30]
data[torch.arange(batch_size),x_start:x_stop] = 1 # crashes

我想我可以在for循环中做到这一点,但是我想知道是否还有一种更Python的方式来做到这一点。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)