How should I create a tensorflow::Tensor from Eigen::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. 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>();
This will avoid a copy. Moreover, Eigen tensors and tensormaps share the same apis, so you can use them interchangeably.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With