2017-07-15 106 views
1

如何打印到定义如下Tensorflow:C++中的张量

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

和张量的屏幕内容的打印内容通过运行下面的操作

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

回答

3

对于分配的值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 
2

要打印d-dimensional tensorflow :: Tensor T

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