Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Hands-On Machine Learning with TensorFlow.js
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

Arrow left icon
Profile Icon Sasaki
Arrow right icon
€18.99 per month
Paperback Nov 2019 296 pages 1st Edition
eBook
€20.98 €29.99
Paperback
€36.99
Subscription
Free Trial
Renews at €18.99p/m
Arrow left icon
Profile Icon Sasaki
Arrow right icon
€18.99 per month
Paperback Nov 2019 296 pages 1st Edition
eBook
€20.98 €29.99
Paperback
€36.99
Subscription
Free Trial
Renews at €18.99p/m
eBook
€20.98 €29.99
Paperback
€36.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
Table of content icon View table of contents Preview book icon Preview Book

Hands-On Machine Learning with TensorFlow.js

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

Technical requirements

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.

The code we'll be using in this book can be found in this...

Why machine learning on the web?

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

Operation graphs

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

What is TensorFlow.js?

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.

You can view many of the interesting demo applications by going to the TensorFlow.js demo page: https://www.tensorflow.org/js/demos/.
This collection demonstrates the richness and potential of TensorFlow.js as a machine learning framework.

But why is TensorFlow.js so important to developers who are trying to create machine learning applications...

Installing TensorFlow.js

There are two ways we can set up the TensorFlow.js environment:

  • Use the minified JavaScript code that's distributed in the CDN
  • Use the bundled package that's distributed by package managers such as npm

Typically, TensorFlow.js should be used on a web platform. Since the prebuilt file is distributed by the global content distribution network (CDN) service, we need to add a script tag to the web application:

<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs/dist/tf.min.js"></script>

TensorFlow.js' classes can be found under the tf namespace. The CDN service works fast and is stable enough to provide such static resources to users. This is the easiest way to use TensorFlow.js.

If you want to serve your application in an environment where a public network isn't available, then you need to import TensorFlow...

The low-level API

The low-level API is flexible and allows us to construct the operation graph at the lowest level. It is also known as TensorFlow.js Core (https://github.com/tensorflow/tfjs-core).

This API allows us to access kernel implementations for each backend directly. Fundamentally, other high-level libraries and ecosystems depend on the Core API. Being familiar with the Core API will help us implement an efficient machine learning model with TensorFlow.js. Although the code base of the Core API was initially separated, TensorFlow.js is now managed by the mono repository. This means we can access any type of API solely from the root of the namespace of the library. Therefore, if we were to use tf as the reference to the root namespace, we can import it as follows:

import * as tf from '@tensorflow/tfjs'; 
...

The Layers API

In the previous section, we described how to use the Core API of TensorFlow.js, which allows us to construct any operation graph as we like. But this is not always the best choice. You may find yourself in a situation where a high-level API is more relevant when we want to build an application quickly. The Layers API is a Keras-like high-level API that's used to create models in an intrinsic way. You may already be familiar with the style of the Layers API if you've used Keras to construct machine learning models in the past.

There are two ways we can construct a machine learning model with the Layers API:

  • By using the sequential model API
  • By using the functional model API

As you may have already noticed, the Layers API has been made to look similar to the Keras API. Those of you who are already familiar with Keras will be able to use the Layers API...

Summary

In this chapter, we have learned about the benefits of constructing a machine learning model on the web and how to use TensorFlow.js to build it. There are two ways we can build a model with TensorFlow.js. The first way is to use the Core API, which helps us build flexible models and optimize their performance as much as possible. The other way is to use the Layers API. This API is similar to Keras, which means we can construct deep learning models more intuitively. We don't need to construct our own model if it is already publicly available.

We also learned that it's possible to import an existing model into TensorFlow.js by using tfjs-converter. By completing this chapter, you know how to construct your own models with TensorFlow.js and import existing models into TensorFlow.js.

In the next chapter, we will learn how to import pretrained models into TensorFlow...

Questions

  1. What is the benefit of building a machine learning model on the web?
  2. When we give the TensorHub model to tfjs-converter, what type of format will be generated?
    1. Layers model
    2. Graph model
  3. How many ways can we release the memory that's been allocated to a tensor in a model in TensorFlow.js?
  4. How can we inspect the structure of the model?
  5. Describe the major difference between the Core API and the Layers API. When should we use them?
  6. Construct a multilayer perceptron with the following layers:
    • The input is a vector with 784 elements.
    • The first intermediate layer is a fully connected layer whose output is a rectified linear unit and has a size of 32.
    • The second intermediate layer is a fully connected layer whose output is a rectified linear unit and has a size of 16.
    • The output is a softmax layer.
  7. Is it possible to save a model that contains a custom layer?
...

Further reading

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • 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

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.

Who is this book 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.

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

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Nov 27, 2019
Length: 296 pages
Edition : 1st
Language : English
ISBN-13 : 9781838821739
Vendor :
Google
Category :
Languages :
Tools :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

Publication date : Nov 27, 2019
Length: 296 pages
Edition : 1st
Language : English
ISBN-13 : 9781838821739
Vendor :
Google
Category :
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 115.97
Advanced Deep Learning with Python
€36.99
Python Machine Learning
€41.99
Hands-On Machine Learning with TensorFlow.js
€36.99
Total 115.97 Stars icon

Table of Contents

16 Chapters
Section 1: The Rationale of Machine Learning and the Usage of TensorFlow.js Chevron down icon Chevron up icon
Machine Learning for the Web Chevron down icon Chevron up icon
Importing Pretrained Models into TensorFlow.js Chevron down icon Chevron up icon
TensorFlow.js Ecosystem Chevron down icon Chevron up icon
Section 2: Real-World Applications of TensorFlow.js Chevron down icon Chevron up icon
Polynomial Regression Chevron down icon Chevron up icon
Classification with Logistic Regression Chevron down icon Chevron up icon
Unsupervised Learning Chevron down icon Chevron up icon
Sequential Data Analysis Chevron down icon Chevron up icon
Dimensionality Reduction Chevron down icon Chevron up icon
Solving the Markov Decision Process Chevron down icon Chevron up icon
Section 3: Productionizing Machine Learning Applications with TensorFlow.js Chevron down icon Chevron up icon
Deploying Machine Learning Applications Chevron down icon Chevron up icon
Tuning Applications to Achieve High Performance Chevron down icon Chevron up icon
Future Work Around TensorFlow.js Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.