c++中tensorflow接口GPU使用情况,通过一个小代码看下:
with tf.Session() as sess:
x = tf.placeholder(tf.float32, [None, 32], name="x")
y = tf.placeholder(tf.float32, [None, 8], name="y")
w1 = tf.Variable(tf.truncated_normal([32, 16], stddev=0.1))
b1 = tf.Variable(tf.constant(0.0, shape=[16]))
w2 = tf.Variable(tf.truncated_normal([16, 8], stddev=0.1))
b2 = tf.Variable(tf.constant(0.0, shape=[8]))
a = tf.nn.tanh(tf.nn.bias_add(tf.matmul(x, w1), b1))
y_out = tf.nn.tanh(tf.nn.bias_add(tf.matmul(a, w2), b2), name="y_out")
cost = tf.reduce_sum(tf.square(y-y_out), name="cost")
optimizer = tf.train.AdamOptimizer().minimize(cost, name="train")
init = tf.initialize_variables(tf.all_variables(), name='init_all_vars_op')
tf.train.write_graph(sess.graph_def,
'./',
'mlp.pb', as_text=False)
c++代码,好像目前的高版本的tensorflow c++接口不再需要用sess.close,看源码知道析构函数会自动释放,强制性,其中TF_CHECK_OK是个好东西。实际还有其他很多小问题:
3
down vote
accepted
Since this question is tagged with C++. The solution is
tensorflow::Session *sess;
tensorflow::SessionOptions options;
tensorflow::ConfigProto* config = &options.config;
// disabled GPU entirely
(*config->mutable_device_count())["GPU"] = 0;
// place nodes somewhere
config->set_allow_soft_placement(true);
See the example here. And my other post, how TensorFlow places the nodes.
edit: There is GitHub issue. You can try:
#include <stdlib.h>
setenv("CUDA_VISIBLE_DEVICES", "", 1);
#include "tensorflow/core/public/session.h"
#include "tensorflow/core/graph/default_device.h"
using namespace tensorflow;
int main(int argc, char* argv[]) {
std::string graph_definition = "mlp.pb";
Session* session;
GraphDef graph_def;
SessionOptions opts;
std::vector<Tensor> outputs; // Store outputs
TF_CHECK_OK(ReadBinaryProto(Env::Default(), graph_definition, &graph_def));
// Set GPU options
graph::SetDefaultDevice("/gpu:0", &graph_def);
opts.config.mutable_gpu_options()->set_per_process_gpu_memory_fraction(0.5);
opts.config.mutable_gpu_options()->set_allow_growth(true);
// create a new session
TF_CHECK_OK(NewSession(opts, &session));
// Load graph into session
TF_CHECK_OK(session->Create(graph_def));
// Initialize our variables
TF_CHECK_OK(session->Run({}, {}, {"init_all_vars_op"}, nullptr));
Tensor x(DT_FLOAT, TensorShape({100, 32}));
Tensor y(DT_FLOAT, TensorShape({100, 8}));
auto _XTensor = x.matrix<float>();
auto _YTensor = y.matrix<float>();
_XTensor.setRandom();
_YTensor.setRandom();
for (int i = 0; i < 10; ++i) {
TF_CHECK_OK(session->Run({{"x", x}, {"y", y}}, {"cost"}, {}, &outputs)); // Get cost
float cost = outputs[0].scalar<float>()(0);
std::cout << "Cost: " << cost << std::endl;
TF_CHECK_OK(session->Run({{"x", x}, {"y", y}}, {}, {"train"}, nullptr)); // Train
outputs.clear();
}
return 0;
}
使用serving格式设置:
#include <iostream>
#include <vector>
#include "tensorflow/cc/saved_model/loader.h"
#include "tensorflow/core/framework/graph.pb.h"
#include "tensorflow/core/protobuf/meta_graph.pb.h"
#include "tensorflow/cc/saved_model/tag_constants.h"
#include <tensorflow/core/public/session.h>
#include <tensorflow/core/graph/default_device.h>
#include <tensorflow/core/graph/graph_def_builder.h>
using namespace std;
using namespace tensorflow;
int main(int argc ,char *argv[]) {
string modelpath;
if(argc<2){
modelpath = "/Users/xxxx/Documents/python/credit-nlp-ner/bi-lstm-crf/model/1";
}else{
modelpath=argv[1];
}
tensorflow::SessionOptions sess_options;
// tensorflow::ConfigProto config=sess_options.config;
// config.mutable_gpu_options()->set_visible_device_list("0");
// config.mutable_gpu_options()->set_allow_growth(true);
// config.set_allow_soft_placement(true);
tensorflow::RunOptions run_options;
tensorflow::SavedModelBundle bundle;
TF_CHECK_OK(tensorflow::LoadSavedModel(sess_options, run_options, modelpath, {tensorflow::kSavedModelTagServe}, &bundle));
tensorflow::MetaGraphDef graph_def = bundle.meta_graph_def;
std::unique_ptr<tensorflow::Session>& session = bundle.session;
GraphDef graphdef;
session->Create(graphdef);
//gpu设置
graph::SetDefaultDevice("/gpu:0", &graphdef);
sess_options.config.mutable_gpu_options()->set_per_process_gpu_memory_fraction(0.5);
sess_options.config.mutable_gpu_options()->set_allow_growth(true);
vector<int> vec={7997, 1945, 8471, 14127, 17565, 7340, 20224, 17529, 3796, 16033, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int ndim=vec.size();
Tensor x(tensorflow::DT_INT32, tensorflow::TensorShape({1, ndim})); // New Tensor shape [1, ndim]
auto x_map = x.tensor<int, 2>();
for (int j = 0; j < ndim; j++) {
x_map(0, j) = vec[j];
}
std::vector<std::pair<string, tensorflow::Tensor>> inputs;
inputs.push_back(std::pair<std::string, tensorflow::Tensor>("input_x", x));
Tensor keep_prob(tensorflow::DT_FLOAT, tensorflow::TensorShape({1}));
keep_prob.vec<float>()(0) = 1.0f;
inputs.push_back(std::pair<std::string, tensorflow::Tensor>("keep_prob", keep_prob));
Tensor tensor_out(tensorflow::DT_INT32, TensorShape({1,ndim}));
std::vector<tensorflow::Tensor> outputs={{ tensor_out }};
TF_CHECK_OK(session->Run(inputs, {"crf_pred/ReverseSequence_1"}, {}, &outputs));
for(int i=0;i<40;++i) {
std::cout << outputs[0].matrix<int>()(0,i)<<" ";
}
return 0;
}
可以简单的看看效果,