包装 Eigen 的张量 class 以创建动态秩张量?

Wrap Eigen's tensor class to create dynamic rank tensors?

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}) return 单独的张量,但它们都指向相同的张量内部数组。因此,您可以从这些 sub_tensors.

修改原始张量

所有数据都分配在一个数组上。如果你想使用点积,你需要 link 它到 BLAS。这是头文件,它详细介绍了大部分方法。 https://github.com/josephjaspers/BlackCat_Tensors/blob/master/BC_Headers/Tensor.h

但是,它并不是特别快,我个人使用这个个人库只是为了制作神经网络原型 (https://github.com/josephjaspers/BlackCat_NeuralNetworks)。