安装 C 版 TensorFlow
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
TensorFlow 提供了一个 C API,该 API 可用于为其他语言构建绑定。该 API 在 c_api.h
中定义,旨在实现简洁性和一致性,而不是便利性。
每夜版 Libtensorflow C 软件包
Libtensorflow 软件包是在夜间构建的,并会针对所有受支持平台上传到 GCS。软件包会上传到 libtensorflow-nightly GCS 存储分区,并按操作系统和构建日期编入索引。对于 MacOS 和 Linux 的共享对象,我们的脚本会根据当前日期重命名 .so 文件,这些文件已复制到包含工件的目录中。
以下系统支持 C 版 TensorFlow:
- Linux - 64 位,x86
- macOS - 版本 10.12.6 (Sierra) 或更高版本
- Windows - 64 位,x86
设置
下载
解压缩下载的归档文件,其中包含要添加到 C 程序中的头文件以及要与之关联的共享库。
在 Linux 和 macOS 上,您可能需要解压缩到 /usr/local/lib
:
sudo tar -C /usr/local -xzf (downloaded file)
链接器
在 Linux/macOS 上,如果将 TensorFlow C 库解压缩到系统目录(例如 /usr/local
),请使用 ldconfig
配置链接器:
sudo ldconfig
如果将 TensorFlow C 库解压缩到非系统目录(例如 ~/mydir
),请配置链接器环境变量:
Linux
export LIBRARY_PATH=$LIBRARY_PATH:~/mydir/lib
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:~/mydir/lib
macOS
export LIBRARY_PATH=$LIBRARY_PATH:~/mydir/lib
export DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:~/mydir/lib
构建
示例程序
安装 TensorFlow C 库后,使用以下源代码创建一个示例程序 (hello_tf.c
):
#include <stdio.h>
#include <tensorflow/c/c_api.h>
int main() {
printf("Hello from TensorFlow C library version %s\n", TF_Version());
return 0;
}
编译
编译示例程序以创建可执行文件,然后运行以下命令:
gcc hello_tf.c -ltensorflow -o hello_tf
./hello_tf
上述命令会输出:Hello from TensorFlow C library version number
如果程序无法构建,请确保 gcc
可以访问 TensorFlow C 库。如果解压缩到 /usr/local
,请将库位置明确传递给编译器:
gcc -I/usr/local/include -L/usr/local/lib hello_tf.c -ltensorflow -o hello_tf
从源代码构建
TensorFlow 是开源系统。请阅读相关说明,了解如何从源代码构建 TensorFlow 的 C 库。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2021-09-21。
[null,null,["最后更新时间 (UTC):2021-09-21。"],[],[],null,["# Install TensorFlow for C\n\n|---------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------|\n| [View on TensorFlow.org](https://www.tensorflow.org/install/lang_c) | [Run in Google Colab](https://colab.research.google.com/github/tensorflow/docs/blob/master/site/en/install/lang_c.ipynb) | [View source on GitHub](https://github.com/tensorflow/docs/blob/master/site/en/install/lang_c.ipynb) | [Download notebook](https://storage.googleapis.com/tensorflow_docs/docs/site/en/install/lang_c.ipynb) |\n\nTensorFlow provides a C API that can be used to build\n[bindings for other languages](https://github.com/tensorflow/docs/tree/master/site/en/r1/guide/extend/bindings.md).\nThe API is defined in\n[`c_api.h`](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/c/c_api.h)\nand designed for simplicity and uniformity rather than convenience.\n\nNightly libtensorflow C packages\n--------------------------------\n\nlibtensorflow packages are built nightly and uploaded to GCS for all supported\nplatforms. They are uploaded to the\n[libtensorflow-nightly GCS bucket](https://storage.googleapis.com/libtensorflow-nightly)\nand are indexed by operating system and date built. For MacOS and Linux shared\nobjects, there is a\n[script](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/tools/ci_build/builds/libtensorflow_nightly_symlink.sh)\nthat renames the `.so` files versioned to the current date copied into the\ndirectory with the artifacts.\n\nSupported Platforms\n-------------------\n\nTensorFlow for C is supported on the following systems:\n\n- Linux, 64-bit, x86\n- macOS, Version 10.12.6 (Sierra) or higher\n- Windows, 64-bit x86\n\nSetup\n-----\n\n### Download and extract\n\n\u003cbr /\u003e\n\n| TensorFlow C library | URL |\n|----------------------|-------------------------------------------------------------------------------------------------------|\n| Linux | **Caution:** TensorFlow 2.16 was the last TensorFlow release that supported macOS x86. | **Caution:** TensorFlow 2.18 was the last release of Linux x86 libtensorflow packages. ||\n| Linux CPU only | \u003chttps://storage.googleapis.com/tensorflow/versions/2.18.0/libtensorflow-cpu-linux-x86_64.tar.gz\u003e |\n| Linux GPU support | \u003chttps://storage.googleapis.com/tensorflow/versions/2.18.0/libtensorflow-gpu-linux-x86_64.tar.gz\u003e |\n| macOS | **Caution:** TensorFlow 2.16 was the last TensorFlow release that supported macOS x86. | **Caution:** TensorFlow 2.18 was the last release of Mac Arm64 Libtensorflow packages. ||\n| macOS CPU only | \u003chttps://storage.googleapis.com/tensorflow/versions/2.16.2/libtensorflow-cpu-darwin-x86_64.tar.gz\u003e |\n| macOS ARM64 CPU only | \u003chttps://storage.googleapis.com/tensorflow/versions/2.18.0/libtensorflow-cpu-darwin-arm64.tar.gz\u003e |\n| Windows | **Caution:** TensorFlow 2.10 was the last TensorFlow release that supported GPU on native-Windows. | **Caution:** TensorFlow 2.18 was the last release of Windows x86 libtensorflow packages. ||\n| Windows CPU only | \u003chttps://storage.googleapis.com/tensorflow/versions/2.18.1/libtensorflow-cpu-windows-x86_64.zip\u003e |\n| Windows GPU only | \u003chttps://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-gpu-windows-x86_64-2.10.0.zip\u003e |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nExtract the downloaded archive, which contains the header files to include in\nyour C program and the shared libraries to link against.\n\nOn Linux and macOS, you may want to extract to `/usr/local/lib`: \n\n FILENAME=libtensorflow-cpu-linux-x86_64.tar.gz\n wget -q --no-check-certificate https://storage.googleapis.com/tensorflow/versions/2.18.1/${FILENAME}\n sudo tar -C /usr/local -xzf ${FILENAME}\n\n### Linker\n\nOn Linux/macOS, if you extract the TensorFlow C library to a system directory,\nsuch as `/usr/local`, configure the linker with `ldconfig`: \n\n sudo ldconfig /usr/local/lib\n\nIf you extract the TensorFlow C library to a non-system directory, such as\n`~/mydir`, then configure the linker environmental variables:\n\n\u003cbr /\u003e\n\n### Linux\n\n```bash\nexport LIBRARY_PATH=$LIBRARY_PATH:~/mydir/lib\nexport LD_LIBRARY_PATH=$LD_LIBRARY_PATH:~/mydir/lib\n```\n\n### macOS\n\n```bash\nexport LIBRARY_PATH=$LIBRARY_PATH:~/mydir/lib\nexport DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:~/mydir/lib\n```\n\n\u003cbr /\u003e\n\nBuild\n-----\n\n### Example program\n\nWith the TensorFlow C library installed, create an example program with the\nfollowing source code (`hello_tf.c`): \n\n %%writefile hello_tf.c\n #include \u003cstdio.h\u003e\n #include \u003ctensorflow/c/c_api.h\u003e\n\n int main() {\n printf(\"Hello from TensorFlow C library version %s\\n\", TF_Version());\n return 0;\n }\n\n```\nWriting hello_tf.c\n```\n\n### Compile\n\nCompile the example program to create an executable, then run: \n\n gcc hello_tf.c -ltensorflow -o hello_tf\n\n ./hello_tf\n\n```\nHello from TensorFlow C library version 2.18.1\n```\n| **Success:** The TensorFlow C library is configured.\n\nIf the program doesn't build, make sure that `gcc` can access the TensorFlow C\nlibrary. If extracted to `/usr/local`, explicitly pass the library location to\nthe compiler: \n\n gcc -I/usr/local/include -L/usr/local/lib hello_tf.c -ltensorflow -o hello_tf\n\n ./hello_tf\n\n```\nHello from TensorFlow C library version 2.18.1\n```\n\nBuild from source\n-----------------\n\nTensorFlow is open source. Read\n[the instructions](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/tools/lib_package/README.md)\nto build TensorFlow's C library from source code."]]