一次从Numpy数组中选择多个切片

时间:2018-10-29 19:40:25

标签: python numpy indexing

我想实现矢量化SGD算法,并想一次生成多个迷你批次。

假设data = np.arange(0, 100)miniBatchSize=10n_miniBatches=10indices = np.random.randint(0, n_miniBatches, 5)(5个小批量)。我想实现的是

miniBatches = np.zeros(5, miniBatchSize)
for i in range(5):
     miniBatches[i] = data[indices[i]: indices[i] + miniBatchSize]

有什么方法可以避免for循环吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

可以使用stride tricks

from numpy.lib.stride_tricks import as_strided

a = as_strided(data[:n_miniBatches], shape=(miniBatchSize, n_miniBatches), strides=2*data.strides, writeable=False)    
miniBatches = a[:, indices].T


# E.g. indices = array([0, 7, 1, 0, 0])
Output:
array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9],
       [ 7,  8,  9, 10, 11, 12, 13, 14, 15, 16],
       [ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10],
       [ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9],
       [ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9]])