Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating tensorflow::Tensor from Eigen::Tensor

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.

like image 918
Moshe Kravchik Avatar asked Jul 12 '26 12:07

Moshe Kravchik


1 Answers

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.

like image 54
Benoit Steiner Avatar answered Jul 15 '26 03:07

Benoit Steiner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!