Summary

Chapter 13

TensorFlow.js enables developers to create machine learning solutions that can run in a web browser using JavaScript, a web programming language that almost all programmers and developers know already. Attention has also been paid to not allow use of any other constructs or concepts like AJAX, data binding, object-orientation etc. so as to not confuse the learners and readers with topics they may not be versed in.

Overview of Key Concepts

The following sections cover the key concepts from this book to help you understand the algorithms that are included in the TensorFlow.js library.

Artificial Intelligence (AI)

Artificial intelligence or AI is a general term and can be used for any automation that can mimic human intelligence or behavior. AI is a broad field and apply to software, spreadsheets, and/or robotics. It is common for AI to be described as the way for software to ‘make decisions’ similar to how humans make decisions. While true, this description is not entirely accurate and perhaps a bit misleading, since artificial intelligence or AI is a way for software to ‘learn’ similar to how humans learn.

Machine Learning

A sub-category of artificial intelligence, machine learning refers to creation of models using statistical methods and provided data to learn from that data so that new predictions and forecasts can be made using the generated model.

Deep Learning

A further sub-category of machine learning is deep learning, and creates a model for prediction using data. This is similar to machine learning in that it uses data for model generation, however, that is the extent of similarity between the two forms. While machine learning uses statistical methods to create predictive models, deep learning uses artificial neural networks (ANN) to create these models.

Note The keyword here is ‘sub-category’, which means that deep learning is a subset of machine learning, which in turn is a subset of artificial intelligence.

The relationship between artificial intelligence (AI), machine learning (ML), and deep learning (DL) is shown in Figure 2-1. Data scientists tend to refer to the data problems they are solving as machine learning problems, even if they use neural networks and deep learning algorithms to learn from the data provided to them; the chief reason for titling this book ‘Machine Learning Solutions using TensorFlow.js’, even though the branch of learning algorithms used to perform predictions falls under deep learning. This is further illustrated in Figure 2-8, and the problems in each of these categories is shown in Figure 6-3a.

TensorFlow.js

The TensorFlow.js library enables programmers, developers, and/or data scientists to develop machine learning solution in the web browser using JavaScript. The programming language is known by most developers and so they are absolved from learning a new server-based programming language like Python, C++, Java, etc. Also, chances are that you have already used a third-party library alongside JavaScript and TensorFlow.js is no different.

The library is visually depicted in Figure 13-1. The diagram can be explained using the following points, as labeled in the figure.

  1. Machine Learning Solutions Machine learning solutions developed using the TensorFlow.js library comprise of two parts: the program and the model. The solution executes in a web browser environment, such as Microsoft Internet Explorer, etc.

  2. TensorFlow.js Code A programmer or developer writes source code in JavaScript using an integrated development environment (IDE) like Visual Studio Code. The code makes use of the TensorFlow.js library. The custom code exists in HTML and JS files, and is composed of a program (a computer representation of human instructions or logic to capture some data and perform a prediction using a neural network) and a model (see items 3 and 4).

  3. Model Training To create a machine learning model, a programmer, developer, or data scientist requires data, usually from multiple sources. The data is partitioned into two sets: training and test. The machine learning model is created using the training set, and its accuracy is gauged using the test set. This process is covered in detail in Chapter 6 in the section titled ‘Model Training’.

  4. Transfer Learning Developers or data scientists do not always have to train a model to perform predictions. A base ML model created using a different machine learning framework can be used by TensorFlow.js to make predictions or forecasts along with some re-training, a process known as transfer learning and explained in Chapter 4 in the section titled ‘Transfer Learning’.

Figure 13-1: The TensorFlow.js Library

Architecture

Do not confuse the architecture of TensorFlow.js with the TensorFlow library. While the former is created to execute in the web browser with the code being in JavaScript, the latter is designed to run on a user computer or server inside a Jupyter notebook with the coding mainly being in Python.

