[英]Reduction (sum) along arbitrary axes of a multidimensional array
我想沿多維矩陣的任意軸執行和減少,該多維矩陣可以具有任意尺寸(例如,10維陣列的軸5)。 矩陣使用行主格式存儲,即作為vector
與沿每個軸的步幅一起存儲。
我知道如何使用嵌套循環執行此縮減(請參見下面的示例),但這樣做會導致硬編碼軸(縮減沿着下面的軸1)和任意數量的維度(下面的4)。 如何在不使用嵌套循環的情況下對此進行概括?
#include <iostream>
#include <vector>
int main()
{
// shape, stride & data of the matrix
size_t shape [] = { 2, 3, 4, 5};
size_t strides[] = {60,20, 5, 1};
std::vector<double> data(2*3*4*5);
for ( size_t i = 0 ; i < data.size() ; ++i ) data[i] = 1.;
// shape, stride & data (zero-initialized) of the reduced matrix
size_t rshape [] = { 2, 4, 5};
size_t rstrides[] = {20, 5, 1};
std::vector<double> rdata(2*4*5, 0.0);
// compute reduction
for ( size_t a = 0 ; a < shape[0] ; ++a )
for ( size_t c = 0 ; c < shape[2] ; ++c )
for ( size_t d = 0 ; d < shape[3] ; ++d )
for ( size_t b = 0 ; b < shape[1] ; ++b )
rdata[ a*rstrides[0] + c*rstrides[1] + d*rstrides[2] ] += \
data [ a*strides [0] + b*strides [1] + c*strides [2] + d*strides [3] ];
// print resulting reduced matrix
for ( size_t a = 0 ; a < rshape[0] ; ++a )
for ( size_t b = 0 ; b < rshape[1] ; ++b )
for ( size_t c = 0 ; c < rshape[2] ; ++c )
std::cout << "(" << a << "," << b << "," << c << ") " << \
rdata[ a*rstrides[0] + b*rstrides[1] + c*rstrides[2] ] << std::endl;
return 0;
}
注意:我想避免'解壓縮'和'壓縮'計數器。 我的意思是,我可以用偽代碼做:
for ( size_t i = 0 ; i < data.size() ; ++i )
{
i -> {a,b,c,d}
discard "b" (axis 1) -> {a,c,d}
rdata(a,c,d) += data(a,b,c,d)
}
我不知道這個代碼有多高效,但在我看來,它確實是准確的。
關於adjusted_strides
一點點:
對於axis_count = 4
, adjusted_strides
大小為5
,其中:
adjusted_strides[0] = shape[0]*shape[1]*shape[2]*shape[3];
adjusted_strides[1] = shape[1]*shape[2]*shape[3];
adjusted_strides[2] = shape[2]*shape[3];
adjusted_strides[3] = shape[3];
adjusted_strides[4] = 1;
讓我們以維數為4
,多維數組( A
)的形狀為n0, n1, n2, n3
為例。
當我們需要將這個數組轉換成另一個形狀的多維數組( B
): n0, n2, n3
(壓縮axis = 1 (0-based)
)時,我們嘗試按如下方式進行:
對於A
每個指數,我們試圖找到它在B
位置。 設A[i][j][k][l]
為A
任何元素。 它在flat_A
位置將是A[i*n1*n2*n3 + j*n2*n3 + k*n3 + l]
idx = i*n1*n2*n3 + j*n2*n3 + k*n3 + l;
在壓縮數組B
,該元素將是B[i][k][l]
的一部分(或添加到其中)。 在flat_B
,索引是new_idx = i*n2*n3 + k*n3 + l;
。
那我們怎樣形成new_idx
從idx
?
壓縮軸之前的所有軸都具有壓縮軸的形狀作為其產品的一部分。 在我們的例子中,我們必須刪除軸1
,因此i
)所代表的0th axis
1軸之前的所有軸(這里只有一個:第0th axis
), n1
作為產品的一部分( i*n1*n2*n3
) 。
壓縮軸后的所有軸都不受影響。
最后,我們需要做兩件事:
在要壓縮的軸的索引之前隔離軸的索引,並刪除此軸的形狀:
整數除法 : idx / (n1*n2*n3);
( == idx / adjusted_strides[1]
)。
我們只剩下i
,可以根據新形狀重新調整(乘以n2*n3
):我們得到
i*n2*n3
( == i * adjusted_strides[2]
)。
我們在壓縮軸之后隔離軸,這些軸不受其形狀的影響。
idx % (n2*n3)
( == idx % adjusted_strides[2]
)
這給了我們k*n3 + l
。
添加步驟i的結果。 和ii。 結果是:
computed_idx = i*n2*n3 + k*n3 + l;
這與new_idx
相同。 所以,我們的轉型是正確的:)。
注意: ni
指的是new_idx
。
size_t cmp_axis = 1, axis_count = sizeof shape/ sizeof *shape;
std::vector<size_t> adjusted_strides;
//adjusted strides is basically same as strides
//only difference being that the first element is the
//total number of elements in the n dim array.
//The only reason to introduce this array was
//so that I don't have to write any if-elses
adjusted_strides.push_back(shape[0]*strides[0]);
adjusted_strides.insert(adjusted_strides.end(), strides, strides + axis_count);
for(size_t i = 0; i < data.size(); ++i) {
size_t ni = i/adjusted_strides[cmp_axis]*adjusted_strides[cmp_axis+1] + i%adjusted_strides[cmp_axis+1];
rdata[ni] += data[i];
}
(0,0,0) 3
(0,0,1) 3
(0,0,2) 3
(0,0,3) 3
(0,0,4) 3
(0,1,0) 3
(0,1,1) 3
(0,1,2) 3
(0,1,3) 3
(0,1,4) 3
(0,2,0) 3
(0,2,1) 3
(0,2,2) 3
(0,2,3) 3
(0,2,4) 3
(0,3,0) 3
(0,3,1) 3
(0,3,2) 3
...
如需進一步閱讀,請參閱此處 。
我認為這應該有效:
#include <iostream>
#include <vector>
int main()
{
// shape, stride & data of the matrix
size_t shape [] = { 2, 3, 4, 5};
size_t strides[] = {60, 20, 5, 1};
std::vector<double> data(2 * 3 * 4 * 5);
size_t rshape [] = { 2, 4, 5};
size_t rstrides[] = {3, 5, 1};
std::vector<double> rdata(2 * 4 * 5, 0.0);
const unsigned int NDIM = 4;
unsigned int axis = 1;
for (size_t i = 0 ; i < data.size() ; ++i) data[i] = 1;
// How many elements to advance after each reduction
size_t step_axis = strides[NDIM - 1];
if (axis == NDIM - 1)
{
step_axis = strides[NDIM - 2];
}
// Position of the first element of the current reduction
size_t offset_base = 0;
size_t offset = 0;
size_t s = 0;
for (auto &v : rdata)
{
// Current reduced element
size_t offset_i = offset;
for (unsigned int i = 0; i < shape[axis]; i++)
{
// Reduce
v += *(data.data() + offset_i);
// Advance to next element
offset_i += strides[axis];
}
s = (s + 1) % strides[axis];
if (s == 0)
{
offset_base += strides[axis - 1];
offset = offset_base;
}
else
{
offset += step_axis;
}
}
// Print
for ( size_t a = 0 ; a < rshape[0] ; ++a )
for ( size_t b = 0 ; b < rshape[1] ; ++b )
for ( size_t c = 0 ; c < rshape[2] ; ++c )
std::cout << "(" << a << "," << b << "," << c << ") " << \
rdata[ a*rstrides[0] + b*rstrides[1] + c*rstrides[2] ] << std::endl;
return 0;
}
輸出:
(0,0,0) 3
(0,0,1) 3
(0,0,2) 3
(0,0,3) 3
(0,0,4) 3
(0,1,0) 3
(0,1,1) 3
(0,1,2) 3
(0,1,3) 3
(0,1,4) 3
(0,2,0) 3
(0,2,1) 3
(0,2,2) 3
// ...
設置axis = 3
產生:
(0,0,0) 5
(0,0,1) 5
(0,0,2) 5
(0,0,3) 5
(0,0,4) 5
(0,1,0) 5
(0,1,1) 5
(0,1,2) 5
(0,1,3) 5
(0,1,4) 5
(0,2,0) 5
(0,2,1) 5
(0,2,2) 5
(0,2,3) 5
// ...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.