2017-03-01 56 views
3

我的问题:是否有可能创建一个具有不同级别的特征张量向量?不同级别的特征张量向量

我的目标是能够创建一个向量,其中包含的对象的类型取值为Eigen::MatrixXd, Tensor3d, Tensor4d,..., Tensor10d(定义如下)中的值。对象可以有不同的类型。

在此先感谢您的帮助!

#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: 
    std::vector<TensorXd> Tensors; 
public: 
    MyClass(); 
}; 
+0

也许:http://stackoverflow.com/questions/13461869/c-push-multiple-types-onto-vector,但这有点难看。 – Aziuth

+0

@Aziuth谢谢,我会试一试,但仍然在寻找更优雅和透明的解决方案。 – Khue

回答

2

既然你标记这个问题[tensorflow],我假设你正在使用Eigen::Tensor作为TensorFlow计划的一部分。 answerAziuth pointed in their comment建议使用包装类来保存张量,幸运的是TensorFlow带有自己的包装类:tensorflow::Tensor

您可以创建一个std::vector<tensorflow::Tensor>,使用TensorFlow的分配为不同的张量分配内存,并使用tensorflow::Tensor::tensor()方法存取包裹Eigen::Tensor对象。

+0

非常感谢,@ mrry。不幸的是,我不是在TensorFlow中使用Eigen :: Tensor,而是使用纯C++。我标记'tensorflow',因为我认为Tensorflow人也非常熟悉Eigen :: Tensor。对不起,如果这造成混乱(但我认为你的答案对Tensorflow用户也是非常有用!)。 – Khue

+0

唉!你可能需要实现类似的东西...也许代码[这里](https://github.com/tensorflow/tensorflow/blob/a230417c58c258b2417225c739a1e5f0890491e6/tensorflow/core/framework/tensor.h)很容易剪切为你的目的下降...... – mrry

+0

谢谢,@ mrry! Tensorflow代码非常有用。让我花点时间弄清楚如何使用它。 – Khue