如何使用 tensorflow 2.0 C API?

编程入门 行业动态 更新时间:2024-10-11 15:24:10
本文介绍了如何使用 tensorflow 2.0 C API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在尝试在 Xcode 的 C++ 项目中使用 tensorflow.我遵循了为 C 安装 TensorFlow".在他们的网页上找到的教程 (https://www.tensorflow/install/lang_c).

I am trying to use tensorflow in a C++ project in Xcode. I've followed the "Install TensorFlow for C" tutorial found on their web page (https://www.tensorflow/install/lang_c).

我必须更改 Xcode 项目的构建设置部分中的几个字段才能使其正常工作.

I had to change several fields in the build settings section of my Xcode project to make it work.

我添加了/usr/local/include"到标题搜索路径.我添加了/usr/local/lib";到图书馆搜索路径.我添加了-ltensorflow";到其他链接器标志.

执行此操作后,程序正确编译并打印:Hello from TensorFlow C library version 2.4.0".所以按照教程,安装成功,应该可以使用C API了.

After doing that, the program correctly compiles and prints : "Hello from TensorFlow C library version 2.4.0". So according to the tutorial, the installation was successful, and I should be able to use the C API.

我不明白的是我如何访问 API 本身.例如,我如何声明一个 tensorflow::Scope 类型的变量?

What I don't understand, is how I can access the API itself. For example how can I declare a variable of type tensorflow::Scope?

根据我在网上找到的其他教程(例如:https://itnext.io/creating-a-tensorflow-dnn-in-c-part-1-54ce69bbd586 )我应该包含位于 tensorflow/core/framework/... 但我的电脑上没有这样的文件.

According to other tutorials I found online (ex : https://itnext.io/creating-a-tensorflow-dnn-in-c-part-1-54ce69bbd586 ) I should include a files located at tensorflow/core/framework/... but I don't have such files on my computer.

Tensorflow 表示可以通过单个文件 tensorflow/c/c_api.h 访问整个 API.但是,我怎样才能使 using namespace tensorflow; 之类的东西起作用?

Tensorflow says that the entire API is accessible though the single file tensorflow/c/c_api.h. But then how can I make something like using namespace tensorflow; work?

我已经在这个问题上苦苦挣扎了好几天了,我真的希望有人能够帮助我.

I've been struggling on that issue for days now and I really hope someone will be able to help me.

推荐答案

您提供的示例 (tensorflow::Scope) 可以与 C++ 一起使用,但不能与 tensorflow C API 一起使用.对于这种指令,您需要从源代码构建 tensorflow(我发现这个 repo 非常有帮助 https://github/FloopCZ/tensorflow_cc).

The example you provided (tensorflow::Scope) can be used with C++ and and not with tensorflow C API. For this kind of instructions, you need to build the tensorflow from source (I found this repo very helpful https://github/FloopCZ/tensorflow_cc).

如果您想使用 C API,那么您可以调用在 c_api.h 中找到的所有函数.比如下面是我的C++文件(hello_tf_cpp.cpp):

In case you want to use C API then you can call all those functions that you find in c_api.h. For example, the following is my C++ file (hello_tf_cpp.cpp):

#include <iostream>
#include <tensorflow/c/c_api.h>

int main() {
  std::cout << "Hello from Tensorflow C++ library version " << TF_Version() << std::endl;
  //printf("Hello from TensorFlow C library version %s\n", TF_Version());

  TF_Graph* Graph = TF_NewGraph();
  TF_Status* Status = TF_NewStatus();
  TF_SessionOptions* SessionOpts = TF_NewSessionOptions();
  TF_Buffer* RunOpts = NULL;
  return 0;
}

这就是我编译和运行它的方式:

And this is how I compile and run it:

$ g++ -I/usr/local/include -L /usr/local/lib hello_tf_cpp.cpp -l tensorflow -o hello_tf_cpp
    $ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib
    $./hello_tf_cpp

    $ unset LD_LIBRARY_PATH

这是我得到的输出:你好来自 Tensorflow C++ 库版本 1.15.0

And this is the output I get: Hello from Tensorflow C++ library version 1.15.0

这篇关于如何使用 tensorflow 2.0 C API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-05-01 01:29:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1402373.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   tensorflow   API

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!