In Tensorflow's C++ API, how do I use an Eigen Tensor to set my Tensorflow Tensor?
Asked Answered
D

1

6

So let's say I have a 4D Eigen::Tensor T.

Similarly I also have a 4D Tensorflow::Tensor X with the same shape as T

  int size = T.dimension(0);
  int rows = T.dimension(1);
  int cols = T.dimension(2);
  int channels = T.dimension(3);

  TensorShape TS;
  TS.AddDim(size);
  TS.AddDim(rows);
  TS.AddDim(cols);
  TS.AddDim(size);

  Tensor x( DT_FLOAT, TS);

Now I want to put the data in T in x.

So I try to do:

  x.matrix<float>()() = T;

But the compiler yells at me when I do thqt:

cannot convert 'Eigen::Tensor' to 'Eigen::TensorMap, 16>::Scalar {aka float}' in assignment

When I try to convert T to a TensorMap I get even more error.

What am I missing here?

Disembarrass answered 17/8, 2016 at 18:46 Comment(1)
It seems that the question here has part of the answer:#36044697Disembarrass
K
0

I think the problem is you are using '.matrix', which only return 2d matrix.

template <typename T>
  typename TTypes<T>::Matrix matrix() {
    return tensor<T, 2>();
  }

You should use '.tensor', which can return Nd matrix.

template <typename T, size_t NDIMS>
typename TTypes<T, NDIMS>::Tensor Tensor::tensor() {
  CheckTypeAndIsAligned(DataTypeToEnum<T>::v());
  return typename TTypes<T, NDIMS>::Tensor(base<T>(),
                                           shape().AsEigenDSizes<NDIMS>());
}
Komsomol answered 14/2, 2018 at 18:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.