Hands-On Machine Learning with TensorFlow.js

Book description

Get hands-on with the browser-based JavaScript library for training and deploying machine learning models effectively

Key Features

  • Build, train and run machine learning models in the browser using TensorFlow.js
  • Create smart web applications from scratch with the help of useful examples
  • Use flexible and intuitive APIs from TensorFlow.js to understand how machine learning algorithms function

Book Description

TensorFlow.js is a framework that enables you to create performant machine learning (ML) applications that run smoothly in a web browser. With this book, you will learn how to use TensorFlow.js to implement various ML models through an example-based approach.

Starting with the basics, you'll understand how ML models can be built on the web. Moving on, you will get to grips with the TensorFlow.js ecosystem to develop applications more efficiently. The book will then guide you through implementing ML techniques and algorithms such as regression, clustering, fast Fourier transform (FFT), and dimensionality reduction. You will later cover the Bellman equation to solve Markov decision process (MDP) problems and understand how it is related to reinforcement learning. Finally, you will explore techniques for deploying ML-based web applications and training models with TensorFlow Core. Throughout this ML book, you'll discover useful tips and tricks that will build on your knowledge.

By the end of this book, you will be equipped with the skills you need to create your own web-based ML applications and fine-tune models to achieve high performance.

What you will learn

  • Use the t-SNE algorithm in TensorFlow.js to reduce dimensions in an input dataset
  • Deploy tfjs-converter to convert Keras models and load them into TensorFlow.js
  • Apply the Bellman equation to solve MDP problems
  • Use the k-means algorithm in TensorFlow.js to visualize prediction results
  • Create tf.js packages with Parcel, Webpack, and Rollup to deploy web apps
  • Implement tf.js backend frameworks to tune and accelerate app performance

Who this book is for

This book is for web developers who want to learn how to integrate machine learning techniques with web-based applications from scratch. This book will also appeal to data scientists, machine learning practitioners, and deep learning enthusiasts who are looking to perform accelerated, browser-based machine learning on Web using TensorFlow.js. Working knowledge of JavaScript programming language is all you need to get started.

