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

eBook
AU$36.99 AU$53.99
Paperback
AU$67.99
Subscription
Free Trial
Renews at AU$24.99p/m

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Product feature icon AI Assistant (beta) to help accelerate your learning
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
Estimated delivery fee Deliver to Australia

Economy delivery 7 - 10 business days

AU$19.95

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 Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Product feature icon AI Assistant (beta) to help accelerate your learning
Estimated delivery fee Deliver to Australia

Economy delivery 7 - 10 business days

AU$19.95

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
AU$24.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
AU$249.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 AU$5 each
Feature tick icon Exclusive print discounts
AU$349.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 AU$5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total AU$ 211.97
Advanced Deep Learning with Python
AU$67.99
Python Machine Learning
AU$75.99
Hands-On Machine Learning with TensorFlow.js
AU$67.99
Total AU$ 211.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 the delivery time and cost of print book? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela
What is custom duty/charge? Chevron down icon Chevron up icon

Customs duty are charges levied on goods when they cross international borders. It is a tax that is imposed on imported goods. These duties are charged by special authorities and bodies created by local governments and are meant to protect local industries, economies, and businesses.

Do I have to pay customs charges for the print book order? Chevron down icon Chevron up icon

The orders shipped to the countries that are listed under EU27 will not bear custom charges. They are paid by Packt as part of the order.

List of EU27 countries: www.gov.uk/eu-eea:

A custom duty or localized taxes may be applicable on the shipment and would be charged by the recipient country outside of the EU27 which should be paid by the customer and these duties are not included in the shipping charges been charged on the order.

How do I know my custom duty charges? Chevron down icon Chevron up icon

The amount of duty payable varies greatly depending on the imported goods, the country of origin and several other factors like the total invoice amount or dimensions like weight, and other such criteria applicable in your country.

For example:

  • If you live in Mexico, and the declared value of your ordered items is over $ 50, for you to receive a package, you will have to pay additional import tax of 19% which will be $ 9.50 to the courier service.
  • Whereas if you live in Turkey, and the declared value of your ordered items is over € 22, for you to receive a package, you will have to pay additional import tax of 18% which will be € 3.96 to the courier service.
How can I cancel my order? Chevron down icon Chevron up icon

Cancellation Policy for Published Printed Books:

You can cancel any order within 1 hour of placing the order. Simply contact customercare@packt.com with your order details or payment transaction id. If your order has already started the shipment process, we will do our best to stop it. However, if it is already on the way to you then when you receive it, you can contact us at customercare@packt.com using the returns and refund process.

Please understand that Packt Publishing cannot provide refunds or cancel any order except for the cases described in our Return Policy (i.e. Packt Publishing agrees to replace your printed book because it arrives damaged or material defect in book), Packt Publishing will not accept returns.

What is your returns and refunds policy? Chevron down icon Chevron up icon

Return Policy:

We want you to be happy with your purchase from Packtpub.com. We will not hassle you with returning print books to us. If the print book you receive from us is incorrect, damaged, doesn't work or is unacceptably late, please contact Customer Relations Team on customercare@packt.com with the order number and issue details as explained below:

  1. If you ordered (eBook, Video or Print Book) incorrectly or accidentally, please contact Customer Relations Team on customercare@packt.com within one hour of placing the order and we will replace/refund you the item cost.
  2. Sadly, if your eBook or Video file is faulty or a fault occurs during the eBook or Video being made available to you, i.e. during download then you should contact Customer Relations Team within 14 days of purchase on customercare@packt.com who will be able to resolve this issue for you.
  3. You will have a choice of replacement or refund of the problem items.(damaged, defective or incorrect)
  4. Once Customer Care Team confirms that you will be refunded, you should receive the refund within 10 to 12 working days.
  5. If you are only requesting a refund of one book from a multiple order, then we will refund you the appropriate single item.
  6. Where the items were shipped under a free shipping offer, there will be no shipping costs to refund.

On the off chance your printed book arrives damaged, with book material defect, contact our Customer Relation Team on customercare@packt.com within 14 days of receipt of the book with appropriate evidence of damage and we will work with you to secure a replacement copy, if necessary. Please note that each printed book you order from us is individually made by Packt's professional book-printing partner which is on a print-on-demand basis.

What tax is charged? Chevron down icon Chevron up icon

Currently, no tax is charged on the purchase of any print book (subject to change based on the laws and regulations). A localized VAT fee is charged only to our European and UK customers on eBooks, Video and subscriptions that they buy. GST is charged to Indian customers for eBooks and video purchases.

What payment methods can I use? Chevron down icon Chevron up icon

You can pay with the following card types:

  1. Visa Debit
  2. Visa Credit
  3. MasterCard
  4. PayPal
What is the delivery time and cost of print books? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela