Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Swift for TensorFlow - The Next Generation plat...

Swift for TensorFlow - The Next Generation platform for Deep Learning.

Aprenderás a entrenar un modelo de Deep Learning utilizando Swift para TensorFlow, la nueva plataforma que Google está construyendo para Deep Learning; sus ventajas y cómo es que te permite crear algoritmos de una forma más sencilla y rápida.

Isabel Palomar

November 16, 2019
Tweet

More Decks by Isabel Palomar

Other Decks in Technology

Transcript

  1. Table of contents • Getting started with AI ◦ Challenges

    • Why Swift? • Training a model with Swift • … Next Steps 3
  2. “ 6 6 @jeremyphoward @math_rachel I started a Deep Learning

    Course last year at the University of San Francisco. MY STORY: TAKING MY FIRST DEEP LEARNING CLASS
  3. 7 After the class….. The key outcome of this lesson

    is that we'll have trained an image classifier which can recognize pet breeds at state of the art accuracy. The key to this success is the use of transfer learning, which will be a key platform for much of this course. We also discuss how to set the most important hyper-parameter when training neural networks: the learning rate, using Leslie Smith's fantastic learning rate finder method. Finally, we'll look at the important but rarely discussed topic of labeling, and learn about some of the features that fastai provides for allowing you to easily add labels to your images. https://course.fast.ai/videos/?lesson=1
  4. challenges…. ‐ Many courses, even basic, assume that you already

    know the subject. ‐ Reaching the final result without learning the basics is not good. 8
  5. 13 Tools and infrastructure • Easy to learn and build

    • Fast • Quick implementations • Accuracy ARTIFICIAL INTELLIGENCE GOALS
  6. PROGRAMMING LANGUAGES AND THE AI WORLD... Slow at runtime, poor

    support for parallel processing, but very easy to use. 14 Hard to use (and C++ is slow at compile time), but fast and (for C++) expressive. Unsafe (unless you use Typescript); somewhat slow (but easy to use and flexible) Poor support for general purpose programming, but fast and expressive. Verbose (but getting better, particularly if you use Kotlin), less flexible (due to JVM issues). Requires installation of a runtime, limited flexibility due to garbage collection, slow https://www.fast.ai/2019/01/10/swift-numerics/
  7. 15 OHH… HELLO SWIFT! • Expressive, flexible, concise, safe, easy

    to learn and use, fast. Swift is developed to be open ◦ Community participation
  8. 17 Swift for tensorflow First-class autodiff Differentiable programming gets first-class

    support in a general-purpose programming language. Next-generation APIs New APIs informed by the best practices of today, and the research directions of tomorrow, are both easier to use and more powerful. Builds on TensorFlow Building on TensorFlow, the Swift APIs give you transparent access to all low-level TensorFlow operators.
  9. 18 High-quality tooling - COLAB Colaboratory is a free Jupyter

    notebook environment that requires no setup and runs entirely in the cloud. https://colab.research.google.com/
  10. How? Where do we get data from? Data curation is

    the organization and integration of data collected from various sources. 25 Techniques You can use techniques like Questionnaires and surveys, conducting interviews, using data scraping and data crawling techniques.
  11. Public datasets • Google AI • UCI ML Repository •

    Data.gov.in • Kaggle Where do we get data from? Crowdsourcing Marketplaces • Amazon Mechanical Turk • Dataturks • Figure-eight 26
  12. BACK TO OUR EXAMPLE... The MNIST database is a large

    database of handwritten digits that is commonly used for training and testing in the field of machine learning. https://en.wikipedia.org/wiki/MNIST_database 27
  13. LOAD THE DATASET 29 %include "/content/swift-datascience/MNIST.swift" // Load dataset let

    dataset = MNIST(batchSize: 128) // Get first 5 images let imgs = dataset.trainingImages.minibatch(at: 0, batchSize:5).makeNumpyArray()
  14. EXPLORING THE MNIST DIGITS 30 // Display first 5 images

    for img in imgs{ plt.imshow(img.reshape(28,28)) plt.show() }
  15. models 36 There are many models that are created over

    the years. Each model has its own advantages and disadvantages based on the type of data on which we are creating a model.
  16. IMAGE CLASSIFICATION MODEL An image classification model is trained to

    recognize various classes of images. 37 When we subsequently provide a new image as input to the model, it will output the probabilities of the image representing each of the types it was trained on.
  17. An example output might be as follows: Digit type Probability

    0 0.00 1 0.02 ... 0.00 7 0.97 8 0.00 9 0.01 38 Based on the output, we can see that the classification model has predicted that the image has a high probability of representing a number 7
  18. In this example, we will create the LeNet-5 model using

    the Swift for TensorFlow Deep Learning Library Model for our example 40
  19. Create a model using the Swift for TensorFlow API 41

    import TensorFlow let epochCount = 100 let batchSize = 128 // The LeNet-5 model var classifier = Sequential { Conv2D<Float>(filterShape: (5, 5, 1, 6), padding: .same, activation: relu) AvgPool2D<Float>(poolSize: (2, 2), strides: (2, 2)) Conv2D<Float>(filterShape: (5, 5, 6, 16), activation: relu) AvgPool2D<Float>(poolSize: (2, 2), strides: (2, 2)) Flatten<Float>() Dense<Float>(inputSize: 400, outputSize: 120, activation: relu) Dense<Float>(inputSize: 120, outputSize: 84, activation: relu) Dense<Float>(inputSize: 84, outputSize: 10, activation: softmax) }
  20. Is something you do everyday... You are optimizing variables and

    basing your personal decisions all day long, most of the time without even recognizing the process consciously https://mitsloan.mit.edu/ideas-made-to-matter/how-to-use -algorithms-to-solve-everyday-problems 43
  21. First Order Optimization Algorithms  • Gradient Descent Types of learning

    algorithms Second Order Optimization Algorithms • Hessian https://towardsdatascience.com/types-of-optimization-algorithms-used-in-neural-networks-and- ways-to-optimize-gradient-95ae5d39529f 44
  22. Choosing Gradient Descent as the Optimizer Swift for TensorFlow has

    many optimization algorithms available for training. This model uses the SGD optimizer that implements the stochastic gradient descent (SGD) algorithm. 46 // Using Gradient Descent as the optimizer let optimizer = SGD(for: classifier, learningRate: 0.1)
  23. OTHER OPTIMIZERS AVAILABLE ON THE SWIFT FOR TENSORFLOW API Swift4Tensorflow

    supports many additional optimizers. You can choose your pick based on your project: 47 ‐ AMSGrad ‐ AdaDelta ‐ AdaGrad ‐ AdaMax ‐ Adam ‐ Parameter ‐ RMSProp ‐ SGD
  24. 5.- loss function How do we know which model is

    better? Loss function (also known as the error) answers this question. 48
  25. Classification losses: • Mean Square Error/L2 Loss • Mean Absolute

    Error/L1 Loss Regression losses: • Hinge Loss/Multi-class SVM Loss • Cross Entropy • Loss/Negative Log Likelihood LOSS FUNCTIONS To know which model is good for our data, we compute the loss function by comparing the predicted outputs to actual output. 49
  26. Choose a loss function Our model will calculate its loss

    using the softmaxCrossEntropy(logits:labels:) function which takes the model's class probability predictions and the desired label, and returns the average loss across the examples. 51 let loss = softmaxCrossEntropy(logits: ŷ, labels: y)
  27. Evaluation/performance metrics 54 • Confusion matrix • Area Under the

    ROC curve (AUC — ROC) • Root Mean Squared Error (RMSE) (used in regression)
  28. Model training 56 print("Beginning training...") struct Statistics { var correctGuessCount:

    Int = 0 var totalGuessCount: Int = 0 var totalLoss: Float = 0 } // The training loop - DATA for epoch in 1...epochCount { var trainStats = Statistics() } let trainAccuracy = Float(trainStats.correctGuessCount) / Float(trainStats.totalGuessCount) let testAccuracy = Float(testStats.correctGuessCount) / Float(testStats.totalGuessCount) print(""" [Epoch \(epoch)] \ Training Loss: \(trainStats.totalLoss), \ Training Accuracy: \(trainStats.correctGuessCount)/\(trainStats.totalGuessCount) \ (\(trainAccuracy)), \ Test Loss: \(testStats.totalLoss), \ Test Accuracy: \(testStats.correctGuessCount)/\(testStats.totalGuessCount) \ (\(testAccuracy)) """) }
  29. LET’S EXPLORE COLABORATORY Colaboratory is a free Jupyter notebook environment

    that requires no setup and runs entirely in the cloud. https://colab.research.google.com
  30. We did it!!! Now you are ready to start building

    your first custom Deep Learning model. 61
  31. Using the model - Tensorflow platform 62 TensorFlow is an

    end-to-end open source platform for machine learning. • Web Apps • Back end - APIs https://www.tensorflow.org/
  32. Tensorflow lite options - Mobile & iot TensorFlow Lite provides

    all the tools you need to convert and run TensorFlow models on mobile, embedded, and IoT devices. 63
  33. There are so many resources where you can start learning….

    Fast AI • Practical Deep Learning for Coders: ◦ https://course.fast.ai/ ◦ https://course.fast.ai/part2. Swift and tensorflow - Colaboration with Chris Lattner • Embracing Swift for Deep Learning ◦ https://www.fast.ai/2019/03/06/fastai-swift/ 66 IF YOU DON’T FEEL READY….
  34. Udacity • Intro to Artificial Intelligence: ◦ https://www.udacity.com/course/intro-to-artificial-intelli gence--cs271 •

    Intro to Machine Learning: ◦ https://www.udacity.com/course/intro-to-machine-learn ing--ud120 • TensorFlow for Deep Learning ◦ https://www.udacity.com/course/intro-to-tensorflow-for -deep-learning--ud187 ◦ 67 Learning about ai, ml and deep learning
  35. • Official documentation and resources ◦ https://www.tensorflow.org/swift • Model training

    walkthrough ◦ https://www.tensorflow.org/swift/tutorials/model_traini ng_walkthrough • Guide to learn swift from scratch for Data Science ◦ https://www.analyticsvidhya.com/blog/2019/10/compre hensive-guide-learn-swift-from-scratch-data-science/ • Swift for Machine learning (podcast episode) ◦ https://www.swiftbysundell.com/podcast/58/ ◦ 68 Swift for tensorflow