日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

机器学习&AI之c++随笔(1)-配置tensorflow并运行第一个C++程序

發布時間:2025/3/12 c/c++ 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 机器学习&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
編譯并運行,第一次編譯速度很慢:

lx@lx-Lenovo:~/soft/tensorflow-r1.13/tensorflow/cc/lx_learn$ bazel run -c opt //tensorflow/cc/lx_learn:learn1 INFO: Invocation ID: 86cff3f3-3f92-4ada-a042-557912ae44f7 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: 0.346s, Critical Path: 0.01s INFO: 0 processes. INFO: Build completed successfully, 1 total action INFO: Build completed successfully, 1 total action 2019-04-30 17:17:43.870014: 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:17:43.871505: I tensorflow/cc/lx_learn/learn1.cc:20] 19 -3

[32?10][35]=[19?3]\begin{bmatrix} 3 &amp; 2 \\ -1 &amp; 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 &amp; 2 \\ -1 &amp; 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++程序的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。