40,81 €
Get hands-on with the browser-based JavaScript library for training and deploying machine learning models effectively
Key Features
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
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.
Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:
Seitenzahl: 293
Veröffentlichungsjahr: 2019
Copyright © 2019 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
Commissioning Editor: Sunith ShettyAcquisition Editor: Devika BattikeContent Development Editor: Athikho Sapuni RishanaSenior Editor: Sofi RogersTechnical Editor: Joseph SunilCopy Editor: Safis EditingProject Coordinator: Aishwarya MohanProofreader: Safis EditingIndexer: Priyanka DhadkeProduction Designer: Aparna Bhagat
First published: November 2019
Production reference: 1261119
Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK.
ISBN 978-1-83882-173-9
www.packt.com
Packt.com
Subscribe to our online digital library for full access to over 7,000 books and videos, as well as industry-leading tools to help you plan your personal development and advance your career. For more information, please visit our website.
Spend less time learning and more time coding with practical eBooks and videos from over 4,000 industry professionals
Improve your learning with skill plans built especially for you
Get a free eBook or video every month
Fully searchable for easy access to vital information
Copy and paste, print, and bookmark content
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.packt.com and, as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details.
At www.packt.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks.
Kai Sasaki works as a software engineer at Treasure Data. He engages in developing large-scale distributed systems to make data valuable. His passion for creating artificial intelligence by processing large-scale data led him to the field of machine learning. He is one of the initial contributors to TensorFlow.js and keeps working to add new operators that are required for new types of machine learning models. Because of his work, he received the Google Open Source Peer Bonus in 2018.
Edoh Kodjo graduated twice as an engineer in networking and in computer science, and is a software developer/data scientist. He started developing an interest in machine learning and artificial intelligence soon after graduating. Since then, his interest in the field has kept on growing. He currently works as a software engineer on a project for computer vision. As a frontend and JavaScript fanatic, he is a machine learning for the web advocate. Here is his motto—Life is an endless road, coding is an endless loop for a while!, which sums up how much he loves coding.
Nick Bourdakos is a developer advocate at IBM in NYC and the creator of Cloud Annotations, a collaborative open source image annotation tool for training computer vision models. His expertise is in machine learning, mainly deep learning applied to computer vision problems. He is passionate when it comes to teaching people about machine learning and has a course on YouTube regarding TensorFlow.js.
If you're interested in becoming an author for Packt, please visit authors.packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea.
Title Page
Copyright and Credits
Hands-On Machine Learning with TensorFlow.js
About Packt
Why subscribe?
Contributors
About the author
About the reviewers
Packt is searching for authors like you
Preface
Who this book is for
What this book covers
To get the most out of this book
Download the example code files
Download the color images
Conventions used
Get in touch
Reviews
Section 1: The Rationale of Machine Learning and the Usage of TensorFlow.js
Machine Learning for the Web
Technical requirements
Why machine learning on the web?
Operation graphs
Visualizing an operation graph
Automatic differentiation
What is TensorFlow.js?
Seamless integration with web technologies
Hardware acceleration using web browser APIs
Compatibility with TensorFlow
Data privacy
Installing TensorFlow.js
tfjs-converter
The low-level API
Tensors
Operations
Memory
Eager execution
The Layers API
Sequential model
Functional model
Model summary
Custom layers
Summary
Questions
Further reading
Importing Pretrained Models into TensorFlow.js
Technical requirements
The portable model format
Protocol buffers
GraphDef
NodeDef
Exporting a model from TensorFlow
TensorFlow's SavedModel
The SavedModel format
Simple save
The SavedModelBuilder API
The Keras HDF5 model
Converting models using tfjs-converter
Converting a TensorFlow SavedModel
The Keras HDF5 model
The TensorFlow Hub module
Loading the model into TensorFlow.js
Supported operations
Summary
Questions
Further reading
TensorFlow.js Ecosystem
Technical requirements
Why high-level libraries?
Using existing models
MobileNet in tfjs-models
Supported models
Image classification application
Example applications in the community
Loading the data from various kinds of storage
Data sources
Webcam
Pose detection with ML5.js
Supported models
PoseNet in ML5.js
Drawing cats with Magenta.js
Sketch drawing
XOR classification with machinelearn.js
Random forest classifier
Summary
Exercises
Further reading
Section 2: Real-World Applications of TensorFlow.js
Polynomial Regression
Technical requirements
What is polynomial regression?
Supervised learning
The simplest linear model
General polynomial model
The loss function
Optimizer for machine learning
Optimizers in TensorFlow.js
Two-dimensional curve fitting
Preparing the dataset
Applying the 2-degree polynomial model
Loss function by mean squared error
Looking into the optimization process
Fitting the curve
Summary
Questions
Further reading
Classification with Logistic Regression
Technical requirements
Background of binary classification
What is logistic regression?
The behavior of the probabilistic generative model
Optimization process
Classifying two-dimensional clusters
Preparing the dataset
Logistic regression model with the Core API
Optimizing with the cross-entropy loss function
Implementing a logistic regression model with the Layers API
Implementing a logistic regression model with machinelearn.js
Summary
Questions
Further reading
Unsupervised Learning
Technical requirements
What is unsupervised learning?
Learning how K-means works
Centroid
Algorithm
Evaluation
Generalizing K-means with the EM algorithm
The algorithm
Relationship with K-means
Clustering two groups in a 2D space
The three clusters
K-means implementation
Summary
Exercise
Further reading
Sequential Data Analysis
Technical requirements
What is Fourier transformation?
Constituent frequencies as a feature
Discrete Fourier transform
Fast Fourier transform
Cosine curve decomposition
Complex number type
The cosine curve
Fourier transformation for cosine curves
Fourier transformation for composite curves
Inversed Fourier transform
Summary
Exercise
Further reading
Dimensionality Reduction
Technical requirements
Why dimensionality reduction?
Curse of dimensionality
Understanding principal component analysis
Variance maximization
Projecting 3D points into a 2D space with PCA
Three-dimensional clusters
Principal component calculation
The variance of projected datasets
Word embedding
What is word embedding?
Loading the IMDb dataset
Embedding the model
Visualization of embeddings
Summary
Exercise
Further reading
Solving the Markov Decision Process
Technical requirements
Reinforcement learning
MDP
Discounted total reward
State-value function
Bellman equation
Q-learning
Solving the four-states environment
Designing the environment
The Q-learning process
Summary
Exercise
Further reading
Section 3: Productionizing Machine Learning Applications with TensorFlow.js
Deploying Machine Learning Applications
Technical requirements
The ecosystem around the JavaScript platform
JavaScript in modern web browsers
Node.js
Node package manager
Benefits of TypeScript in ML applications
Module bundler
Parcel
Webpack
Deploying modules with GitHub Pages
Summary
Questions
Further reading
Tuning Applications to Achieve High Performance
Technical requirements
The backend API of TensorFlow.js
Operations that use the CPU backend
Low overhead
Memory management
Implementing higher parallelism using the WebGL backend
Avoid blocking the main thread
Prefer tf.tidy to be free from memory management
Floating number precision
Save the overhead of shader compilation
Using TensorFlow with the Node.js backend
Tensor management
Tensor construction
Tensors as variables
Revisiting tensor destruction
Asynchronous data access
Profiling
Chrome profiler
Model visualization
Summary
Questions
Further reading
Future Work Around TensorFlow.js
Technical requirements
Experimental backend implementations
WebGPU – a new standard for accelerated graphics and computations
WebAssembly – where the web meets the hardware instruction set
React Native – moving forward to mobile-native applications
Creating a React Native app
Installing the dependencies required for TensorFlow.js
Writing an application
Running the application
Electron – cross-platform desktop environment
AutoML edge helper
Summary
Questions
Further Reading
Other Books You May Enjoy
Leave a review - let other readers know what you think
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.
In this book, you'll understand how ML models can be built on the web. Moving on, you will get to grips with using 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 use 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.
This book is for web developers who want to learn how to integrate ML techniques with web-based applications from scratch. This book will also appeal to data scientists, ML practitioners, and deep learning enthusiasts who are looking to perform accelerated, browser-based ML on the web using TensorFlow.js. Working knowledge of the JavaScript programming language is all you need to get started.
Chapter 1, Machine Learning for the Web, will show you the importance of ML on the web platform. Fundamentally, ML applications should provide some value to the users through a user-facing interface such as a web platform. In this chapter, we will leverage ML on the web platform to remove the fences between the user-facing environment and the environment where traditional server-side ML runs. You will learn how to install TensorFlow.js and set up the environment around it.
Chapter 2, Importing Pretrained Models into TensorFlow.js, explains how to import Keras pretrained models into TensorFlow.js. Since TensorFlow Core can train such a model efficiently, we can easily reuse the model in a client-side application.
Chapter 3, TensorFlow.js Ecosystem, shows you how to use some frameworks and libraries running with TensorFlow.js that are used to construct ML models, so that you can develop your own application more efficiently.
Chapter 4, Polynomial Regression, shows you how TensorFlow.js APIs are used with the simplest models. The application we look at predicts the y value of a sine curve with a given x value by using a polynomial regression model, implemented with a neural network.
Chapter 5, Classification with Logistic Regression, teaches you how to implement a classification model such as a logistic regression model. With the help of a practical example, we will teach you how to write a logistic regression application to classify flower types with the Iris dataset.
Chapter 6, Unsupervised Learning, demonstrates the potential of TensorFlow as an ML framework by implementing a clustering algorithm such as k-means and demonstrating unsupervised learning. We will be implementing the k-means algorithm using the Iris dataset.
Chapter 7, Sequential Data Analysis, explains how the FFT algorithm is implemented in TensorFlow and how to use it in an ML application. You will also learn how complex numerical types are implemented in TensorFlow.js.
Chapter 8, Dimensionality Reduction, introduces t-SNE and how it can be implemented in TensorFlow.js.
Chapter 9, Solving Markov Decision Problems, introduces the implementation of the Bellman equation for solving MDP problems and explains how it is related to reinforcement learning.
Chapter 10, Deploying Machine Learning Applications, shows you the general ways to create a package from a TensorFlow.js application.
Chapter 11, Tuning Applications to Achieve High Performance, shows you how to make use of certain backend implementations to pursue high performance as well as giving you tips for tuning an application written in TensorFlow.js.
Chapter 12, Future Works around TensorFlow.js, covers more advanced features and optimizations implemented in TensorFlow.js so that you can learn about what is going on in TensorFlow.js projects.
Working knowledge of JavaScript will be handy.
You can download the example code files for this book from your account at www.packt.com. If you purchased this book elsewhere, you can visit www.packtpub.com/support and register to have the files emailed directly to you.
You can download the code files by following these steps:
Log in or register at
www.packt.com
.
Select the
Support
tab.
Click on
Code Downloads
.
Enter the name of the book in the
Search
box and follow the onscreen instructions.
Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:
WinRAR/7-Zip for Windows
Zipeg/iZip/UnRarX for Mac
7-Zip/PeaZip for Linux
The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Hands-On-Machine-Learning-with-TensorFlow.js. In case there's an update to the code, it will be updated on the existing GitHub repository.
We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!
We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here: http://www.packtpub.com/sites/default/files/downloads/9781838821739_ColorImages.pdf.
Feedback from our readers is always welcome.
General feedback: If you have questions about any aspect of this book, mention the book title in the subject of your message and email us at [email protected].
Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/support/errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.
Piracy: If you come across any illegal copies of our works in any form on the internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.
If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.
Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!
For more information about Packt, please visit packt.com.
In this section, readers will explore how machine learning applications work on the web platform. They will also learn how to set up an environment to run TensorFlow.js. Furthermore, readers will learn how to import pretrained models from Keras into TensorFlow.js. This section will also cover the ecosystem around TensorFlow.js.
This section contains the following chapters:
Chapter 1
,
Machine Learning for the Web
Chapter 2
,
Importing Pretrained Models into TensorFlow.js
Chapter 3
,
TensorFlow.js Ecosystem
In this book, we will learn how to use TensorFlow.js to create machine learning applications. You'll need to be familiar with the following in order to get started:
Web-based programming languages, such as JavaScript and TypeScript
Web platform technology stacks (only a basic knowledge is required)
The fundamentals of machine learning algorithms
In this chapter, we are going to clarify why machine learning on the web is crucial in modern machine learning use cases and when to use web technology so that you can run your applications. You will also be introduced to the basic APIs of TensorFlow.js so that you can construct machine learning models. These topics act as the basis for the chapters that follow.
In this chapter, we will cover the following topics:
Why machine learning on the web?
Operation graphs
What is TensorFlow.js?
Installing TensorFlow.js
The low-level API
The Layers API
In this chapter, as a prerequisite, you need to prepare the following libraries or frameworks in your environment:
A web browser
(
Chrome is recommended
):
TensorFlow.js primarily runs on web browsers.
The Node.js environment, which contains a node package manager
(
npm
):
Node.js is necessary since it resolves dependencies so that we can run TensorFlow.js.
TypeScript compiler
:
TensorFlow.js and its application are often written in TypeScript.
Python
(
3.x is recommended
):
We need this so that we can run Python-dependent tools such as
tfjs-converter
and the TensorFlow Python API.
If you are unsure about how to build the environment, please look at the Further reading section, which can be found at the end of this chapter. You will find these resources useful while you set up these prerequisites.
Machine learning technology was invented in the 1950s. Back then, there was no such period where machine learning was the exciting field in computer science that it currently is. However, thanks to breakthroughs in areas of deep learning and artificial intelligence, a huge amount of resources in terms of money and manpower have been devoted to help research it. For example, it isn't unusual to use an extensive amount of computing power that's leveraged by GPUs in laboratories in universities. Nowadays, industries and academics are cooperating to make progress in the computer science field. We are living in an era that's creating and facing large-scale data like never before. The importance of machine learning mainly comes from the demand for providing value by making use of this large-scale data. Machine learning technology gives us a chance to find innovative insights in a scalable and reproducible manner more than ever. For the last decade, intensive research has been done in the machine learning field. Deep learning is one such technology that has achieved accuracy that competes with human intelligence in relation to problems such as image recognition, audio generation, and machine translation. Many machine learning frameworks are emerging and being developed by both academics and industries in order to follow this trend. These technologies can contribute to making such use cases more abundant so that more research can take place.
However, creating a user-friendly application using machine learning is still a challenge. Most machine learning frameworks are designed and optimized to run in an environment that uses distributed systems that are running on thousands of machines and accelerators, such as GPUs. Generally, a machine learning model is used to predict something that's unknown to us after we've trained a model with a known dataset. Environments that contain GPUs and accelerators are exclusively used for the training phase. Although this allows us to train the machine learning model efficiently, it builds a wall between training the model and the inference of the model because it is necessary to make the trained model work on real data. We may need to fine-tune the model with a custom user dataset or convert the model into an executable format in the user's environment. This means that we need to deal with new challenges in terms of integrating between the machine learning model and the user-facing environment when we try to create applications that leverage machine learning technology. Porting machine learning models to platforms that users use often requires intensive work and skill because they're not compatible with the environment. It is common for data scientists who usually use Python as their primary language to struggle with building web applications with JavaScript.
In that sense, the web is the environment that's used the most by the end users of any kind of application. Machine learning applications are not an exception. More and more users are expected to use machine learning applications while they're on the web. Therefore, the web can be seen as the next frontier for machine learning applications because of its potential in terms of how many users use it. The technology that makes machine learning runnable on the web expands the possible use cases for machine learning in the real world. In this book, we are going to learn how to run machine learning on the web by using a modern framework known as TensorFlow.js. TensorFlow.js is a framework that is compatible with TensorFlow APIs so that users can create machine learning applications on the web. Apart from providing the flexibility of web-based machine learning applications, it also provides satisfactory performance since it uses an acceleration mechanism that's provided by modern web browsers.
This book is a practical guide to applying machine learning technologies to the web so that our users can quickly benefit and get value from our applications. It's assumed that you are a developer who wants to create a machine learning application with a rich user interface swiftly and efficiently.
Before diving into TensorFlow.js itself, we need to be familiar with the idea of operation graphs, or calculation graphs, which are common constructs that we'll use to build machine learning models alongside modern frameworks such as TensorFlow. In these frameworks, the data is represented as a tensor. A tensor is a data structure that represents an arbitrary dimensional array. Those of you who have used the NumPy library in Python may already be familiar with this concept. In NumPy, ndarray is commonly used to display various kinds of data in machine learning, such as images and audio, regardless of whether it's structured or unstructured.
Modern machine learning frameworks, including TensorFlow, illustrates the fact that machine learning models are operation graphs of tensors. An operation graph is defined as a chain that's used for the manipulation or transformation of tensors.
Operation graphs are at the core of modern machine learning frameworks, including TensorFlow. They are powerful and flexible constructs that allow us to build any kind of mathematical structure technically. Let's take a look at this important structure in more detail.
The following diagram is an example of an operation graph that represents a simple classification neural network. As you can see, the operation graph illustrates the flow of the data. The squared nodes represent the tensors for the input, output, and intermediate nodes. Each tensor transformation is represented by oval nodes. These are responsible for changing the shape of the tensor, the values inside it, and their control flow. This indicates that you can construct any kind of data flow while you're writing code with certain programming languages:
But does this mean that operation graphs are the best choice when we're constructing a machine learning model? Let's take a look at another factor that demonstrates the advantages of using an operation graph to represent a machine learning model.
TensorFlow.js is a framework that we can use to construct machine learning models that are compatible with TensorFlow Python APIs. Unlike TensorFlow Python APIs, TensorFlow.js can be seamlessly integrated with the web so that we can quickly run machine learning algorithms on any platform. It was originally invented by Google and published as a piece of open source software known initially as deeplearn.js. Thanks to the contributions of developers, it is one of the most actively developed projects in the TensorFlow family.
But why is TensorFlow.js so important to developers who are trying to create machine learning applications? There are several characteristics that illustrate the significance of this framework. We'll go over some of these now.
Since TensorFlow.js is written in TypeScript, it can be naturally integrated with existing web technologies. This means that a machine learning model that's been written with TensorFlow.js can be run in a web browser without any modifications having to be made. This ensures that we can make use of the rich user interface that the web browser provides. Actually, TensorFlow.js itself has its own set of utilities so that we can import the Document Object Model (DOM) element of an image as a tensor, such as a canvas. This makes it easy for us to run the application with the data that's available on the internet. The teachable machine is a good example of the power of integrating with the web. It uses input from webcams by using the browser API.
The following is a screenshot of the teachable machine (https://teachablemachine.withgoogle.com/):
Another benefit of integrating with the web is application distribution. Distributing the application is a common challenge that many developers face. We need to find a way of distributing the package and making sure that it works as expected in the user environment. The web platform was originally developed to distribute such resources globally. Due to the nature of the platform, package distribution can be done scalably and efficiently, and web browsers hide the complexity of accessing local resources securely.
