【问题标题】:Concatenating Tensors in C++ -在 C++ 中连接张量 -
【发布时间】:2019-06-06 05:35:42
【问题描述】:

我有两个 dim=1x224x224x3 的张量 T1 和 dim=1x224x224x3 的 T2。我想将这 2 个张量连接成单个张量,例如昏暗 2x224x224x3 的 T3。 我不知道用 C++ 怎么做。

【问题讨论】:

  • 你如何存储你的张量?
  • 变量为Tensor T1, T2;
  • C++ 没有张量,你要么必须使用第三方库,要么自己创建
  • 是的,我使用过 Tensorflow。所以在导入 tensorflow/core/framework/tensor.h 之后。我可以使用“Tensor t1”关键字。实际上有一个文档,但不清楚如何连接 2 张量。
  • C++ API 指南展示了如何在 C++ 中使用 TensorFlow 运算,包括一个 Concat 示例。或者您可以使用Tensor::tensor 获取内部Eigen tensor 并使用concatenate

标签: c++ tensorflow concat


【解决方案1】:

C++ API 展示了如何在 C++ 中使用操作。对于Concat,你可以这样做:

#include "tensorflow/cc/client/client_session.h"
#include "tensorflow/cc/ops/standard_ops.h"
#include "tensorflow/core/framework/tensor.h"

int main() {
  using namespace tensorflow;
  using namespace tensorflow::ops;
  Scope root = Scope::NewRootScope();
  // Tensors to concatenate
  auto t1 = Const(root, { {1.f, 2.f}, {3.f, 4.f} });
  auto t2 = Const(root, { {5.f, 6.f}, {7.f, 8.f}, {9.f, 0.f} });
  // Concatenate
  auto concatT1T2 = Concat(root.WithOpName("ConcatT1T2"), { t1, t2 }, 0);
  // Evaluate
  std::vector<Tensor> outputs;
  ClientSession session(root);
  TF_CHECK_OK(session.Run({concatT1T2}, &outputs));
  // Get output tensor
  Tensor result = outputs[0];
  // Print output
  LOG(INFO) << result.matrix<float>();
  return 0;
}

【讨论】:

    猜你喜欢
    • 2017-08-20
    • 2021-05-17
    • 2021-10-30
    • 2019-07-10
    • 2021-11-20
    • 2018-09-20
    • 1970-01-01
    • 1970-01-01
    • 2018-04-11
    相关资源
    最近更新 更多