作为上述答案的综合,以及我现在对 Google 集团 2020 年 5 月 8 日最新版本和评论的经验,我发现:
成功
- Xcode 项目命令行(如上面的@8bitmp3 回答)
- 命令行 swiftc(使用 -O 参数。注意这是编译器)
失败
- Xcode Playground macOS(正如@ian-do 最初询问的那样)
- 命令行 swift(并由 Google Group 确认。注意这是解释器)
对于那些对命令行成功(macOS 10.15.4)感兴趣的人,从这里安装工具链,在这个页面创建源代码文件 inference.swift ,然后执行以下命令:
# Test this command. Expected outcome is your existing Xcode app as show below
$ xcrun -f swift
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift
# Switch to the new TensorFlow toolchain you installed
# Mine was located at /Library/Developer/Toolchains/swift-tensorflow-RELEASE-0.9.xctoolchain/
# Open the Info.plist file, locate CFBundleIdentifier, and copy the string value
# Mine was com.google.swift.20200507
$ export TOOLCHAINS=com.google.swift.20200507
# Test the switch to your TensorFlow toolchain. Note the different result
$ xcrun -f swift
/Library/Developer/Toolchains/swift-tensorflow-RELEASE-0.9.xctoolchain/usr/bin/swift
# Compile your Swift for TensorFlow source code
$ cd <directory with .swift file>
$ swiftc -O -sdk `xcrun --show-sdk-path` inference.swift
# Run the program
$ ./inference
[[0.68070436]]