Eigen::Tensor使用,定义高维矩阵

2024-09-01 18:08

本文主要是介绍Eigen::Tensor使用,定义高维矩阵,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在实际项目中,需要存储大于等于三维的矩阵,而平常中我们使用Eigen::MatrixXd二维数据,这里我们使用Eigen::Tensor来定义

1.Using the Tensor module
#include <unsupported/Eigen/CXX11/Tensor>
2.定义矩阵
2.一般矩阵

官方文档

  // 定义一个2x3x4大小的矩阵Eigen::Tensor<float, 3> a(2, 3, 4);// 初始化为0a.setZero();// 访问元素a(0, 1, 0) = 12.0f;for (int i = 0; i < 2; i++) {for (int j = 0; j < 3; j++) {for (int k = 0; k < 4; k++) {std::cout << a(i, j, k) << " ";}std::cout << std::endl;}std::cout << std::endl << std::endl;}// 输出维度std::cout<<a.dimension(0)<<" "<<a.dimension(1)<<" "<<a.dimension(2)<<std::endl;

上面输出结果

0 0 0 0 
12 0 0 0 
0 0 0 0 0 0 0 0 
0 0 0 0 
0 0 0 02 3 4
2.固定大小矩阵TensorFixedSize

参考官方解释

The fixed sized equivalent of Eigen::Tensor<float, 3> t(3, 5, 7); is Eigen::TensorFixedSize<float, Size<3,5,7>> t;

这里我们定义

  // 固定 大小的Size 2x3x4Eigen::TensorFixedSize<float, Eigen::Sizes<2, 3, 4>> b;// 每个元素都设置固定值b.setConstant(3.f);for (int i = 0; i < 2; i++) {for (int j = 0; j < 3; j++) {for (int k = 0; k < 4; k++) {std::cout << b(i, j, k) << " ";}std::cout << std::endl;}std::cout << std::endl << std::endl;}

结果如下

3 3 3 3 
3 3 3 3 
3 3 3 3 3 3 3 3 
3 3 3 3 
3 3 3 3
3.常用函数API

参考从零开始编写深度学习库(四)Eigen::Tensor学习使用及代码重构
1.维度

  Eigen::Tensor<float, 2> a(3, 4);std::cout << "Dims " << a.NumDimensions;//=> Dims 2
  Eigen::Tensor<float, 2> a(3, 4);int dim1 = a.dimension(1);std::cout << "Dim 1: " << dim1;//=> Dim 1: 4

2.形状

  Eigen::Tensor<float, 2> a(3, 4);const Eigen::Tensor<float, 2>::Dimensions& d = a.dimensions();std::cout << "Dim size: " << d.size << ", dim 0: " << d[0]<< ", dim 1: " << d[1];//=> Dim size: 2, dim 0: 3, dim 1: 4

3.矩阵元素个数

  Eigen::Tensor<float, 2> a(3, 4);std::cout << "Size: " << a.size();//=> Size: 12

4.初始化

  /// 1.// setConstant(const Scalar& val),用于把一个矩阵的所有元素设置成一个指定的常数。Eigen::Tensor<string, 2> a(2, 3);a.setConstant("yolo");std::cout << "String tensor: " << endl << a << endl << endl;//=>// String tensor:// yolo yolo yolo// yolo yolo yolo/// 2.// setZero() 全部置零a.setZero();/// 3.// setRandom() 随机初始化a.setRandom();std::cout << "Random: " << endl << a << endl << endl;//=>//Random://  0.680375    0.59688  -0.329554    0.10794// -0.211234   0.823295   0.536459 -0.0452059// 0.566198  -0.604897  -0.444451   0.257742/// 4.// setValues({..initializer_list}) 从列表、数据初始化Eigen::Tensor<float, 2> a(2, 3);a.setValues({{0.0f, 1.0f, 2.0f}, {3.0f, 4.0f, 5.0f}});std::cout << "a" << endl << a << endl << endl;//=>// a// 0 1 2// 3 4 5//如果给定的数组数据,少于矩阵元素的个数,那么后面不足的元素其值不变:Eigen::Tensor<int, 2> a(2, 3);a.setConstant(1000);a.setValues({{10, 20, 30}});std::cout << "a" << endl << a << endl << endl;//=>// a// 10   20   30// 1000 1000 1000

4.运算
参考Eigen Tensor详解【二】
4.1 一元运算

<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;std::cout << "a" << std::endl << a << std::endl <<std:: endl;
}

4.2 二元运算

<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;std::cout << "a" << std::endl << a << std::endl << std::endl;std::cout << "b" << std::endl << b << std::endl << std::endl;std::cout << "a+b" << std::endl << a + b << std::endl << std::endl;std::cout << "a-b" << std::endl << a - b << std::endl << std::endl;std::cout << "a*b" << std::endl << a * b << std::endl << std::endl;std::cout << "a.cwiseMax(b)" << std::endl <<a.cwiseMax(b) << std::endl << std::endl;std::cout << "b.cwiseMax(a)" << std::endl << b.cwiseMax(a) << std::endl << std::endl;std::cout << "a.cwiseMin(b)" << std::endl << a.cwiseMin(b) << std::endl << std::endl;std::cout << "b.cwiseMin(a)" << std::endl << b.cwiseMin(a) << std::endl << std::endl;
}

4.3 三元运算和降维运算
看参考链接Eigen Tensor详解【二】

