C++ Eigen : dynamic tensor

标签 c++ tensorflow eigen

我想实现一个以张量 vector 作为成员的 C++ 类。张量的维度没有预定义,但会根据一些输入数据取值。此外,张量的秩可以不同。像这样:

std::vector< TensorXd > myTensors;

Eigen , 然而, 没有这样的 TensorXd动态张量的类型。

为了构造每个张量,我将读取一个数据 vector std::vector<double> values表示维度为 n x n x ... x n 的张量(r 次)。像这样:

Tensor<double, r> tensor = TensorMap<double, r>(values.data(), std::vector<size_t>(r, n);
myTensors.push_back(tensor);

这有可能吗?

非常感谢您的帮助!

更新:

正如 Yaroslav Bulatov 指出的那样,Eigen 不支持动态排名,因此必须明确写出支持的排名。在我的代码中:

#include <iostream>
#include <vector>
#include <Eigen/Dense>
#include <unsupported/Eigen/CXX11/Tensor>

typedef Eigen::Tensor< double , 3 > Tensor3d;
typedef Eigen::Tensor< double , 4 > Tensor4d;
typedef Eigen::Tensor< double , 5 > Tensor5d;
typedef Eigen::Tensor< double , 6 > Tensor6d;
typedef Eigen::Tensor< double , 7 > Tensor7d;
typedef Eigen::Tensor< double , 8 > Tensor8d;
typedef Eigen::Tensor< double , 9 > Tensor9d;
typedef Eigen::Tensor< double , 10 > Tensor10d;

class MyClass
{
private:                          
    Eigen::MatrixXd Potentials_1;            
    std::vector<Eigen::MatrixXd> Potentials_2;  
    std::vector< Tensor3d > Potentials_3;
    std::vector< Tensor4d > Potentials_4;
    std::vector< Tensor5d > Potentials_5;
    std::vector< Tensor6d > Potentials_6;
    std::vector< Tensor7d > Potentials_7;
    std::vector< Tensor8d > Potentials_8;
    std::vector< Tensor9d > Potentials_9;
    std::vector< Tensor10d > Potentials_10;

public:
    MyClass();
    void setPotentials_1(const Eigen::MatrixXd &_Potentials_1){ Potentials_1 = _Potentials_1; }
    void setPotentials_2(const std::vector<Eigen::MatrixXd> &_Potentials_2){ Potentials_2 = _Potentials_2; }
    void setPotentials_3(const std::vector<Tensor3d> &_Potentials_3){ Potentials_3 = _Potentials_3; }
    void setPotentials_4(const std::vector<Tensor4d> &_Potentials_4){ Potentials_4 = _Potentials_4; }
    void setPotentials_5(const std::vector<Tensor5d> &_Potentials_5){ Potentials_5 = _Potentials_5; }
    void setPotentials_6(const std::vector<Tensor6d> &_Potentials_6){ Potentials_6 = _Potentials_6; }
    void setPotentials_7(const std::vector<Tensor7d> &_Potentials_7){ Potentials_7 = _Potentials_7; }
    void setPotentials_8(const std::vector<Tensor8d> &_Potentials_8){ Potentials_8 = _Potentials_8; }
    void setPotentials_9(const std::vector<Tensor9d> &_Potentials_9){ Potentials_9 = _Potentials_9; }
    void setPotentials_10(const std::vector<Tensor10d> &_Potentials_10){ Potentials_10 = _Potentials_10; }
};

Yaroslav 还建议使用宏可以帮助避免代码重复。我不熟悉 C++ 宏,因此非常感谢任何帮助。

感谢您的帮助!

最佳答案

你可以看看xtensor C++模板库,它同时支持动态和静态维度。

http://xtensor.readthedocs.io/en/latest/

xtensor有一个与numpy非常相似的API,包括向量化、广播、通用函数。这里有一个 numpy 到 xtensor 备忘单:http://xtensor.readthedocs.io/en/latest/numpy.html

最后,您可以通过单击 https://github.com/QuantStack/xtensor/ 顶部的 Binder 徽章在 C++ Jupyter 笔记本中实时试用它。

xtensor 还绑定(bind)了主要的科学计算语言(R、Julia、Python)。

关于C++ Eigen : dynamic tensor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42475212/

相关文章:

tensorflow - 加载了运行时 CuDNN 库 : 7. 1.2,但源代码是用 : 7. 6.0 编译的; Ubuntu 18.04

c++ - 为什么我不能在同一个 MS VS 解决方案中的两个控制台应用程序中使用 C++ Eigen(仅 header 库)?

c++ - Eigen:引用 ArrayWrapper 的有效方法

c++ - 将 fftw_complex 重新映射到 Eigen 库中的 Matrixcd

c++ - 与负数进行比较时在程序中出现 '\342'

c++ - gSOAP命名空间前缀问题

python - 运行子图并提供中间变量

c++ - SFML 检测假操纵杆

c++ - 消息框中的字符串自动换行?

python - Keras 在一类 Cifar-10 上过度拟合