Search code examples
pythonarraysnumpyfor-loopbatch-processing

Batch-wise loop over array Python to create new array without overwriting


I want to iterate over a 3d array (sequences) with shape (1134500, 1, 50)

array([[[1000, 1000, 1000, ..., 1005, 1005, 1005]],

   [[1000, 1000, 1000, ..., 1004, 1005, 1004]],

   [[1000, 1000, 1000, ..., 1004, 1005, 1004]],

   ...,

   [[1000, 1000, 1000, ..., 1005, 1005, 1004]],

   [[1000, 1000, 1000, ..., 1005, 1005, 1005]],

   [[1000, 1000, 1000, ..., 1004, 1005, 1004]]], dtype=int32)

To do this, I use the following for loop, which works well except for it overwriting the results from the batch before:

batchsize = 500

for i in range(0, sequences.shape[0], batchsize):
   batch = sequences[i:i+batchsize]
   relevances = lrp_model.lrp(batch)

As a result, I want an array (relevances) with shape (1134500, 1, 50), but I get one with shape (500, 1, 50) Can someone tell me what's going wrong?


Solution

  • In case you want to save the relevances, maybe

    batchsize = 500
    relevances = np.zeros(sequences.shape)
    for i in range(0, sequences.shape[0], batchsize):
       batch = sequences[i:i+batchsize]
       relevances[i:i+batchsize, :, :] = lrp_model.lrp(batch)