TensorFlow Swift 高级 API。 Tweet

Swift 4.0 Platforms Linux & OS X GPL Octadero Twitter

API 结构

architecture

API 基于 TensorFlow 库。

使用库。

首先,您应该安装 tensorflow_c 库。 您可以在 Mac OS 上使用 brew 来执行此操作。 目前 1.4 版本可用于 mac oslinux on Google cloud,因此您可以使用它:此外,您可以从源代码安装它,您可以在此处找到如何从源代码安装 TensorFlow

教程

Xcode

请确保您已阅读子模块 CTensorFlow 的 README

要生成 xcode 项目文件,您可以调用:如果您将 TensorFlow 库(1.4.1 版本)安装为 brew 包

swift package -Xlinker -rpath -Xlinker /usr/local/Cellar/libtensorflow/1.4.1/lib/ generate-xcodeproj
swift package -Xlinker -rpath -Xlinker /server/repository/tensorflow/bazel-bin/tensorflow generate-xcodeproj

其中 /server/repository/tensorflow/bazel-bin/tensorflow 是指向您的 TensorFlow C 库的路径 /user/local/lib, 通常是.

您也可以使用配置

swift package generate-xcodeproj --xcconfig-overrides TensorFlow.xcconfig

构建并设置 RPATH 设置

#MacOS
swift build -Xlinker -rpath -Xlinker /usr/local/Cellar/libtensorflow/1.4.1/lib/
swift test -Xlinker -rpath -Xlinker /usr/local/Cellar/libtensorflow/1.4.1/lib/
#Linux
swift build -Xlinker -rpath -Xlinker /server/repository/tensorflow/bazel-bin/tensorflow
swift test -Xlinker -rpath -Xlinker /server/repository/tensorflow/bazel-bin/tensorflow

特性

Swift API 提供了对 TensorFlow 库中所有可用 C 特性的访问。

graph

总结

Summary

从 0.0.5 版本开始,您可以使用 TensorFlowKit 跟踪任何指标。 这是在 Swift 应用程序中可视化您的模型的简便方法。

您可以可视化权重和偏差

summary-distribution

绘制您的图表

summary-graph

以 3D 跟踪变化

summary-histogram

将组件提取为 png 图像

summary-image

观察变化的动态

summary-scalar

故障排除

otool -L libtensorflow_cc.so
sudo install_name_tool -id /%repository%/tensorflow/bazel-out/darwin_x86_64-opt/bin/tensorflow/libtensorflow_cc.so libtensorflow_cc.so
sudo install_name_tool -id /%repository%/tensorflow/bazel-out/darwin_x86_64-opt/bin/tensorflow/libtensorflow_framework.so libtensorflow_framework.so
sudo install_name_tool -id /%repository%/tensorflow/bazel-out/darwin_x86_64-opt/bin/tensorflow/libtensorflow.so libtensorflow.so
error while loading shared libraries: *libtensorflow_cc.so*: cannot open shared object file: No such file or directory

添加您的库路径 linux

export LD_LIBRARY_PATH=/server/repository/tensorflow/bazel-bin/tensorflow:/usr/local/lib/:$LD_LIBRARY_PATH

mac os

export DYLD_LIBRARY_PATH=/server/repository/tensorflow/bazel-bin/tensorflow/:$DYLD_LIBRARY_PATH

C++ 旧的相关问题。

warning: error while trying to use pkgConfig flags for CProtobuf: nonWhitelistedFlags("Non whitelisted flags found: [\"-D_THREAD_SAFE\", \"-D_THREAD_SAFE\"] in pc file protobuf")

从文件 /usr/local/lib/pkgconfig/protobuf.pc 中删除 -D_THREAD_SAFE

在构建阶段出错

#include "tensorflow/core/framework/graph.pb.h"                                                                                                                                         ^                                                                                                                                                                     1 error generated.                                                                                                                                                             error: terminated(1): /usr/bin/swift-build-tool -f /server/repository/TensorFlow/.build/debug.yaml main

在 tensorflow 存储库中下载 C++ 库的依赖项。

tensorflow/contrib/makefile/download_dependencies.sh

开发和扩展 API

创建新的 proto 文件

// Create temperory folder
mkdir /tmp/swift
cd %path-to-tensorflow-reposytory%

// Find all proto files and generate swift classes.
find. -name '*.proto' -print -exec protoc --swift_opt=Visibility=Public --swift_out=/tmp/swift {} \;

// All files will be removed after restart.
open /tmp/swift

调试

设置将记录哪些消息的阈值。 添加 TF_CPP_MIN_LOG_LEVEL=0TF_CPP_MIN_VLOG_LEVEL=0

操作列表

有几种方法可以获取已注册操作的 OpDefs 列表

TF_GetAllOpList in the C API retrieves all registered OpDef protocol messages. This can be used to write the generator in the client language. This requires that the client language have protocol buffer support in order to interpret the OpDef messages.

The C++ function OpRegistry::Global()->GetRegisteredOps() returns the same list of all registered OpDefs (defined in [tensorflow/core/framework/op.h]). This can be used to write the generator in C++ (particularly useful for languages that do not have protocol buffer support).

The ASCII-serialized version of that list is periodically checked in to [tensorflow/core/ops/ops.pbtxt] by an automated process.

OpProducer 使用 C API 提取并准备所有可用的操作作为 Swift 源代码。