Reputation: 21
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!
Upvotes: 1
Views: 807
Reputation: 21
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.
Upvotes: 1