目录
1. setConstant(const Scalar& val)
3. setValues({..initializer_list})
1.Tensor、TensorFixedSize、TensorRef 提供了如下几种方法访问tensor元素
2.Scalar* data() and const Scalar* data() const
1. constant(const Scalar& val)
七 相关API说明
1.Tensor 属性的查询
void testTensorInfo()
{
//1. NumDimensions 输出维度个数
Eigen::Tensor<float, 2> a(3, 4);
cout << "Dims " << a.NumDimensions <<endl; // 输出:Dims:2
//2. dimensions() 输出不同维度的size
//typedef DSizes<Index, NumIndices_> Dimensions;
const Eigen::Tensor<float, 2>::Dimensions & d = a.dimensions();
cout << "Dim size: " << d.size()<< ", dim 0: " << d[0]
<< ", dim 1: " << d[1] <<endl; //Dim size: 2, dim 0: 3, dim 1: 4
//3. Index dimension(Index n) 输出第n 维的维度
int dim1 = a.dimension(1);
cout << "Dim 1: " << dim1 <<endl; // Dim 1: 4
//4. Index size() 输出tensor的总元素个数
cout << "Size: " << a.size() <<endl;
}
2.初始化
当Tensor 或TensorFixedSize 创建的时候,内存已经分配,但是没有被初始化,同样第,当TensorMap如果Map了没有初始化的Tensor,它同样也没有初始化。
初始化方法有如下几种
1.<Tensor-Type> setConstant(const Scalar& val)
设置tensor所有的元素值都等于val ,Scalar 是tensor存储的数据类型
返回值是tensor本身,如下代码所示
Eigen::Tensor<float, 2> a(3, 4);
a.setConstant(12.3f);
cout << "Constant: " << endl << a << endl << endl;
setConstant()可以用于任何tensor(只要tensor存储的数据类型拥有拷贝构造函数和 operator=() )
Eigen::Tensor<string, 2> b(2, 3);
b.setConstant("yolo");
cout << "String tensor: " << endl << b << endl << endl;
2. <Tensor-Type> setZero()
将tensor的所有元素设置为0,该操作相当于调用 setContent(Scalar(0))
返回tensor 本身(用于链式调用)
a.setZero();
cout << "Zero Constant: " << endl << a << endl << endl;
3.<Tensor-Type> setValues({..initializer_list})
用初始化列表填充tensor,初始化列表的数据类型和维度取决于tensor的类型和维度
如果tensor的维度是N ,那么初始化列表(initializer_list) 必须嵌套N次,最深层次的嵌套列表里面必须包含P 个元素(P是tensor最后一维的元素个数
setValues 返回tensor本身
Eigen::Tensor<float, 2> c(2, 3);
c.setValues({ {0.0f, 1.0f, 2.0f}, {3.0f, 4.0f, 5.0f} });
cout << "c setValues" << endl << c << endl << endl;
下面的代码可以只设置tensor的第一行
Eigen::Tensor<int, 2> d(2, 3);
d.setConstant(1000);
d.setValues({ {10, 20, 30} });
cout << "d" << endl << d << endl << endl;
4.<Tensor-Type> setRandom()
用随机值填充tensor ,返回tensor本身
c.setRandom();
cout << "Random: " << endl << c << endl << endl;
3.访问tensor元素
1.Tensor、TensorFixedSize、TensorRef 提供了如下几种方法访问tensor元素
const Scalar& operator()(const array<Index, NumIndices>& indices)
const Scalar& operator()(Index firstIndex, IndexTypes... otherIndices)
Scalar& operator()(const array<Index, NumIndices>& indices)
Scalar& operator()(Index firstIndex, IndexTypes... otherIndices)
Eigen::Tensor<float, 2> c(2, 3);
c.setValues({ {0.0f, 1.0f, 2.0f}, {3.0f, 4.0f, 5.0f} });
cout << c(0,1) <<endl;
cout << c(0) << endl;
2.Scalar* data() and const Scalar* data() const
返回tensor的存储的数据指针,数据的layout依赖于tensor的layout(RowMajor 或ColMajor)
void testAccess()
{
Eigen::Tensor<float, 2> c(2, 3);
c.setValues({ {0.0f, 1.0f, 2.0f}, {3.0f, 4.0f, 5.0f} });
cout << c(0,1) <<endl;
cout << c(0) << endl;
Eigen::Tensor<float, 2> a(3, 4);
float* a_data = a.data();
a_data[0] = 123.45f;
cout << "a(0, 0): " << a(0, 0);
}
该方法通常用于比较特殊的情况下,比如将Eigon 与其他库联合使用的情况下
4.Tensor 运算
1.<Operation> constant(const Scalar& val)
返回一个tensor 与原tensor具有相同的维度和数据类型,并且所有的数据元素均被赋值为val
void testOperation()
{
Eigen::Tensor<float, 2> a(2, 3);
a.setConstant(1.0f);
Eigen::Tensor<float, 2> b = a + a.constant(2.0f);
Eigen::Tensor<float, 2> c = b * b.constant(0.2f);
cout << "a" << endl << a << endl << endl;
cout << "b" << endl << b << endl << endl;
cout << "c" << endl << c << endl << endl;
}
2.<Operation> random()
返回一个tensor 与原tensor具有相同的维度和数据类型,并且所有的数据元素被随机赋值
Eigen::Tensor<float, 2> d = c * c.random();
cout << "d" << endl << d << endl << endl;
3.一元操作
<Operation> operator-() 求相反数
<Operation> sqrt() 平方根
<Operation> rsqrt() 逆平方根
<Operation> square() 平方
<Operation> inverse()求逆
<Operation> exp()指数
<Operation> log() log运算
<Operation> abs() 绝对值
<Operation> pow(Scalar exponent)
<Operation> operator * (Scalar scale) 乘以某个值
void testUnary()
{
Eigen::Tensor<int, 2> a(2, 3);
a.setValues({ {0, 1, 8}, {27, 64, 125} });
Eigen::Tensor<double, 2> b = a.cast<double>().pow(1.0 / 3.0);
Eigen::Tensor<double, 2> sqrt = a.cast<double>().sqrt();
Eigen::Tensor<double, 2> rsqrt = a.cast<double>().rsqrt();
Eigen::Tensor<double, 2> square = a.cast<double>().square();
Eigen::Tensor<double, 2> inverse = a.cast<double>().inverse();
Eigen::Tensor<double, 2> exp = a.cast<double>().exp();
Eigen::Tensor<double, 2> log = a.cast<double>().log();
Eigen::Tensor<double, 2> abs = a.cast<double>().abs();
Eigen::Tensor<int, 2> multiply = a * 2;
cout << "a" << endl << a << endl << endl;
cout << "b" << endl << b << endl << endl;
cout << "b" << endl << b << endl << endl;
cout << "sqrt" << endl << sqrt << endl << endl;
cout << "rsqrt" << endl << rsqrt << endl << endl;
cout << "square" << endl << square << endl << endl;
cout << "inverse" << endl << inverse << endl << endl;
cout << "exp" << endl << exp << endl << endl;
cout << "log" << endl << log << endl << endl;
cout << "abs" << endl << abs << endl << endl;
cout << "multiply" << endl << multiply << endl << endl;
}
Eigen::Tensor<float, 2> f(2, 3);
f.setConstant(1.0f);
Eigen::Tensor<float, 2> g = -f;
cout << "f" << endl << f << endl << endl;
cout << "g" << endl << g << endl << endl;
4. 二元运算/逻辑运算
<Operation> operator+(const OtherDerived& other)
<Operation> operator-(const OtherDerived& other)
<Operation> operator*(const OtherDerived& other)
<Operation> operator/(const OtherDerived& other)
<Operation> cwiseMax(const OtherDerived& other) //返回与原tensor同类型,同尺寸的tensor,且以两个原tensor的最大值填充
<Operation> cwiseMin(const OtherDerived& other)
//返回与原tensor同类型,同尺寸的tensor,且以两个原tensor的最小值填充
operator&&(const OtherDerived& other)
operator||(const OtherDerived& other)
operator<(const OtherDerived& other)
operator<=(const OtherDerived& other)
operator>(const OtherDerived& other)
operator>=(const OtherDerived& other)
operator==(const OtherDerived& other)
operator!=(const OtherDerived& other)
void testBinary()
{
Eigen::Tensor<int, 2> a(2, 3);
a.setValues({ {0, 1, 8}, {27, 64, 125} });
Eigen::Tensor<int, 2> b = a * 3;
cout << "a" << endl << a << endl << endl;
cout << "b" << endl << b << endl << endl;
cout << "a+b" << endl << a + b << endl << endl;
cout << "a-b" << endl << a - b << endl << endl;
cout << "a*b" << endl << a * b << endl << endl;
cout << "a.cwiseMax(b)" << endl <<a.cwiseMax(b) << endl << endl;
cout << "b.cwiseMax(a)" << endl << b.cwiseMax(a) << endl << endl;
cout << "a.cwiseMin(b)" << endl << a.cwiseMin(b) << endl << endl;
cout << "b.cwiseMin(a)" << endl << b.cwiseMin(a) << endl << endl;
}
5.Selection
Selection 是一个三元操作符,if,then ,else 他们必须具有相同的维度,if tensor必须是bool类型的,then 和 else 必须具有相同的类型。
result 中的每一个元素值通过如下运算得到:
如果 if中相应位置的元素是true,则result的结果为 then相应位置的值,否则为else相应位置的值
void testSelection()
{
Eigen::Tensor<bool, 2> _if(2,2);
_if.setValues({ { false,true }, { true,false } });
Eigen::Tensor<int, 2> _then (2,2);
_then.setValues({ { 2,2 }, { 2,2 } });
Eigen::Tensor<int, 2> _else (2,2);
_else.setValues({ { 10,10 }, { 10,10 } });
Eigen::Tensor<int, 2> result = _if.select(_then, _else);
cout << "result:" << endl << result << endl;
}
6.降维运算
降维运算返回的tensor比原始tensor具有更少的维度,返回的tensor的值是通过对原始tensor的值切片应用一个降维算子来计算的。切片的维度可以手动指定(切片指的是需要获取元素的下标)
相关测试代码见:https://github.com/Mayi-Keiji/EigenTest.git
未完待续~~