Giter Site home page Giter Site logo

zachgrayio / swift-tensorflow Goto Github PK

View Code? Open in Web Editor NEW
14.0 4.0 1.0 21 KB

Dockerized Swift for TensorFlow and advanced usage examples.

License: MIT License

Dockerfile 100.00%
swift swift-4 docker dockerfile tensorflow swift-for-tensorflow rxswift

swift-tensorflow's Introduction

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.

swift-tensorflow's People

Contributors

zachgrayio avatar zou000 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

johndpope

swift-tensorflow's Issues

Cannot import TensorFlow

I'm running the 4.2 image on Docker for Windows and run a container via
docker run --rm --security-opt seccomp:unconfined -itv %CD%:/usr/src zachgray/swift-tensorflow:4.2 swift (%CD% being windows version of pwd)

When I run import TensorFlow in the REPL I see

<module-includes>:1:10: note: in file included from <module-includes>:1:
#include "c_api.h"
         ^

error: /usr/lib/swift/linux/x86_64/modulemaps/CTensorFlow/c_api.h:19:10: error: 'stddef.h' file not found
#include <stddef.h>
         ^

/usr/include/stdint.h:25:10: note: while building module 'SwiftGlibc' imported from /usr/include/stdint.h:25:
#include <features.h>
         ^

<module-includes>:3:10: note: in file included from <module-includes>:3:
#include "///usr/include/utmp.h"
         ^

///usr/include/utmp.h:23:10: note: in file included from ///usr/include/utmp.h:23:
#include <sys/types.h>
         ^

error: /usr/include/x86_64-linux-gnu/sys/types.h:146:10: error: 'stddef.h' file not found
#include <stddef.h>
         ^

error: could not build C module 'CTensorFlow'

Is this a Docker for windows issue or an image issue?

Docker command typo

In the "Run the interpreter" section of the README, there's a typo:

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

In the command, run must come before --rm: docker run --rm ....

Docker pull must have a specific tag

The latest can not be found.

docker pull zachgray/swift-tensorflow:4.2
docker run  --privileged --cap-add sys_ptrace -it --rm zachgray/swift-tensorflow:4.2 swift -I/usr/lib/swift/clang/include

image

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.