1

我有一个A形状的 numpy 数组(550,10)。我有 100 的批量大小,即我想要多少数据行A。在每次迭代中,我想从 A 中提取 100 行。但是当我到达最后 50 行时,我想从 A 中提取最后 50 行和前 50 行。

我有这样的功能:

def train(index, batch_size):

    if(batch_size + index < A.shape(0)):
          data_end_index = index + batch_size
          batch_data = A[index:batch_end_index,:]
    else:
          data_end_index = index + batch_size - A.shape(0) #550+100-600 = 50
          batch_data = A[500 to 549 and 0 to 49] # How to slice here ?

如何执行最后一步?

4

2 回答 2

4

you can try:

import numpy as np
data=np.random.rand(550,10)
batch_size=100

for index in range(0,data.shape[0],batch_size):
    batch=data[index:min(index+batch_size,data.shape[0]),:]
    print(batch.shape)

output:

(100, 10)
(100, 10)
(100, 10)
(100, 10)
(100, 10)
(50, 10)
于 2018-02-09T09:47:48.743 回答
1

窃取 riccardo 的示例数据,使用numpy.split

data=np.random.rand(550,10)
batch_size=100

q, block_end = data.shape[0] // batch_size, q * batch_size

batch = np.split(data[:block_end], q) + [data[block_end:]]

[*map(np.shape, batch)]
Out[89]: [(100, 10), (100, 10), (100, 10), (100, 10), (100, 10), (50, 10)]
于 2018-02-09T14:48:20.357 回答