c++ - 包装 Eigen 的张量类以创建动态秩张量?

标签 c++ templates eigen eigen3

Eigen 的张量模块通过使用模板参数支持静态等级张量,例如 Eigen::Tensor<typename T,int dims> .我只会使用 Eigen::Tensor<double, n> ,其中 n 在编译时不一定是已知的。反正有没有像这样的类(class):

class TensorWrapper{
    Eigen::Tensor<double,??????> t; //not sure what could go here
    TensorWrapper(int dimensions){
        t = Eigen::Tensor<double,dimensions>(); //wouldn't work either way
    }
};

我知道Eigen::Tensor<double,2>Eigen::Tensor<double,3>彼此完全没有关系,因为它们是模板化的,而且我不能在运行时确定模板参数,所以上述方法会以各种可能的方式失败。我也知道 Tensorflow 中的张量支持这一点,但它们在 C++ 中没有张量收缩(这就是我首先需要张量的原因)。有什么办法可以做我想做的事情,即使是有限数量的维度(我不需要超过 5 或 6 个)?如果没有,是否有任何支持动态秩和张量收缩的 c++ 张量库?

最佳答案

嗨,我写了这个多维张量库(它不是完全成熟的),它支持点积和逐点元素等基本操作。

https://github.com/josephjaspers/BlackCat_Tensors

Tensor<float> tensor3 = {3, 4, 5};   -- generates a 3 dimensional tensor (3 rows, 4 columns, 5 pages)
Tensor<float> tensor5 = {1,2,3,4,5}; -- generate a 5d tensors (1 rows, 2 columns, 3 pages, etc)
tensor3[1] = 3;                      -- returns the second matrix and sets all the values to 3. 
tensor3[1][2];                       -- returns the second matrx and then then 3rd column of that matrix
tensor3({1,2,3},{2,2});              -- at index 1,2,3, returns a sub-matrix of dimensions 2x2

所有访问运算符 [] (int index) 和 ({initializer_list index},{initializer_list shape}) 返回单独的张量,但它们都引用同一个内部数组。因此,您可以从这些 sub_tensor 中修改原始张量。

所有数据都分配在一个数组上。如果您想使用 dotproduct,您需要将其链接到 BLAS。这是头文件,它详细说明了大多数方法。 https://github.com/josephjaspers/BlackCat_Tensors/blob/master/BC_Headers/Tensor.h

然而,它并不是特别快,我个人使用这个个人库只是为了对 NeuralNetworks 进行原型(prototype)设计(https://github.com/josephjaspers/BlackCat_NeuralNetworks)。

关于c++ - 包装 Eigen 的张量类以创建动态秩张量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47275496/

相关文章:

c++ - 有没有办法让C++应用程序功能开机?

c++ - 渲染动态立方体贴图 (OpenGL)

c++ - 如何让 C++ 从 lambda 推断模板类型参数?

c++ - 在模板类 C++ 中使用函数指针

C++ 递归类型特征

c++ - 使用 Eigen::VectorXd 减少 OpenMP

c++ - 断言失败 Eigen Debug模式

opencv - 将CV Mat对象转换为std::vector <Eigen::Vector3d>不带循环

c++ - 仅对具有显式构造函数的类的对象进行相等性检查

c++ - 安装VC2008后VC2005变慢了