How to Change 2D Eigen :: Tensor to Eigen :: Matrix

It seems simple enough. I would have thought that some kind of casting would be possible, but I can not find any documentation for it.

Although I found ways in my application to avoid using the Eigen :: Matrix class, TensorFlow only works with Eigen :: Tensor, and the other library that I use has only functionality to work directly with Eigen :: Matrix. It would be impressive to read the code if I could use the tensor as a matrix and work with it.

: it seems that TensorFlow DOES has a function to get Eigen :: Matrix (still checking it). Perhaps this makes the original less interesting question (perhaps no one NEEDS to convert Tensors to Matrices.) However, I still think this is the right question. therefore I will not record

edit 2: after looking at the TF documentation after some build errors, it seems that the tensorflow Tensor :: matrix () function just returns 2d Eigen :: Tensor, so the conversion is really necessary.

+4
source share
1 answer

TensorFlow, tensorflow/core/kernels/linalg_ops_common.cc. templatized, .

, tensorflow::Tensor, t float, Eigen- m :

tensorflow::Tensor t = ...;

auto m = Eigen::Map<Eigen::Matrix<
             float,           /* scalar element type */
             Eigen::Dynamic,  /* num_rows is a run-time value */
             Eigen::Dynamic,  /* num_cols is a run-time value */
             Eigen::RowMajor  /* tensorflow::Tensor is always row-major */>>(
                 t.flat<float>().data(),  /* ptr to data */
                 t.dim_size(0),           /* num_rows */
                 t.dim_size(1)            /* num_cols */);

tensorflow::OpKernel (, Compute()), const:

OpKernelContext* ctx = ...;
const tensorflow::Tensor t = ctx->input(...);

const auto m = Eigen::Map<const Eigen::Matrix<
                   float,           /* scalar element type */
                   Eigen::Dynamic,  /* num_rows is a run-time value */
                   Eigen::Dynamic,  /* num_cols is a run-time value */
                   Eigen::RowMajor  /* tensorflow::Tensor is always row-major */>>(
                       t.flat<float>().data(),  /* ptr to data */
                       t.dim_size(0),           /* num_rows */
                       t.dim_size(1)            /* num_cols */);
+9

Source: https://habr.com/ru/post/1654490/


All Articles