【问题标题】:Creating tensorflow::Tensor from Eigen::Tensor从 Eigen::Tensor 创建 tensorflow::Tensor
【发布时间】:2018-06-07 14:47:19
【问题描述】:

我应该如何从 Eigen::Tensor 创建一个 tensorflow::Tensor?我可以一个接一个地复制元素,但我希望有更好的方法。

【问题讨论】:

    标签: c++ tensorflow eigen tensor


    【解决方案1】:

    没有公共 api 可以在不复制数据的情况下从 Eigen::Tensor 创建 tensorflow::Tensor。但是,您可以使用以下 api 创建一个 tensorflow::Tensor 并将其解释为 Eigen::TensorMap: tensorflow::Tensor tf_tensor(tensor_constructor_args); // For the general case: Eigen::TensorMap<type_params> eigen_tensor = tf_tensor.tensor<Type, NumDims>(); // shortcuts if you know the tensor is a matrix/vector/scalar Eigen::TensorMap<type_params> eigen_matrix = tf_tensor.matrix<Type>(); Eigen::TensorMap<type_params> eigen_vector = tf_tensor.vector<Type>(); Eigen::TensorMap<type_params> eigen_scalar = tf_tensor.scalar<Type>();

    这将避免复制。此外,Eigen tensors 和 tensormaps 共享相同的 api,因此可以互换使用。

    【讨论】:

    • 非常感谢@Benoit Steiner,但这对我帮助不大。但也许我试图解决一个错误的问题。我使用 Eigen::Tensor 的原因是我可以轻松地对它们进行切片。所以我从文件中读取了所有样本,每次迭代都传递给模型对应的切片。我可以用纯 tensorflow::Tensor 做到这一点吗?
    • 没有简单的方法来切片 tensorflow::Tensor。但是 TensorFlow 提供了一个 tf.slice 操作,您可以使用它来对输入数据进行切片,并在每次迭代时单独提供每个样本。
    猜你喜欢
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-06
    • 1970-01-01
    相关资源
    最近更新 更多