Changing tensors in C ++

The C ++ interface for TensorFlow doesn't seem to have a reformatting method. Does anyone have an idea how to convert eg. [A,B,C,D]

in [A*B,C,D]

? Seems like the only way to do this is using Eigen? However, the documentation is very thin and the code is boilerplate hell and not easy to parse.

+3


source to share


2 answers


Solution checking if the modified tensor has the same number of elements of the source tensor:



// Extracted image features from MobileNet_224
tensorflow::Tensor image_features(tensorflow::DT_FLOAT,
                                  tensorflow::TensorShape({1, 14, 14, 512}));

tensorflow::Tensor image_features_reshaped(tensorflow::DT_FLOAT,
                                           tensorflow::TensorShape({1, 196, 512}));

// Reshape tensor from [1, 14, 14, 512] to [1, 196, 512]
if(!image_features_reshaped.CopyFrom(image_features, tensorflow::TensorShape({1, 196, 512})))
{
  LOG(ERROR) << "Unsuccessfully reshaped image features tensor [" << image_features.DebugString() << "] to [1, 196, 512]";
  return false;
}

LOG(INFO) << "Reshaped features tensor: " << image_features_reshaped.DebugString();

      

+1


source


This should work:



Tensor my_tensor; // [A, B, C, D]
Tensor reshaped_tensor = my_tensor.shaped<float, 3>({A*B, C, D});  //[A*B, C, D]

      

0


source







All Articles