问题
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.
回答1:
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();
回答2:
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]
来源:https://stackoverflow.com/questions/43476944/reshaping-tensors-in-c