A view of the API’s offered by TensorFlow.js is illustrated in Figure 4-3. It would not be accurate to state that all the four flavors of TensorFlow.js (mentioned on the first page of Chapter 4) have the same architecture. They are, however, designed to offer the same results. Figure 13-2 shows the high-level architectures of the four flavors that lie under the TensorFlow.js name.

Figure 13-2: TensorFlow.js Flavors

Note Viewed in tandem Figure 12-1, the illustration in Figure 13-2 lists the APIs each flavor of the TensorFlow.js library uses and the types of applications that can be developed.

Algorithms

The following list presents the types of data you need for machine learning. You can view a definition of these types by referring to one of the ‘Note’ sections in Chapter 2.

  • Numeric

  • Image

  • Video

  • Sound

  • Text

  • Time-series

The datatypes above have been visually illustrated in Figure 13-4 below:

Figure 13-4: Machine Learning Algorithms in TensorFlow.js

Each datatype in the above diagram is further linked to a machine learning algorithm in the TensorFlow.js library. These algorithms are described in the following text:

  • Multi-Layer Perceptron (MLP): Refer to Figure 2-9b to view an example of a multi-layer perceptron (MLP). A multi-layer perceptron or MLP is composed of multiple layers of perceptrons, and should have at least three layers; one of each input, hidden, and output layers (the neural network in Figure 2-9a is not a multi-layer perceptron, but the one in Figure 2-9c is). As shown in Figure 13-3, MLP’s are meant for numeric (vector) data.

  • Convolutional Neural Network (CNN): A convolutional neural network (CNN) or ConvNet allows a visual exhibit or sequential data to be analyzed for image recognition, image classification, natural language processing, time-series data analysis in finance, healthcare, etc. and product recommendation. The datatypes in Figure13-3 can be mapped to the previous statement as follows:

  • Image (2D)

  • Video (2D or 3D)

  • Sound (2D)

  • Text (1D)

  • Time-series (1D)

  • Recurrent Neural Network (RNN): A recurrent neural network (RNN) is used to process sequential inputs that are of different lengths. The way an RNN works is by maintaining the state regardless of its iterations. If you were to refer to Figure 13-3, recurrent neural networks can be used for speech recognition, handwriting recognition, and time-series prediction. All three can be performed using long short-term memory (LSTM) architecture.

Machine learning continues to evolve at a rapid pace, eliminating or reducing the limitations of the technology. This advancement can be attributed to the following trends in computing:

  1. Hardware Availability – Computing power has evolved (especially in the public cloud) whereby it is accessible by enterprises of every size and even individuals. Furthermore, the task of managing or administering large machines is only a fraction of what it used to be.

  2. Storage – Storing and keeping vast amounts of information is possible now more than ever, and processing this data in various formats is another trend that is speeding up the adoption of machine learning applications.

  3. Automation – Automated machine learning has computerized many of the cumbersome manual tasks that were previously performed by a human operator e.g. neural network architecture definition, hyper-parameter tuning, feature engineering, etc.

  4. Edge Computing – To allow machine/deep learning to happen on devices are microcontrollers, training and prediction can now take place on non-server environments close to the end-users. This also eliminates the need to transfer any data over the wire. Machine learning on single-computer processors allow ML to be used in a wide variety of areas such as agriculture, construction, healthcare, etc.

  5. Transfer Learning – Re-use of models is further enabled by transfer learning, since training, testing, and/or validation can be done on local data whereas the bulk of the training in performed remotely using other data. Also, machine learning models generated using large amounts of data can be used over and over again by other platforms or frameworks.

Concluding Remarks

At the end of the book, I leave you with the hope that you have been able to understand and possibly learn something about machine/deep learning in general and the TensorFlow.js library in particular. One book is, however, not enough to learn a topic in its entirety and requires other reading material, along with research papers, articles, practice exercises, and connection with experts over a long period of time. You may have barely scratched the surfaces with this text, you still have a long way to go in your journey, so I wish you the best of luck in all your future endeavors.

Last updated