Table of contents

  1. Title Page
  2. Copyright and Credits
    1. Hands-On Machine Learning with TensorFlow.js
  3. About Packt
    1. Why subscribe?
  4. Contributors
    1. About the author
    2. About the reviewers
    3. Packt is searching for authors like you
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Download the color images
      3. Conventions used
    4. Get in touch
      1. Reviews
  6. Section 1: The Rationale of Machine Learning and the Usage of TensorFlow.js
  7. Machine Learning for the Web
    1. Technical requirements
    2. Why machine learning on the web?
    3. Operation graphs
      1. Visualizing an operation graph
      2. Automatic differentiation
    4. What is TensorFlow.js?
      1. Seamless integration with web technologies
      2. Hardware acceleration using web browser APIs
      3. Compatibility with TensorFlow
      4. Data privacy
    5. Installing TensorFlow.js
      1. tfjs-converter
    6. The low-level API
      1. Tensors
      2. Operations
      3. Memory
      4. Eager execution
    7. The Layers API
      1. Sequential model
      2. Functional model
      3. Model summary
      4. Custom layers
    8. Summary
    9. Questions
    10. Further reading
  8. Importing Pretrained Models into TensorFlow.js
    1. Technical requirements
    2. The portable model format
      1. Protocol buffers
      2. GraphDef
      3. NodeDef
    3. Exporting a model from TensorFlow
      1. TensorFlow's SavedModel
        1. The SavedModel format
        2. Simple save
        3. The SavedModelBuilder API
      2. The Keras HDF5 model
    4. Converting models using tfjs-converter
      1. Converting a TensorFlow SavedModel
      2. The Keras HDF5 model
      3. The TensorFlow Hub module
    5. Loading the model into TensorFlow.js
      1. Supported operations
    6. Summary
    7. Questions
    8. Further reading
  9. TensorFlow.js Ecosystem
    1. Technical requirements
    2. Why high-level libraries?
    3. Using existing models
      1. MobileNet in tfjs-models
      2. Supported models
      3. Image classification application
      4. Example applications in the community
    4. Loading the data from various kinds of storage
      1. Data sources
      2. Webcam
    5. Pose detection with ML5.js
      1. Supported models
      2. PoseNet in ML5.js
    6. Drawing cats with Magenta.js
      1. Sketch drawing
    7. XOR classification with machinelearn.js
      1. Random forest classifier
    8. Summary
    9. Exercises
    10. Further reading
  10. Section 2: Real-World Applications of TensorFlow.js
  11. Polynomial Regression
    1. Technical requirements
    2. What is polynomial regression?
      1. Supervised learning
      2. The simplest linear model
      3. General polynomial model
      4. The loss function
      5. Optimizer for machine learning
      6. Optimizers in TensorFlow.js
    3. Two-dimensional curve fitting
      1. Preparing the dataset
      2. Applying the 2-degree polynomial model
      3. Loss function by mean squared error
      4. Looking into the optimization process
      5. Fitting the curve
    4. Summary
    5. Questions
    6. Further reading
  12. Classification with Logistic Regression
    1. Technical requirements
    2. Background of binary classification
    3. What is logistic regression?
      1. The behavior of the probabilistic generative model
      2. Optimization process
    4. Classifying two-dimensional clusters
      1. Preparing the dataset
      2. Logistic regression model with the Core API
      3. Optimizing with the cross-entropy loss function
      4. Implementing a logistic regression model with the Layers API
      5. Implementing a logistic regression model with machinelearn.js
    5. Summary
    6. Questions
    7. Further reading
  13. Unsupervised Learning
    1. Technical requirements
    2. What is unsupervised learning?
    3. Learning how K-means works
      1. Centroid
      2. Algorithm
      3. Evaluation
    4. Generalizing K-means with the EM algorithm
      1. The algorithm
      2. Relationship with K-means
    5. Clustering two groups in a 2D space
      1. The three clusters
      2. K-means implementation
    6. Summary
    7. Exercise
    8. Further reading
  14. Sequential Data Analysis
    1. Technical requirements
    2. What is Fourier transformation?
      1. Constituent frequencies as a feature
      2. Discrete Fourier transform
      3. Fast Fourier transform
    3. Cosine curve decomposition
      1. Complex number type
      2. The cosine curve
      3. Fourier transformation for cosine curves
      4. Fourier transformation for composite curves
      5. Inversed Fourier transform
    4. Summary
    5. Exercise
    6. Further reading
  15. Dimensionality Reduction
    1. Technical requirements
    2. Why dimensionality reduction?
      1. Curse of dimensionality
    3. Understanding principal component analysis
      1. Variance maximization
    4. Projecting 3D points into a 2D space with PCA
      1. Three-dimensional clusters
      2. Principal component calculation
      3. The variance of projected datasets
    5. Word embedding
      1. What is word embedding?
      2. Loading the IMDb dataset
      3. Embedding the model
      4. Visualization of embeddings
    6. Summary
    7. Exercise
    8. Further reading
  16. Solving the Markov Decision Process
    1. Technical requirements
    2. Reinforcement learning
      1. MDP
      2. Discounted total reward
      3. State-value function
      4. Bellman equation
      5. Q-learning
    3. Solving the four-states environment
      1. Designing the environment
      2. The Q-learning process
    4. Summary
    5. Exercise
    6. Further reading
  17. Section 3: Productionizing Machine Learning Applications with TensorFlow.js
  18. Deploying Machine Learning Applications
    1. Technical requirements
    2. The ecosystem around the JavaScript platform
      1. JavaScript in modern web browsers
      2. Node.js
      3. Node package manager
      4. Benefits of TypeScript in ML applications
    3. Module bundler
      1. Parcel
      2. Webpack
    4. Deploying modules with GitHub Pages
    5. Summary
    6. Questions
    7. Further reading
  19. Tuning Applications to Achieve High Performance
    1. Technical requirements
    2. The backend API of TensorFlow.js
      1. Operations that use the CPU backend
        1. Low overhead
        2. Memory management
      2. Implementing higher parallelism using the WebGL backend
        1. Avoid blocking the main thread
        2. Prefer tf.tidy to be free from memory management
        3. Floating number precision
        4. Save the overhead of shader compilation
      3. Using TensorFlow with the Node.js backend
    3. Tensor management
      1. Tensor construction
      2. Tensors as variables
      3. Revisiting tensor destruction
    4. Asynchronous data access
    5. Profiling
      1. Chrome profiler
    6. Model visualization
    7. Summary
    8. Questions
    9. Further reading
  20. Future Work Around TensorFlow.js
    1. Technical requirements
    2. Experimental backend implementations
      1. WebGPU – a new standard for accelerated graphics and computations
      2. WebAssembly – where the web meets the hardware instruction set
      3. React Native – moving forward to mobile-native applications
        1. Creating a React Native app
        2. Installing the dependencies required for TensorFlow.js
        3. Writing an application
        4. Running the application
      4. Electron – cross-platform desktop environment
    3. AutoML edge helper
    4. Summary
    5. Questions
    6. Further Reading
  21. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: Hands-On Machine Learning with TensorFlow.js
  • Author(s): Kai Sasaki
  • Release date: November 2019
  • Publisher(s): Packt Publishing
  • ISBN: 9781838821739