【问题标题】:Vector of Eigen tensors with different ranks不同等级的特征张量向量
【发布时间】:2017-03-01 14:15:40
【问题描述】:

我的问题:是否可以创建具有不同等级的特征张量向量?

我的目标是能够创建一个包含对象的向量,这些对象的类型取值在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();
};

【问题讨论】:

标签: c++ tensorflow eigen eigen3


【解决方案1】:

由于您将此问题标记为[tensorflow],我假设您将Eigen::Tensor 用作TensorFlow 程序的一部分。 answer Aziuth pointed in their comment 建议使用包装类来保存张量,幸运的是 TensorFlow 带有自己的包装类:tensorflow::Tensor

您可以创建 std::vector&lt;tensorflow::Tensor&gt;,使用 TensorFlow 的分配器为不同的张量分配内存,并使用 tensorflow::Tensor::tensor() 方法访问包装好的 Eigen::Tensor 对象。

【讨论】:

  • 非常感谢,@mrry。不幸的是,我没有在 TensorFlow 中使用 Eigen::Tensor,而是在纯 C++ 中使用。我标记了“tensorflow”,因为我认为 Tensorflow 的人也非常熟悉 Eigen::Tensor。抱歉,如果这造成了混乱(但我认为您的回答对 Tensorflow 用户也非常有用!)。
  • 唉!您可能需要实现类似的东西......也许代码here 很容易为您的目的而减少......
  • 谢谢,@mrry!该 TensorFlow 代码非常有用。让我花点时间弄清楚如何使用它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-06-30
  • 2023-03-15
  • 2018-03-25
  • 1970-01-01
  • 1970-01-01
  • 2018-09-20
  • 2020-11-08
相关资源
最近更新 更多