2017-07-26 102 views
1

我想在C++中使用armadillo线性代数库实现神经网络。我正在使用Cube来存储网络的inputsweights,我希望能够在3d矩阵中添加bias单元。我遇到了很多方法来做到这一点,涉及从立方体到矩阵的对话,这似乎是低效的。那么在多维数据集中每个矩阵的开始处添加一列零的最有效方式是什么?在Armadillo Cube中添加1列的高效方法

回答

1

不幸的是,join_slices只支持具有相同行数和列数的连接立方体。因此,你通过每片需要循环,并追加使用insert_rows行向量,像这样:

#include<armadillo> 

using namespace arma; 

uword nRows = 5; 
uword nCols = 3; 
uword nSlices = 3; 

/*original cube*/ 
cube A(nRows , nCols, nSlices, fill::randu); 
/*new cube*/ 
cube B(nRows+1, nCols, nSlices, fill::zeros); 
/*row vector to append*/ 
rowvec Z(nCols, fill::zeros); 

/*go through each slice and change mat*/ 
for (uword i = 0; i < A.n_slices; i++) 
{ 
    mat thisMat = A.slice(i); 
    thisMat.insert_rows(0, Z); 
    B.slice(i) = thisMat; 
} 

这应该给:

A: 
[cube slice 0] 
    0.0013 0.1741 0.9885 
    0.1933 0.7105 0.1191 
    0.5850 0.3040 0.0089 
    0.3503 0.0914 0.5317 
    0.8228 0.1473 0.6018 

[cube slice 1] 
    0.1662 0.8760 0.7797 
    0.4508 0.9559 0.9968 
    0.0571 0.5393 0.6115 
    0.7833 0.4621 0.2662 
    0.5199 0.8622 0.8401 

[cube slice 2] 
    0.3759 0.8376 0.5990 
    0.6772 0.4849 0.7350 
    0.0088 0.7437 0.5724 
    0.2759 0.4580 0.1516 
    0.5879 0.7444 0.4252 


B: 
[cube slice 0] 
     0  0  0 
    0.0013 0.1741 0.9885 
    0.1933 0.7105 0.1191 
    0.5850 0.3040 0.0089 
    0.3503 0.0914 0.5317 
    0.8228 0.1473 0.6018 

[cube slice 1] 
     0  0  0 
    0.1662 0.8760 0.7797 
    0.4508 0.9559 0.9968 
    0.0571 0.5393 0.6115 
    0.7833 0.4621 0.2662 
    0.5199 0.8622 0.8401 

[cube slice 2] 
     0  0  0 
    0.3759 0.8376 0.5990 
    0.6772 0.4849 0.7350 
    0.0088 0.7437 0.5724 
    0.2759 0.4580 0.1516 
    0.5879 0.7444 0.4252