[英]Select Multiple slices from Numpy array at once
I want to implement a vectorized SGD algorithm and would like to generate multiple mini batches at once. 我想实现矢量化SGD算法,并想一次生成多个迷你批次。
Suppose data = np.arange(0, 100)
, miniBatchSize=10
, n_miniBatches=10
and indices = np.random.randint(0, n_miniBatches, 5)
(5 mini batches). 假设
data = np.arange(0, 100)
, miniBatchSize=10
, n_miniBatches=10
并且indices = np.random.randint(0, n_miniBatches, 5)
(5 indices = np.random.randint(0, n_miniBatches, 5)
批量)。 What I would like to achieve is 我想实现的是
miniBatches = np.zeros(5, miniBatchSize)
for i in range(5):
miniBatches[i] = data[indices[i]: indices[i] + miniBatchSize]
Is there any way to avoid for loop? 有什么方法可以避免for循环吗?
Thanks! 谢谢!
It can be done using 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]])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.