{"id":15038381,"url":"https://github.com/zachgrayio/swift-tensorflow","last_synced_at":"2025-04-10T01:23:15.169Z","repository":{"id":90044427,"uuid":"131245916","full_name":"zachgrayio/swift-tensorflow","owner":"zachgrayio","description":"Dockerized Swift for TensorFlow and advanced usage examples.","archived":false,"fork":false,"pushed_at":"2019-04-26T21:03:24.000Z","size":21,"stargazers_count":14,"open_issues_count":1,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-24T03:04:35.159Z","etag":null,"topics":["docker","dockerfile","rxswift","swift","swift-4","swift-for-tensorflow","tensorflow"],"latest_commit_sha":null,"homepage":"","language":"Dockerfile","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zachgrayio.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-04-27T04:35:10.000Z","updated_at":"2020-06-22T09:22:49.000Z","dependencies_parsed_at":null,"dependency_job_id":"f5c63f4a-08b6-49df-ba10-5a5a0447e25f","html_url":"https://github.com/zachgrayio/swift-tensorflow","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zachgrayio%2Fswift-tensorflow","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zachgrayio%2Fswift-tensorflow/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zachgrayio%2Fswift-tensorflow/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zachgrayio%2Fswift-tensorflow/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zachgrayio","download_url":"https://codeload.github.com/zachgrayio/swift-tensorflow/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248138560,"owners_count":21053874,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["docker","dockerfile","rxswift","swift","swift-4","swift-for-tensorflow","tensorflow"],"created_at":"2024-09-24T20:38:14.148Z","updated_at":"2025-04-10T01:23:15.145Z","avatar_url":"https://github.com/zachgrayio.png","language":"Dockerfile","funding_links":[],"categories":[],"sub_categories":[],"readme":"# swift-tensorflow\n\nDockerized [Swift for TensorFlow](https://github.com/tensorflow/swift). This image is available now on Docker Hub at `zachgray/swift-tensorflow:4.2`.\n\n## Overview\n\nThis image will allow you to easily take the official [Swift for TensorFlow](https://github.com/tensorflow/swift) for a test drive without worrying about installing dependencies, changing your path, and interfering with your existing Swift/Xcode config.\n\n## Run\n### Run a REPL\n\n*Note: when running this interactive container with the standard `-it`, we also must [run without the default seccomp profile](https://docs.docker.com/engine/security/seccomp/) with `--security-opt seccomp:unconfined` to allow the Swift REPL access to `ptrace` and run correctly.*\n\n#### Run the `swift-tensorflow` container:\n\n```bash\ndocker run --rm --security-opt seccomp:unconfined -itv ${PWD}:/usr/src \\\n    zachgray/swift-tensorflow:4.2 \\\n    swift\n```\n\n#### Observe the output:\n\n```\nWelcome to Swift version 4.2-dev (LLVM fd66ce58db, Clang cca52e8396, Swift 280486afdc).\nType :help for assistance.\n  1\u003e\n```\n\n#### Interact with TensorFlow:\n\n```\n  1\u003e import TensorFlow\n  2\u003e var x = Tensor\u003cFloat\u003e([[1, 2], [3, 4]])\nx: TensorFlow.Tensor\u003cFloat\u003e = [[1.0, 2.0], [3.0, 4.0]]\n  3\u003e x + x\n$R2: TensorFlow.Tensor\u003cFloat\u003e = [[2.0, 4.0], [6.0, 8.0]]\n  4\u003e :exit\n```\n\n### Run the Interpreter\n\nAssuming you've added a swift file, like this one copied from [official docs](https://github.com/tensorflow/swift/blob/master/Usage.md#interpreter) in your current directory with the name `inference.swift`:\n\n```swift\nimport TensorFlow\n\nstruct MLPClassifier {\n    var w1 = Tensor\u003cFloat\u003e(shape: [2, 4], repeating: 0.1)\n    var w2 = Tensor\u003cFloat\u003e(shape: [4, 1], scalars: [0.4, -0.5, -0.5, 0.4])\n    var b1 = Tensor\u003cFloat\u003e([0.2, -0.3, -0.3, 0.2])\n    var b2 = Tensor\u003cFloat\u003e([[0.4]])\n\n    func prediction(for x: Tensor\u003cFloat\u003e) -\u003e Tensor\u003cFloat\u003e {\n        let o1 = tanh(matmul(x, w1) + b1)\n        return tanh(matmul(o1, w2) + b2)\n    }\n}\nlet input = Tensor\u003cFloat\u003e([[0.2, 0.8]])\nlet classifier = MLPClassifier()\nlet prediction = classifier.prediction(for: input)\nprint(prediction)\n```\n\nTo use the interpreter:\n\n```bash\ndocker run --rm -v ${PWD}:/usr/src \\\n    zachgray/swift-tensorflow:4.2 \\\n    swift -O /usr/src/inference.swift\n```\n\n### Run the Compiler\n\n```bash\ndocker run --rm -v ${PWD}:/usr/src zachgray/swift-tensorflow:4.2 \\\n    swiftc -O /usr/src/inference.swift -ltensorflow \n```\n\n## Run with Dependencies (advanced)\n\nImporting third-party packages in the REPL requires a few additional steps, but it's possible if we make use of [SPM](https://swift.org/package-manager/) and a dynamic library.\n\n### Package Manager Tutorial\n\nFor 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.\n\n*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`.*\n\n#### 1) Start the interactive session:\n\n```bash\ndocker run --rm -itv ${PWD}:/usr/src \\\n    zachgray/swift-tensorflow:4.2 \\\n    /bin/bash\n```\n\n#### 2) Create a library called `example`:\n\n```bash\nmkdir TFExample \ncd TFExample \nswift package init --type library\n```\n\n#### 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:\n\n```swift\n// swift-tools-version:4.0\n\nimport PackageDescription\n\nlet package = Package(\n    name: \"TFExample\",\n    products: [\n        .library(\n            name: \"TFExample\",\n            type: .dynamic,    // allow use of this package and it's deps from the REPL\n            targets: [\"TFExample\"]\n        )\n    ],\n    dependencies: [\n        .package(url: \"https://github.com/ReactiveX/RxSwift.git\", \"4.0.0\" ..\u003c \"5.0.0\")\n    ],\n    targets: [\n        .target(\n            name: \"TFExample\",\n            dependencies: [\"RxSwift\"]),\n        .testTarget(\n            name: \"TFExampleTests\",\n            dependencies: [\"TFExample\"]),\n    ]\n)\n```\n\n#### 4) Now fetch package dependencies:\n\n```bash\nswift package update\n```\n\n#### 5) Build the package:\n\n```bash\nswift build\n```\n\n#### 6) Once the build is complete, we will exit our interactive session:\n\n```\nexit\n```\n\n#### 7) Start the REPL in a container:\n\nNotice that we start the REPL in a similar manner to the examples above, but this time link to the built package.\n\n```bash\ndocker run --rm --security-opt seccomp:unconfined -itv ${PWD}:/usr/src \\\n    zachgray/swift-tensorflow:4.2 \\\n    swift \\\n    -I/usr/lib/swift/clang/include \\\n    -I/usr/src/TFExample/.build/debug \\\n    -L/usr/src/TFExample/.build/debug \\\n    -lswiftPython \\\n    -lswiftTensorFlow \\\n    -lTFExample\n```\n\n#### 8) Interact with the REPL:\n\nNow we can import dependences into the REPL session!\n\n```\nWelcome to Swift version 4.2-dev (LLVM 04bdb56f3d, Clang b44dbbdf44). Type :help for assistance.\n  1\u003e import RxSwift\n  2\u003e import Python\n  3\u003e import TensorFlow\n  4\u003e var x = Tensor([[1, 2], [3, 4]])\nx: TensorFlow.Tensor\u003cDouble\u003e = [[1.0, 2.0], [3.0, 4.0]]\n  5\u003e _ = Observable.from([1,2]).subscribe(onNext: { print($0) })\n1\n2\n  6\u003e var x: PyValue = [1, \"hello\", 3.14]\nx: Python.PyValue = [1, 'hello', 3.14]\n  7\u003e :exit\n```\n\n*Note: the Swift-related `-l` flags are currently necessary ([see discussion here](https://github.com/google/swift/issues/4)) 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.*\n\n## License\n\nThis project is [MIT Licensed](https://github.com/zachgrayio/swift-tensorflow/blob/master/LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzachgrayio%2Fswift-tensorflow","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzachgrayio%2Fswift-tensorflow","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzachgrayio%2Fswift-tensorflow/lists"}