1、从源代码安装tensorflow-r1.13
2、建立目录
lx@lx-Lenovo:~/soft/tensorflow-r1.13/tensorflow/cc$ pwd
/home/lx/soft/tensorflow-r1.13/tensorflow/cc
lx@lx-Lenovo:~/soft/tensorflow-r1.13/tensorflow/cc$ mkdir lx_learn
lx@lx-Lenovo:~/soft/tensorflow-r1.13/tensorflow/cc$ cd lx_learn
lx@lx-Lenovo:~/soft/tensorflow-r1.13/tensorflow/cc/lx_learn$ pwd
/home/lx/soft/tensorflow-r1.13/tensorflow/cc/lx_learn
lx@lx-Lenovo:~/soft/tensorflow-r1.13/tensorflow/cc/lx_learn$
3、输入程序
lx@lx-Lenovo:~/soft/tensorflow-r1.13/tensorflow/cc/lx_learn$ vim learn1.cc
lx@lx-Lenovo:~/soft/tensorflow-r1.13/tensorflow/cc/lx_learn$ cat learn1.cc
#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();
// Matrix A = [3 2; -1 0]
auto A = Const(root, { {3.f, 2.f}, {-1.f, 0.f} });
// Vector b = [3 5]
auto b = Const(root, { {3.f, 5.f} });
// v = Ab^T
auto v = MatMul(root.WithOpName("v"), A, b, MatMul::TransposeB(true));
std::vector<Tensor> outputs;
ClientSession session(root);
// Run and fetch v
TF_CHECK_OK(session.Run({v}, &outputs));
// Expect outputs[0] == [19; -3]
LOG(INFO) << outputs[0].matrix<float>();
return 0;
}
lx@lx-Lenovo:~/soft/tensorflow-r1.13/tensorflow/cc/lx_learn$ cat BUILD
load("//tensorflow:tensorflow.bzl", "tf_cc_binary")
tf_cc_binary(
name = "learn1",
srcs = ["learn1.cc"],
deps = [
"//tensorflow/cc:cc_ops",
"//tensorflow/cc:client_session",
"//tensorflow/core:tensorflow",
],
)
lx@lx-Lenovo:~/soft/tensorflow-r1.13/tensorflow/cc$ ls
BUILD framework lx_learn profiler tools tutorials
client gradients ops saved_model training
lx@lx-Lenovo:~/soft/tensorflow-r1.13/tensorflow/cc$ cd lx_learn
lx@lx-Lenovo:~/soft/tensorflow-r1.13/tensorflow/cc/lx_learn$ ls
BUILD learn1.cc
lx@lx-Lenovo:~/soft/tensorflow-r1.13/tensorflow/cc/lx_learn$ bazel run -c opt //tensorflow/cc/lx_learn:learn1