2017-03-01 32 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