eof
eof

Reputation: 349

Reshaping tensors in C++

The C++ interface to TensorFlow doesn't seem to have a reshape method. Does anyone have an idea how to convert e.g. [A,B,C,D] into [A*B,C,D]? It looks like the only way to do this is to use Eigen? However, the documentation there is very slim and the code is template hell and not easy to parse.

Upvotes: 4

Views: 2761

Answers (2)

Jarikus
Jarikus

Reputation: 834

Solution with checking whether reshaped 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();

Upvotes: 1

dgorissen
dgorissen

Reputation: 6305

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]

Upvotes: 0

Related Questions