简体   繁体   English

如何通过迭代批量数组来创建多维结果

[英]How to create a multi-dimensional result by iterating over batches of arrays

I'm iterating over some input batches and generating results that have shape (BatchSize, X, Y).我正在迭代一些输入批次并生成具有形状(BatchSize、X、Y)的结果。 The BatchSize is not necessarily the same as I loop over the batches. BatchSize不一定与我循环批处理相同。 I'd like to return a single output which is the concatenated version of the results along the batch dimension.我想返回一个单一的输出,它是沿着批次维度的结果的串联版本。 What's the most elegant way to do this in NumPy?在 NumPy 中执行此操作最优雅的方法是什么?

I'm not so much worried about the performance but rather dealing with the multi-dimensionality of the accumulated result array.我不太担心性能,而是处理累积结果数组的多维性。

Assuming that you have enough memory to hold all of the results, a good solution is to simply pre-allocate the memory:假设您有足够的内存来保存所有结果,一个好的解决方案是简单地预先分配内存:

result = np.empty(OUTPUT_SHAPE)
i=0
while i < input_tensor.shape[0]:
    batch_size = get_batch_size(i)
    result[i:i+batch_size] = deal_with_batch(input_tensor[i:i+batch_size])
    i += batch_size

The answer by @Scott is correct. @Scott 的回答是正确的。 I was however looking for the incremental version which I think I've found:然而,我正在寻找我认为已经找到的增量版本:

Define results = np.empty((0, output_shape)) and then update it in the loop using results = np.concatenate((results, some_func(x)))定义results = np.empty((0, output_shape))然后使用results = np.concatenate((results, some_func(x)))在循环中更新它

I'm not sure how I should think about a dimension of size 0 in numpy but it works.我不确定我应该如何考虑 numpy 中大小为 0 的维度,但它有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM