【问题标题】:Reshaping tensors in C++在 C++ 中重塑张量
【发布时间】:2017-09-14 13:47:02
【问题描述】:

TensorFlow 的 C++ 接口似乎没有 reshape 方法。有谁知道如何转换例如[A,B,C,D] 变成 [A*B,C,D]?看起来这样做的唯一方法是使用 Eigen?但是,那里的文档很薄,代码是模板地狱,不容易解析。

【问题讨论】:

    标签: tensorflow eigen


    【解决方案1】:

    检查重构张量是否与源张量具有相同数量的元素的解决方案:

    // 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】:

      这应该可行:

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

      【讨论】:

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