Discover millions of ebooks, audiobooks, and so much more with a free trial

Only $11.99/month after trial. Cancel anytime.

Hands-On Machine Learning with TensorFlow.js: A guide to building ML applications integrated with web technology using the TensorFlow.js library
Hands-On Machine Learning with TensorFlow.js: A guide to building ML applications integrated with web technology using the TensorFlow.js library
Hands-On Machine Learning with TensorFlow.js: A guide to building ML applications integrated with web technology using the TensorFlow.js library
Ebook483 pages3 hours

Hands-On Machine Learning with TensorFlow.js: A guide to building ML applications integrated with web technology using the TensorFlow.js library

Rating: 0 out of 5 stars

()

Read preview

About this ebook

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.

LanguageEnglish
Release dateNov 27, 2019
ISBN9781838827878
Hands-On Machine Learning with TensorFlow.js: A guide to building ML applications integrated with web technology using the TensorFlow.js library

Related to Hands-On Machine Learning with TensorFlow.js

Related ebooks

Internet & Web For You

View More

Related articles

Reviews for Hands-On Machine Learning with TensorFlow.js

Rating: 0 out of 5 stars
0 ratings

0 ratings0 reviews

What did you think?

Tap to rate

Review must be at least 10 words

    Book preview

    Hands-On Machine Learning with TensorFlow.js - Kai Sasaki

    Hands-On Machine Learning with TensorFlow.js

    Hands-On Machine Learning with TensorFlow.js

    A guide to building ML applications integrated with

    web technology using the TensorFlow.js library

    Kai Sasaki

    BIRMINGHAM - MUMBAI

    Hands-On Machine Learning with TensorFlow.js

    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 Shetty

    Acquisition Editor: Devika Battike

    Content Development Editor: Athikho Sapuni Rishana

    Senior Editor: Sofi Rogers

    Technical Editor: Joseph Sunil

    Copy Editor: Safis Editing

    Project Coordinator: Aishwarya Mohan

    Proofreader: Safis Editing

    Indexer: Priyanka Dhadke

    Production 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.

    Why subscribe?

    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. 

    Contributors

    About the author

    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.

    About the reviewers

    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.

    Packt is searching for authors like you

    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.

    Table of Contents

    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

    Bewertungen

    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

    Preface

    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.

    Who this book is for

    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.

    What this book covers

    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.

    To get the most out of this book

    Working knowledge of JavaScript will be handy.

    Download the example code files

    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!

    Download the color images

    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.

    Conventions used

    There are a number of text conventions used throughout this book.

    CodeInText: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: "GraphDef is a definition to describe the graph structure in the protocol buffer."

    A block of code is set as follows:

    message GraphDef {

      repeated NodeDef node = 1;

      VersionDef versions = 4;

      FunctionDefLibrary library = 2;

    }

    Any command-line input or output is written as follows:

    $ ls my_tfjs_model

    Bold: Indicates a new term, an important word, or words that you see on screen. For example, words in menus or dialog boxes appear in the text like this. Here is an example: "For example, ResNet-50, which is one of the major deep learning models for image classification, takes up 100 MB."

    Warnings or important notes appear like this.

    Tips and tricks appear like this.

    Get in touch

    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.

    Bewertungen

    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.

    Section 1: The Rationale of Machine Learning and the Usage of TensorFlow.js

    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

    Machine Learning for the Web

    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

    Technical requirements

    In this chapter, as a prerequisite, you need to prepare the following libraries or frameworks in your

    Enjoying the preview?
    Page 1 of 1