Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tensorflow: Print contents of a tensor in C++

Tags:

c++

tensorflow

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);
like image 919
Effective_cellist Avatar asked Jul 15 '17 05:07

Effective_cellist


2 Answers

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
like image 150
P-Gn Avatar answered Sep 24 '22 07:09

P-Gn


To print a d-dimensional tensorflow::Tensor T

#define printTensor(T, d) \
    std::cout<< (T).tensor<float, (d)>() << std::endl
like image 41
user2702031 Avatar answered Sep 26 '22 07:09

user2702031