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中String字符串使用避坑指南

《Java中String字符串使用避坑指南》Java中的String字符串是我们日常编程中用得最多的类之一,看似简单的String使用,却隐藏着不少“坑”,如果不注意,可能会导致性能问题、意外的错误容... 目录8个避坑点如下:1. 字符串的不可变性:每次修改都创建新对象2. 使用 == 比较字符串,陷阱满

Python使用国内镜像加速pip安装的方法讲解

《Python使用国内镜像加速pip安装的方法讲解》在Python开发中,pip是一个非常重要的工具,用于安装和管理Python的第三方库,然而,在国内使用pip安装依赖时,往往会因为网络问题而导致速... 目录一、pip 工具简介1. 什么是 pip?2. 什么是 -i 参数?二、国内镜像源的选择三、如何

使用C++实现链表元素的反转

《使用C++实现链表元素的反转》反转链表是链表操作中一个经典的问题,也是面试中常见的考题,本文将从思路到实现一步步地讲解如何实现链表的反转,帮助初学者理解这一操作,我们将使用C++代码演示具体实现,同... 目录问题定义思路分析代码实现带头节点的链表代码讲解其他实现方式时间和空间复杂度分析总结问题定义给定

Linux使用nload监控网络流量的方法

《Linux使用nload监控网络流量的方法》Linux中的nload命令是一个用于实时监控网络流量的工具,它提供了传入和传出流量的可视化表示,帮助用户一目了然地了解网络活动,本文给大家介绍了Linu... 目录简介安装示例用法基础用法指定网络接口限制显示特定流量类型指定刷新率设置流量速率的显示单位监控多个

JavaScript中的reduce方法执行过程、使用场景及进阶用法

《JavaScript中的reduce方法执行过程、使用场景及进阶用法》:本文主要介绍JavaScript中的reduce方法执行过程、使用场景及进阶用法的相关资料,reduce是JavaScri... 目录1. 什么是reduce2. reduce语法2.1 语法2.2 参数说明3. reduce执行过程

如何使用Java实现请求deepseek

《如何使用Java实现请求deepseek》这篇文章主要为大家详细介绍了如何使用Java实现请求deepseek功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1.deepseek的api创建2.Java实现请求deepseek2.1 pom文件2.2 json转化文件2.2

python使用fastapi实现多语言国际化的操作指南

《python使用fastapi实现多语言国际化的操作指南》本文介绍了使用Python和FastAPI实现多语言国际化的操作指南,包括多语言架构技术栈、翻译管理、前端本地化、语言切换机制以及常见陷阱和... 目录多语言国际化实现指南项目多语言架构技术栈目录结构翻译工作流1. 翻译数据存储2. 翻译生成脚本

C++ Primer 多维数组的使用

《C++Primer多维数组的使用》本文主要介绍了多维数组在C++语言中的定义、初始化、下标引用以及使用范围for语句处理多维数组的方法,具有一定的参考价值,感兴趣的可以了解一下... 目录多维数组多维数组的初始化多维数组的下标引用使用范围for语句处理多维数组指针和多维数组多维数组严格来说,C++语言没

在 Spring Boot 中使用 @Autowired和 @Bean注解的示例详解

《在SpringBoot中使用@Autowired和@Bean注解的示例详解》本文通过一个示例演示了如何在SpringBoot中使用@Autowired和@Bean注解进行依赖注入和Bean... 目录在 Spring Boot 中使用 @Autowired 和 @Bean 注解示例背景1. 定义 Stud

使用 sql-research-assistant进行 SQL 数据库研究的实战指南(代码实现演示)

《使用sql-research-assistant进行SQL数据库研究的实战指南(代码实现演示)》本文介绍了sql-research-assistant工具,该工具基于LangChain框架,集... 目录技术背景介绍核心原理解析代码实现演示安装和配置项目集成LangSmith 配置(可选)启动服务应用场景