All Projects → zachgrayio → swift-tensorflow

zachgrayio / swift-tensorflow

Licence: MIT license
Dockerized Swift for TensorFlow and advanced usage examples.

Programming Languages

Dockerfile
14818 projects

Projects that are alternatives of or similar to swift-tensorflow

Lockwise Ios
Firefox's Lockwise app for iOS
Stars: ✭ 224 (+1500%)
Mutual labels:  rxswift
RxSwift-VIPER-iOS
RxSwiftVIPER is an sample iOS App written in RxSwift using the VIPER architecture. Also RxSwiftVIPER is not a strict VIPER architecture.
Stars: ✭ 47 (+235.71%)
Mutual labels:  rxswift
RxSwift-MVVM-iOS
SwiftMVVM is an sample iOS App written in Swift using the MVVM architecture.
Stars: ✭ 96 (+585.71%)
Mutual labels:  rxswift
Rxswift Tutorial
RxSwift 学习资料(学习教程、开源项目)
Stars: ✭ 236 (+1585.71%)
Mutual labels:  rxswift
BringMyOwnBeer-
PunkAPI(BrewDog) 을 이용한 RxSwift-MVVM 예제 (Naver Tech Concert)
Stars: ✭ 80 (+471.43%)
Mutual labels:  rxswift
minimalist
Observable Property and Signal for building data-driven UI without Rx
Stars: ✭ 88 (+528.57%)
Mutual labels:  rxswift
V2ex
An iOS client written in Swift for V2EX
Stars: ✭ 208 (+1385.71%)
Mutual labels:  rxswift
RxSwift-Xcode-Templates
A handful of Xcode file templates for projects that use RXSwift and MVVM
Stars: ✭ 77 (+450%)
Mutual labels:  rxswift
deep-learning-in-s4tf
Get started with Swift for TensorFlow by examples
Stars: ✭ 31 (+121.43%)
Mutual labels:  swift-for-tensorflow
Swift-Viper-Weather-App
iOS app with Clean Architecture
Stars: ✭ 20 (+42.86%)
Mutual labels:  rxswift
Rxdatasources
UITableView and UICollectionView Data Sources for RxSwift (sections, animated updates, editing ...)
Stars: ✭ 2,784 (+19785.71%)
Mutual labels:  rxswift
Passcode
🔑 Passcode for iOS Rxswift, ReactorKit and IGListKit example
Stars: ✭ 254 (+1714.29%)
Mutual labels:  rxswift
RxScreenProtectKit
Protect the screen from recording 🔐
Stars: ✭ 17 (+21.43%)
Mutual labels:  rxswift
Rxpermission
RxSwift bindings for Permissions API in iOS.
Stars: ✭ 234 (+1571.43%)
Mutual labels:  rxswift
mvvm-ios
A sample app to demonstrate MVVM implementation in iOS
Stars: ✭ 26 (+85.71%)
Mutual labels:  rxswift
Recaptcha
[In]visible ReCaptcha v2 for iOS
Stars: ✭ 208 (+1385.71%)
Mutual labels:  rxswift
Cyanic
Declarative, state-driven UI framework
Stars: ✭ 32 (+128.57%)
Mutual labels:  rxswift
HikingClub iOS
매시업산악회11기 iOS팀 열정! 열정! 열정!🔥🔥
Stars: ✭ 12 (-14.29%)
Mutual labels:  rxswift
KDInstagram
Instagram Clone built in Swift. Utilize three design patterns in three major modules.
Stars: ✭ 119 (+750%)
Mutual labels:  rxswift
iOS Started Kit
iOS Started Kit: Clean Architecture + RxSwift + Moya
Stars: ✭ 12 (-14.29%)
Mutual labels:  rxswift

swift-tensorflow

Dockerized Swift for TensorFlow. This image is available now on Docker Hub at zachgray/swift-tensorflow:4.2.

Overview

This image will allow you to easily take the official Swift for TensorFlow for a test drive without worrying about installing dependencies, changing your path, and interfering with your existing Swift/Xcode config.

Run

Run a REPL

Note: when running this interactive container with the standard -it, we also must run without the default seccomp profile with --security-opt seccomp:unconfined to allow the Swift REPL access to ptrace and run correctly.

Run the swift-tensorflow container:

docker run --rm --security-opt seccomp:unconfined -itv ${PWD}:/usr/src \
    zachgray/swift-tensorflow:4.2 \
    swift

Observe the output:

Welcome to Swift version 4.2-dev (LLVM fd66ce58db, Clang cca52e8396, Swift 280486afdc).
Type :help for assistance.
  1>