4.其他方式

参考Eigen构造使用三维矩阵
如果定义多维数据也可以使用Matrix模板来自定义,

Matrix<typename Scalar, int RowsAtCompileTime, int ColsAtCompileTime>
Eigen::Matrix<Eigen::MatrixXd,1,1> a;
Eigen::Matrix<Eigen::Matrix<double,1,5>,1,1> a;Eigen::Matrix<Eigen::MatrixXd, 1, 1> a;//声明a,一个1*1矩阵
Eigen::MatrixXd b;      //声明b
b.setZero(1, 5); //对b初始化
b << 1, 2, 3, 4, 5;//对b赋值
a(0, 0) = b;//对a(0,0)赋值
std::cout << "a(0,0):  " << a(0, 0) << std::endl;//输出a(0,0)
std::cout << "b:  " << b << std::endl;//输出b
int row = a(0, 0).rows();//row为a(0,0)处矩阵的行维数
int col = a(0, 0).cols();//col为a(0,0)处矩阵的列维数
std::cout << "row:  " << row << "  col:  " << col << std::endl;//输出row和col值
参考

https://blog.csdn.net/hjimce/article/details/71710893
https://blog.csdn.net/fengshengwei3/article/details/103591178
http://eigen.tuxfamily.org/index.php?title=Tensor_support#Using_the_Tensor_module
https://eigen.tuxfamily.org/dox/unsupported/classEigen_1_1TensorFixedSize.html
https://zhuanlan.zhihu.com/p/148019818

这篇关于Eigen::Tensor使用,定义高维矩阵的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1127671

相关文章

Java 中的 @SneakyThrows 注解使用方法(简化异常处理的利与弊)

《Java中的@SneakyThrows注解使用方法(简化异常处理的利与弊)》为了简化异常处理,Lombok提供了一个强大的注解@SneakyThrows,本文将详细介绍@SneakyThro... 目录1. @SneakyThrows 简介 1.1 什么是 Lombok?2. @SneakyThrows

使用Python和Pyecharts创建交互式地图

《使用Python和Pyecharts创建交互式地图》在数据可视化领域,创建交互式地图是一种强大的方式,可以使受众能够以引人入胜且信息丰富的方式探索地理数据,下面我们看看如何使用Python和Pyec... 目录简介Pyecharts 简介创建上海地图代码说明运行结果总结简介在数据可视化领域,创建交互式地

Java Stream流使用案例深入详解

《JavaStream流使用案例深入详解》:本文主要介绍JavaStream流使用案例详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录前言1. Lambda1.1 语法1.2 没参数只有一条语句或者多条语句1.3 一个参数只有一条语句或者多

Java Spring 中 @PostConstruct 注解使用原理及常见场景

《JavaSpring中@PostConstruct注解使用原理及常见场景》在JavaSpring中,@PostConstruct注解是一个非常实用的功能,它允许开发者在Spring容器完全初... 目录一、@PostConstruct 注解概述二、@PostConstruct 注解的基本使用2.1 基本代

C#使用StackExchange.Redis实现分布式锁的两种方式介绍

《C#使用StackExchange.Redis实现分布式锁的两种方式介绍》分布式锁在集群的架构中发挥着重要的作用,:本文主要介绍C#使用StackExchange.Redis实现分布式锁的... 目录自定义分布式锁获取锁释放锁自动续期StackExchange.Redis分布式锁获取锁释放锁自动续期分布式

springboot使用Scheduling实现动态增删启停定时任务教程

《springboot使用Scheduling实现动态增删启停定时任务教程》:本文主要介绍springboot使用Scheduling实现动态增删启停定时任务教程,具有很好的参考价值,希望对大家有... 目录1、配置定时任务需要的线程池2、创建ScheduledFuture的包装类3、注册定时任务,增加、删

使用Python实现矢量路径的压缩、解压与可视化

《使用Python实现矢量路径的压缩、解压与可视化》在图形设计和Web开发中,矢量路径数据的高效存储与传输至关重要,本文将通过一个Python示例,展示如何将复杂的矢量路径命令序列压缩为JSON格式,... 目录引言核心功能概述1. 路径命令解析2. 路径数据压缩3. 路径数据解压4. 可视化代码实现详解1

Pandas透视表(Pivot Table)的具体使用

《Pandas透视表(PivotTable)的具体使用》透视表用于在数据分析和处理过程中进行数据重塑和汇总,本文就来介绍一下Pandas透视表(PivotTable)的具体使用,感兴趣的可以了解一下... 目录前言什么是透视表?使用步骤1. 引入必要的库2. 读取数据3. 创建透视表4. 查看透视表总结前言

Python 交互式可视化的利器Bokeh的使用

《Python交互式可视化的利器Bokeh的使用》Bokeh是一个专注于Web端交互式数据可视化的Python库,本文主要介绍了Python交互式可视化的利器Bokeh的使用,具有一定的参考价值,感... 目录1. Bokeh 简介1.1 为什么选择 Bokeh1.2 安装与环境配置2. Bokeh 基础2

Android使用ImageView.ScaleType实现图片的缩放与裁剪功能

《Android使用ImageView.ScaleType实现图片的缩放与裁剪功能》ImageView是最常用的控件之一,它用于展示各种类型的图片,为了能够根据需求调整图片的显示效果,Android提... 目录什么是 ImageView.ScaleType?FIT_XYFIT_STARTFIT_CENTE