$30 off During Our Annual Pro Sale. View Details »

Everything a Swift Dev Ever Wanted to Know About Machine Learning But Was Too Afraid to Ask

Everything a Swift Dev Ever Wanted to Know About Machine Learning But Was Too Afraid to Ask

Presented as the opening session at try! Swift, on March 2nd, 2017, in Tokyo.

Alexis Gallagher

March 02, 2017
Tweet

More Decks by Alexis Gallagher

Other Decks in Technology

Transcript

  1. Everything a Swift Dev
    Ever Wanted to Know about
    Machine Learning
    But Was Too Afraid To Ask
    @alexisgallagher
    TopologyEyewear

    View Slide

  2. View Slide

  3. View Slide

  4. View Slide

  5. View Slide

  6. 1. What is
    MACHINE LEARNING
    really?

    View Slide

  7. func foo()
    { /* … */ }

    View Slide

  8. func foo()
    { /* … */ }

    View Slide

  9. let heights = [160,162.2,183,...]

    let expectedHeight = average(heights)

    func height() -> Float {
    return expectedHeight
    }

    View Slide

  10. let menHeights = [160,162.2,183,...]
    let womenHeights = [160,150.2,170,...]

    let expectedManHeight = average(menHeights)
    let expectedWomanHeight = average(womenHeights)

    func height(sex:Sex) -> Float {
    switch sex {
    case .female: return expectedWomanHeight
    case .male: return expectedManHeight
    }
    }

    View Slide

  11. let data = // data on our age, weight, shoe size, etc..
    // ꔅ
    let (c1, c2, c3, ... ) = ...

    func height(age:Float,weight:Float,shoeSize:Float,…) -> Float
    {
    return age * c1 + weight * c2 + shoeSize * c3 + …
    }

    View Slide

  12. Francis Galton
    (1822-1911)

    View Slide

  13. their respective means.
    Figure 1.
    Figure 1. Connecting the means of the individual columns of data provides a crude approximation of the
    regression line. The slope is exactly 0.50 and the correlation is approximately r = 0.51. Many, though not all,
    Sweetpea size
    heritability
    Galton (1894)1
    1 Stanton (2001). Galton, Pearson, and the Peas. Journal of
    Statistics Education.

    View Slide

  14. 2. So why all the
    EXCITEMENT
    right now ?

    View Slide

  15. Neural networks, circa 1958

    View Slide

  16. Neural networks, circa 2016

    View Slide

  17. View Slide

  18. Deep Learning
    • Lower layers identify more
    basic features2
    • Network discovers intrinsic
    hierarchy
    • Deeply cool!
    2 Goodfellow, 2016. Deep Learning. Figure 1.2, p 43.

    View Slide

  19. Networks getting bigger, faster, more accurate4
    4 Canziani 2017, An Analysis of Deep Neural Network Models for Practical Applications.

    View Slide

  20. Object
    recognition
    2014: funny punchline5
    2016: WWDC sample code
    5 xkcd, https://xkcd.com/1425/

    View Slide

  21. Human-like, or deeply weird?3
    bstract
    s (DNNs) have recently been
    performance on a variety of
    most notably visual classification
    Ns are now able to classify objects
    an-level performance, questions
    differences remain between com-
    A recent study [30] revealed that
    a lion) in a way imperceptible to
    to label the image as something
    eling a lion a library). Here we
    easy to produce images that are
    e to humans, but that state-of-the-
    ecognizable objects with 99.99%
    with certainty that white noise
    ally, we take convolutional neu-
    form well on either the ImageNet
    en find images with evolutionary
    cent that DNNs label with high
    o each dataset class. It is possi-
    lly unrecognizable to human eyes
    ar certainty are familiar objects,
    ages” (more generally, fooling ex- Figure 1. Evolved images that are unrecognizable to humans,
    3 Nguyen 2015. Deep Neural Networks are Easily Fooled: High Confidence Predictions for
    Unrecognizable Images. Computer Vision and Pattern Recognition, IEEE, 2015.

    View Slide

  22. 3. As a
    SWIFT DEVELOPER
    you might ask …

    View Slide

  23. Q: Does this affect me?
    Yes

    View Slide

  24. ML
    will creep
    into everything

    View Slide

  25. Q: Could I build an ML model?
    (even if I'm not a mathematician?)

    View Slide

  26. Q: Could I build an ML model?
    (even if I'm not a mathematician?)
    Yes

    View Slide

  27. Google's TensorFlow
    • Under active development,
    documented
    • Loads of official and
    unofficial tutorials (Siraj
    Raval on You, etc)
    • Making ML approachable
    • TensorFlow version 1 is like
    Swift version 1

    View Slide

  28. Q: Can I build it in Swift?
    A: Sort of.

    View Slide

  29. Build models with Python
    • Adapt a standard model: plain old regression,
    existing pre-trained neural network, or one of
    many off-the-shelf models
    • Use TensorFlow, in Python (probably)
    • Train with your data

    View Slide

  30. Deployment on iOS in various ways
    • Talk to TensorFlow server over the network
    • Embed TensorFlow library in the app
    • Port to Accelerate
    • Port to MetalPerformanceShaders

    View Slide

  31. Q: What's it like?

    View Slide

  32. Reminiscent of AVFoundation and CoreImage
    • Deferred execution model
    • Wire up a "session," inputs, outputs, filters
    • Framework optimizes data flow on hardware
    • Computation only as needed

    View Slide

  33. Curiously similar to UI design
    • Cannot prove anything will work
    • Starts with hunches and intuitions
    • Refined through experiment
    • 98% correct is major victory!

    View Slide

  34. ML

    View Slide

  35. Q: Where do I start?

    View Slide

  36. Resources
    • TensorFlow tutorials and summit videos
    (tensorflow for poets)
    • Introduction to Machine Learning (Stanford/
    Coursera, Andrew Ng)
    • This Week in Machine Learning & AI podcast
    • Saraj Raval's YouTube videos
    • Deep Learning, by Goodfellow et al.

    View Slide

  37. Conclusion
    • Yes, ML really is real
    • Likely coming soon to an API near you
    • Increasingly approachable, thanks to tooling
    and resources. (Version 1.0 all over again!)
    • Python on the server, Swift in the hand

    View Slide

  38. end
    curious? say hi!
    (we're hiring)
    twitter: @alexisgallagher
    [email protected]

    View Slide

  39. Concepts. Model definition vs training vs
    execution. Training vs test data. Accuracy and
    loss function. Overfit vs underfit. Models.
    Linear and logistic regression. Convolutional,
    recurrent, and adversarial neural networks.
    Support vector machines. Ensemble regression
    trees. Etc. Libraries. TensorFlow. Theano. Caffe.
    Torch. Keras. SciPy. NumPy. HelloWorlds. MNIST.
    ImageNet. Iris images. Networks. Inception.
    AlexNet. VGGNet. Resources. Intro to ML, Andrew
    Ng (Coursera). Siraj Ravel (Youtube). TensorFlow
    Summit videos. This Week in Machine Learning.

    View Slide

  40. the real end
    curious? say hi!
    (we're hiring)
    twitter: @alexisgallagher
    [email protected]

    View Slide