swift CLI and xcode gives contradict results

Created on 27 Apr 2018  Â·  5Comments  Â·  Source: tensorflow/swift

screen shot 2018-04-27 at 8 22 18 pm
I followed the Mac install instructions and configured xcode to use TF toolchain instead of the default one.

screen shot 2018-04-27 at 8 22 08 pm
However, I'm not able to compile and run the code within xcode.

I can run it using the CLI tool though
swift -O main.swift
[[0.680704]]

Any thoughts on what could be the root cause?

Here is the current environment setting
$ ls -l /Library/Developer/Toolchains/
total 0
lrwxr-xr-x 1 root wheel 83 Apr 27 19:53 swift-latest -> /Library/Developer/Toolchains/swift-tensorflow-DEVELOPMENT-2018-04-26-a.xctoolchain
drwxr-xr-x 7 root wheel 224 Apr 27 19:53 swift-tensorflow-DEVELOPMENT-2018-04-26-a.xctoolchain

$ env
PATH=/Library/Developer/Toolchains/swift-latest/usr/bin:/Users/wangtz/Downloads/google-cloud-sdk/bin:/Users/wangtz/.opam/4.02.3+buckle-master/bin:/Users/wangtz/homebrew/bin:/Users/wangtz/bin:/usr/local/share/python:/usr/local/git/current/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/local/go/bin:/opt/X11/bin

Thanks!

Most helpful comment

Final version you could find on my GitHub

All 5 comments

I fixed code:

//
//  main.swift
//  TensotFlowTest
//
//  Created by Volodymyr Pavliukevych on 4/27/18.
//  Copyright © 2018 Octadero. All rights reserved.
//
import Foundation
import TensorFlow

struct MLPClassifier {
    var w1 = Tensor<Float>(shape: [2, 4], scalars: [0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1])
    var w2 = Tensor<Float>(shape: [4, 1], scalars: [0.4, -0.5, -0.5, 0.4])
    var b1 = Tensor<Float>([0.2, -0.3, -0.3, 0.2])
    var b2 = Tensor<Float>(shape: [1, 1], scalars: [0.4])


    func prediction(for x: Tensor<Float>) -> Tensor<Float> {
        // The ⊗ operator performs matrix multiplication.
        let o1 = tanh(x ⊗ w1 + b1)
        return tanh(o1 ⊗ w2 + b2)
    }
}
let input = Tensor<Float>(shape: [1, 2], scalars: [0.2, 0.8])


let classifier = MLPClassifier()
let prediction = classifier.prediction(for: input)
print(prediction)

Compile and run:

/Library/Developer/Toolchains/swift-tensorflow-DEVELOPMENT-2018-04-26-a.xctoolchain/usr/bin/swift -O  /Users/roaming/Desktop/TensotFlowTest/TensotFlowTest/main.swift -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk 
Result:  [[0.680704]]

To launch in Xcode project you should change build settings:
1) Generate debug symbols - Turn off

2) Runpath search path - /Library/Developer/Toolchains/swift-latest/usr/lib/swift/macosx/
screen shot 2018-04-27 at 6 22 21 pm

3) Add Libraries: libtensorflow.so & libtensorflow_framework.so to linked frameworks.
screen shot 2018-04-27 at 6 27 18 pm

And that is it:
screen shot 2018-04-27 at 6 27 13 pm

Final version you could find on my GitHub

Thank you @VolodymyrPavliukevych !

It helps. Thanks!

For future reference, if you don't know where the two .so files are located, There are under /Library/Developer/Toolchains/swift-tensorflow-DEVELOPMENT-2018-04-26-a.xctoolchain/usr/lib/swift/macosx

Was this page helpful?
0 / 5 - 0 ratings