简体   繁体   English

Tensorflow:用C ++打印张量的内容

[英]Tensorflow: Print contents of a tensor in C++

How do I print to screen contents of a tensor defined as below 如何打印到下面定义的张量的屏幕内容

std::vector<tensorflow::Tensor> finalOutput;

and which is assigned value by running the following operation 并通过运行以下操作赋值

tensorflow::Status run_status = session->Run({{"x",input_tensor}, 
                                                       {"keep_prob", keep_prob}},
                                                      {"prediction"},
                                                      {},
                                                  &finalOutput);

For example : 例如

// The session will initialize the outputs
std::vector<tensorflow::Tensor> outputs;

// Run the session, evaluating our "c" operation from the graph
status = session->Run(inputs, {"c"}, {}, &outputs);
if (!status.ok()) {
  std::cout << status.ToString() << "\n";
  return 1;
}

// Grab the first output (we only evaluated one graph node: "c")
// and convert the node to a scalar representation.
auto output_c = outputs[0].scalar<float>();

// (There are similar methods for vectors and matrices here:
// https://github.com/tensorflow/tensorflow/blob/master/tensorflow/core/public/tensor.h)

// Print the results
std::cout << outputs[0].DebugString() << "\n"; // Tensor<type: float shape: [] values: 30>
std::cout << output_c() << "\n"; // 30

To print a d -dimensional tensorflow::Tensor T 打印d维张量流:: Tensor T

#define printTensor(T, d) \
    std::cout<< (T).tensor<float, (d)>() << std::endl

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

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