简体   繁体   English

Tensorflow C++:如何获得“输出”的张量形状?

[英]Tensorflow c++: how to get the tensor shape of an `Output?`

There's TF_GraphGetTensorShape in C API, but the interface isn't compatible with C++ Graph and Output . C API 中有TF_GraphGetTensorShape ,但接口与 C++ GraphOutput不兼容。 How to do the same using tensorflow C/C++ API?如何使用 tensorflow C/C++ API 做同样的事情?

For example.例如。 How to get the returned tensor shape of Slice operation using C++ API and then using that tensor shape to make a variable with the same shape?如何使用 C++ API 获取Slice操作的返回张量形状,然后使用该张量形状制作具有相同形状的variable

Here is a small function which returns the shape as a vector, eg {48,48,2} 这是一个小函数,它将形状作为向量返回,例如{48,48,2}

std::vector<int> get_tensor_shape(tensorflow::Tensor& tensor)
{
    std::vector<int> shape;
    int num_dimensions = tensor.shape().dims()
    for(int ii_dim=0; ii_dim<num_dimensions; ii_dim++) {
        shape.push_back(tensor.shape().dim_size(ii_dim));
    }
    return shape;
}

Apart from that I found tensor.DebugString() helpful, which yields for example "Tensor type: float shape: [48,48,2] values: [[0,0390625 -1][0,0390625]]...>" 除此之外我发现tensor.DebugString()很有帮助,例如“Tensor类型:浮点形状:[48,48,2]值:[[0,0390625 -1] [0,0390625]] ...> “

For python see this thread: https://stackoverflow.com/a/40666375/2135504 , where tensor.get_shape().as_list() is recommended. 对于python,请参阅以下主题: httpstensor.get_shape().as_list() ,其中建议使用tensor.get_shape().as_list()

I have never used tensorflow C API but in C++ API you have class Tensor which have function shape() . 我从未使用过tensorflow C API,但在C ++ API中,你有class Tensor ,它具有函数shape() It will return const TensorShape& , which has function dim_size(int index) . 它将返回const TensorShape& ,它具有函数dim_size(int index) This function will return dimension for given index value. 此函数将返回给定索引值的维度。 Hope this helps you :) 希望这可以帮助你:)

查看tensor_shape.h ,看起来tensor.shape().dim_sizes()应该给你一个包含形状的向量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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