Giter Site home page Giter Site logo

philipturner / differentiation-ios-demo Goto Github PK

View Code? Open in Web Editor NEW
8.0 3.0 1.0 6.7 MB

Running Swift automatic differentiation on iOS

Home Page: https://forums.swift.org/t/swift-for-tensorflow-resurrection-differentiation-running-on-ios/53841

License: MIT License

Swift 100.00%
autodiff swift arheadsetkit swift-for-tensorflow ios

differentiation-ios-demo's Introduction

Differentiation Demo

This is an example of Swift's automatic differentiation running on iOS. It is a modified version of the game from ARHeadsetKit tutorial #8, where the user knocks out cubes with their hand. Automatic differentiation computes a cube's velocity from its equations of motion, then shows the velocity in text.

Differentiation is disabled in the Swift 5.5 toolchain due to compiler instability, but can be activated by importing the Differentiation package. The following code segment shows how differentiation is used to find the vertical component of a cube's velocity:

import Differentiation

let cube = cubes[...]

@differentiable(reverse)
func getLocationY(t: Float) -> Float {
    let acceleration: SIMD3<Float> = [0, -9.8, 0]
    let a_contribution = acceleration.y / 2 * t * t

    return a_contribution + cube.velocity0.y * t + cube.location0.y
}

let dydt = gradient(at: cube.timeSinceCollision, of: getLocationY(t:))
let velocityText = String(format: "%.1f", dydt)
let messageText = "Velocity (Y): \(velocityText) m/s"

The complete implementation is located in Sample Code/Game/GameRendererExtensions.swift.

Rationale

The purpose of this demo is to make a case for the resurrection of Swift for TensorFlow, which relies on differentiation. Python ML libraries cannot run on iOS devices, yet real-time machine learning makes apps more intelligent. With stable automatic differentiation and a Metal GPU backend, new opportunities and flexible workflows could be unlocked for mobile app developers.

Video of App

differentiation-video.mp4

For those with a slow internet connection:

Text in augmented reality showing that Y velocity is -17.7 meters per second

If you have any insights or suggestions about this project, please comment on the Swift Forums post.

differentiation-ios-demo's People

Contributors

philipturner avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

beichenglangzi

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.