【问题标题】:Reshaping Tensor in C在 C 中重塑张量
【发布时间】:2018-11-21 22:01:42
【问题描述】:

我如何使用 Tensorflow 的 C_api 重塑 TF_Tensor*,因为它是在 C++ 中完成的?

TensorShape inputShape({1,1,80,80});

Tensor inputTensor;
Tensor newTensor;

bool result = inputTensor->CopyFrom(newTensor, inputShape);

我没有看到使用 tensorflow 的 c_api 的类似方法。

【问题讨论】:

    标签: c++ tensorflow deep-learning c-api


    【解决方案1】:

    Tensorflow C API 使用 (data,dims) 模型运行 - 将数据视为提供所需维度的平面原始数组。

    第一步:分配一个new张量

    看看TF_AllocateTensor(ref):

    TF_CAPI_EXPORT extern TF_Tensor* TF_AllocateTensor(TF_DataType,
                                                       const int64_t* dims,
                                                       int num_dims, size_t len);
    

    这里:

    1. TF_DataTypeTF 相当于您需要来自 here 的数据类型。
    2. dims:对应于要分配的张量维度的数组,例如。 {1, 1, 80, 80}
    3. num_dims:dims 的长度(4 以上)
    4. len: reduce(dims, *): 即 1*1*80*80*sizeof(DataType) = 6400*sizeof(DataType)。

    第 2 步:复制数据

    // Get the tensor buffer
    auto buff = (DataType *)TF_TensorData(output_of_tf_allocate);
    // std::memcpy() ...
    

    Here 是我之前在编写一个非常轻量级的 Tensorflow C-API Wrapper 时所做的一个项目的一些示例代码。

    所以,基本上你的重塑将涉及分配你的新张量并将数据从原始张量复制到buff

    Tensorflow C API 不适合常规使用,因此更难学习 + 缺乏文档。我通过实验想出了很多。更有经验的开发者有什么建议吗?

    【讨论】:

      猜你喜欢
      • 2017-09-14
      • 2016-02-15
      • 2016-10-18
      • 2018-10-27
      • 1970-01-01
      • 2020-10-13
      • 1970-01-01
      • 1970-01-01
      • 2018-07-19
      相关资源
      最近更新 更多