在犰狳立方体中添加一列 1 的有效方法
Efficient way to add a column of 1s in armadillo Cube
我正在尝试使用线性代数库在 c++ 中实现神经网络armadillo
。我正在使用Cube
来存储网络的inputs
和weights
,我希望能够在 3d 矩阵中添加bias
单元。我遇到了许多方法,其中涉及从立方体到矩阵的对话,这似乎效率低下。那么,在立方体中每个矩阵的开头添加一列零的最有效方法是什么?
不幸的是,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
相关文章:
- 有符号的int和int-有没有一种方法可以在C++中区分它们
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 有没有一种方法可以在编译时获得作用域类名
- 有没有一种方法可以测量c++程序的运行时内存使用情况
- 有没有一种方法可以使用placement new将堆叠对象分配给分配的内存
- 有没有一种方法可以通过"typedef"为重新定义的基本类型定义特征和强制转换运算符
- 查找矩阵C++中每一列和每一行的最小和最大元素
- 在 c++ 中,有一种方法可以创建一个包含地图作为值的树状地图?
- 如何在C++中获取二维数组中最少的一列数?
- C++ STD 函数运算符:有没有一种方法可以通过函数将一个向量映射到另一个向量上?
- 有没有一种方法可以捕获进程中的堆栈溢出?C++Linux
- C++有没有办法强制重写一组方法,如果其中一个方法在子类中具有重写?
- 我想直接在结构中插入,但没有一种方法可以正确避免填充问题
- 使用 C++20 概念强制类实现一组方法
- 类中一种方法的部分专用化
- 只需要知道我在c ++中打印模式的方式是否有效,或者有另一种方法可以有效地做到这一点
- 有没有一种方法可以忽略Qt c++中的文件名大小写敏感性?
- 在犰狳立方体中添加一列 1 的有效方法
- 有没有一种方法可以在两列中显示我的素数列表输出
- 有没有一种方法可以从输入中找到二维数组的行和列