机器学习&AI之c++随笔(1)-配置tensorflow并运行第一个C++程序
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^Tauto v = MatMul(root.WithOpName("v"), A, b, MatMul::TransposeB(true));std::vector<Tensor> outputs;ClientSession session(root);// Run and fetch vTF_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
編譯并運行,第一次編譯速度很慢:
[32?10][35]=[19?3]\begin{bmatrix} 3 & 2 \\ -1 & 0 \end{bmatrix} \begin{bmatrix} 3 \\ 5 \end{bmatrix}=\begin{bmatrix} 19 \\ -3 \end{bmatrix} [3?1?20?][35?]=[19?3?]
改下代碼,再次編譯運行,因為是第二次編譯了,所以為增量編譯,速度很快:
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^Tauto v = MatMul(root.WithOpName("v"), A, b, MatMul::TransposeB(true));std::vector<Tensor> outputs;ClientSession session(root);// Run and fetch vTF_CHECK_OK(session.Run({v}, &outputs));// Expect outputs[0] == [1; -3]LOG(INFO) << outputs[0].matrix<float>();return 0; }輸出:
lx@lx-Lenovo:~/soft/tensorflow-r1.13/tensorflow/cc/lx_learn$ bazel run -c opt //tensorflow/cc/lx_learn:learn1 INFO: Invocation ID: 1ba6c115-db48-467c-ab4b-d59509816757 INFO: Analysed target //tensorflow/cc/lx_learn:learn1 (0 packages loaded, 0 targets configured). INFO: Found 1 target... Target //tensorflow/cc/lx_learn:learn1 up-to-date:bazel-bin/tensorflow/cc/lx_learn/learn1 INFO: Elapsed time: 10.203s, Critical Path: 9.74s INFO: 2 processes: 2 local. INFO: Build completed successfully, 3 total actions INFO: Build completed successfully, 3 total actions 2019-04-30 17:20:07.677274: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 2019-04-30 17:20:07.679244: I tensorflow/cc/lx_learn/learn1.cc:20] 1 -3[-32?10][35]=[1?3]\begin{bmatrix} -3 & 2 \\ -1 & 0 \end{bmatrix} \begin{bmatrix} 3 \\ 5 \end{bmatrix}=\begin{bmatrix} 1 \\ -3 \end{bmatrix} [-3?1?20?][35?]=[1?3?]
總結
以上是生活随笔為你收集整理的机器学习&AI之c++随笔(1)-配置tensorflow并运行第一个C++程序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SpringBoot使用jasypt加解
- 下一篇: mxnet基础到提高(22)-C++-常