简体   繁体   English

从Eigen :: Tensor创建tensorflow :: Tensor

[英]Creating tensorflow::Tensor from Eigen::Tensor

How should I create a tensorflow::Tensor from Eigen::Tensor? 我应该如何从Eigen :: Tensor创建一个tensorflow :: Tensor? I could just copy the elements over one-by-one, but I hope there is a better way. 我可以一一复制这些元素,但我希望有更好的方法。

There is no public api to create a tensorflow::Tensor from an Eigen::Tensor without copying the data. 没有公共api从Eigen :: Tensor创建tensorflow :: Tensor,而不复制数据。 However, you can create a tensorflow::Tensor and interpret it as a Eigen::TensorMap using the following apis: 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>(); 但是,您可以创建tensorflow :: Tensor并使用以下API将其解释为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>(); 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>();

This will avoid a copy. 这样可以避免复制。 Moreover, Eigen tensors and tensormaps share the same apis, so you can use them interchangeably. 此外,本征张量和张量图共享相同的api,因此您可以互换使用它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM