简体   繁体   中英

Tensorflow C++ API: How to iterate Tensor with Dimesion=3?

I have a tensor with shape(2,2,3) from the input. And I can get the shape of this tensor by .shape() which can be used to help iteration. But I fail to iterate it which may caused by the wrong conversion to matrix while matrix's dimession always 2 The code showes as below:

const Tensor& input = context->input(0); // get the input
DCHECK_EQ(input.dims(), 3); //check the dimission==3
const TensorShape& input_shape = input.shape(); //get the shape of the tensor
auto input_tensor = input.matrix<float>();//wrong conversion
for(int i=0; i<input_shape.dim_size(0); i++)
{
  for(int j=0; j<input_shape.dim_size(1); j++)
  {
    for(int k=0; k<input_shape.dim_size(2); k++)
      {
        count<<input_tensor(i,j,k);//wrong code
      }
  }
}

So, I want to know the way to iterate this tensor. Thanks very much!

It's suprised that I get the answer when I compare the matrix api and the tensor api in Tensorflow.org . And find the usage:

const Tensor& input = context->input(0); // get the input
DCHECK_EQ(input.dims(), 3); //check the dimission==3
const TensorShape& input_shape = input.shape(); //get the shape of the tensor
auto input_tensor = input.tensor<float, 3>();//true conversion
for(int i=0; i<input_shape.dim_size(0); i++)
{
    for(int j=0; j<input_shape.dim_size(1); j++)
    {
        for(int k=0; k<input_shape.dim_size(2); k++)
        {
           count<<input_tensor(i,j,k);//right one
        }
    }
}

Maybe in the before I miss the NDImission paramter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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