在 numpy/pytorch 中取多个切片

Take multiply slices in numpy/pytorch

我有一个大的一维数组 X.shape = (10000,) 和一个索引向量 y = [0, 7, 9995]

我想得到一个包含行的矩阵

[
 X[0 : 100],
 X[7 : 107],
 concat(X[9995:], X[:95]),
]

也就是说,长度为 100 的切片,从每个索引开始,环绕。

我可以用 python 循环来做到这一点,但我想知道在 pytorch 或 numpy 中是否有更智能的批处理方法,因为我的数组可能非常大。

其实很简单。

  1. 对于 y 中的每个元素 E,创建一个从 EE + 100
  2. 的范围
  3. 水平连接所有范围
  4. X
  5. 的长度对结果数组求模
indexes = np.hstack([np.arange(v, v + 100) for v in y]) % X.shape[0]

输出:

>>> indexes
array([   0,    1,    2,    3,    4,    5,    6,    7,    8,    9,   10,
         11,   12,   13,   14,   15,   16,   17,   18,   19,   20,   21,
         22,   23,   24,   25,   26,   27,   28,   29,   30,   31,   32,
         33,   34,   35,   36,   37,   38,   39,   40,   41,   42,   43,
         44,   45,   46,   47,   48,   49,   50,   51,   52,   53,   54,
         55,   56,   57,   58,   59,   60,   61,   62,   63,   64,   65,
         66,   67,   68,   69,   70,   71,   72,   73,   74,   75,   76,
         77,   78,   79,   80,   81,   82,   83,   84,   85,   86,   87,
         88,   89,   90,   91,   92,   93,   94,   95,   96,   97,   98,
         99,    7,    8,    9,   10,   11,   12,   13,   14,   15,   16,
         17,   18,   19,   20,   21,   22,   23,   24,   25,   26,   27,
         28,   29,   30,   31,   32,   33,   34,   35,   36,   37,   38,
         39,   40,   41,   42,   43,   44,   45,   46,   47,   48,   49,
         50,   51,   52,   53,   54,   55,   56,   57,   58,   59,   60,
         61,   62,   63,   64,   65,   66,   67,   68,   69,   70,   71,
         72,   73,   74,   75,   76,   77,   78,   79,   80,   81,   82,
         83,   84,   85,   86,   87,   88,   89,   90,   91,   92,   93,
         94,   95,   96,   97,   98,   99,  100,  101,  102,  103,  104,
        105,  106, 9995, 9996, 9997, 9998, 9999,    0,    1,    2,    3,
          4,    5,    6,    7,    8,    9,   10,   11,   12,   13,   14,
         15,   16,   17,   18,   19,   20,   21,   22,   23,   24,   25,
         26,   27,   28,   29,   30,   31,   32,   33,   34,   35,   36,
         37,   38,   39,   40,   41,   42,   43,   44,   45,   46,   47,
         48,   49,   50,   51,   52,   53,   54,   55,   56,   57,   58,
         59,   60,   61,   62,   63,   64,   65,   66,   67,   68,   69,
         70,   71,   72,   73,   74,   75,   76,   77,   78,   79,   80,
         81,   82,   83,   84,   85,   86,   87,   88,   89,   90,   91,
         92,   93,   94])

现在只需使用索引 X 即可:

X[indexes]