Interact with TensorFlow:

  1> import TensorFlow
  2> var x = Tensor<Float>([[1, 2], [3, 4]])
x: TensorFlow.Tensor<Float> = [[1.0, 2.0], [3.0, 4.0]]
  3> x + x
$R2: TensorFlow.Tensor<Float> = [[2.0, 4.0], [6.0, 8.0]]
  4> :exit

Run the Interpreter

Assuming you've added a swift file, like this one copied from official docs in your current directory with the name inference.swift:

import TensorFlow

struct MLPClassifier {
    var w1 = Tensor<Float>(shape: [2, 4], repeating: 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>([[0.4]])

    func prediction(for x: Tensor<Float>) -> Tensor<Float> {
        let o1 = tanh(matmul(x, w1) + b1)
        return tanh(matmul(o1, w2) + b2)
    }
}
let input = Tensor<Float>([[0.2, 0.8]])
let classifier = MLPClassifier()
let prediction = classifier.prediction(for: input)
print(prediction)

To use the interpreter:

docker run --rm -v ${PWD}:/usr/src \
    zachgray/swift-tensorflow:4.2 \
    swift -O /usr/src/inference.swift

Run the Compiler

docker run --rm -v ${PWD}:/usr/src zachgray/swift-tensorflow:4.2 \
    swiftc -O /usr/src/inference.swift -ltensorflow 

Run with Dependencies (advanced)

Importing third-party packages in the REPL requires a few additional steps, but it's possible if we make use of SPM and a dynamic library.

Package Manager Tutorial

For the sake of simplicity we'll run all of these commands in interactive mode from within the Docker container. Keep in mind that since we've mounted the current directory as a container volume which we're working in, changes here will be reflected in your host filesystem.

Note: if you wanted to run these commands from outside of the container, as we did the previous examples, you'd simple include the following before each swift binary interaction: docker run --rm -v ${PWD}:/usr/src zachgray/swift-tensorflow:4.2.

1) Start the interactive session:

docker run --rm -itv ${PWD}:/usr/src \
    zachgray/swift-tensorflow:4.2 \
    /bin/bash

2) Create a library called example:

mkdir TFExample 
cd TFExample 
swift package init --type library

3) Add some third-party dependencies to Package.swift, and make the library dynamic so we can import it and it's dependencies. Here's an example:

// swift-tools-version:4.0

import PackageDescription

let package = Package(
    name: "TFExample",
    products: [
        .library(
            name: "TFExample",
            type: .dynamic,    // allow use of this package and it's deps from the REPL
            targets: ["TFExample"]
        )
    ],
    dependencies: [
        .package(url: "https://github.com/ReactiveX/RxSwift.git", "4.0.0" ..< "5.0.0")
    ],
    targets: [
        .target(
            name: "TFExample",
            dependencies: ["RxSwift"]),
        .testTarget(
            name: "TFExampleTests",
            dependencies: ["TFExample"]),
    ]
)

4) Now fetch package dependencies:

swift package update

5) Build the package:

swift build

6) Once the build is complete, we will exit our interactive session:

exit

7) Start the REPL in a container:

Notice that we start the REPL in a similar manner to the examples above, but this time link to the built package.

docker run --rm --security-opt seccomp:unconfined -itv ${PWD}:/usr/src \
    zachgray/swift-tensorflow:4.2 \
    swift \
    -I/usr/lib/swift/clang/include \
    -I/usr/src/TFExample/.build/debug \
    -L/usr/src/TFExample/.build/debug \
    -lswiftPython \
    -lswiftTensorFlow \
    -lTFExample

8) Interact with the REPL:

Now we can import dependences into the REPL session!

Welcome to Swift version 4.2-dev (LLVM 04bdb56f3d, Clang b44dbbdf44). Type :help for assistance.
  1> import RxSwift
  2> import Python
  3> import TensorFlow
  4> var x = Tensor([[1, 2], [3, 4]])
x: TensorFlow.Tensor<Double> = [[1.0, 2.0], [3.0, 4.0]]
  5> _ = Observable.from([1,2]).subscribe(onNext: { print($0) })
1
2
  6> var x: PyValue = [1, "hello", 3.14]
x: Python.PyValue = [1, 'hello', 3.14]
  7> :exit

Note: the Swift-related -l flags are currently necessary (see discussion here) but may eventually become redundant. Also, while they're relevant, the order in which the flags are passed matters! Be sure to link your dynamic library after the Swift libs.

License

This project is MIT Licensed.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].