concept section in category deep learning

This is an excerpt from Manning's book Math and Architectures of Deep Learning.
As stated in section 1.1, to create a brain-like machine that makes classifications or estimations, we have to find a mathematical function (model) that transforms inputs into corresponding desired outputs. Sadly however, in typical real life situations, we do not know that transformation function. For instance, we do not know the function that takes in past prices, world events etc and estimates the future price of a stock - something that stops us from building a stock price estimator and getting rich. All we have is the training data - a set of inputs on which the output is known. How do we proceed then? Answer, we will try to model the unknown function. This means, we will create a function that will be a proxy or surrogate to the unknown function. Viewed in this way, machine learning is nothing but function approximation - we are simply trying to approximate the unknown classification or estimation function.
One particularly significant notion in machine learning and data science is the idea of a feature vector. This is essentially a vector that describes various properties of the object being dealt with in a particular machine learning problem. We will illustrate the idea with an example from the world of Natural Language Processing (NLP). Suppose we have a set of documents. We want to create a document retrieval system where, given a new document, we have to retrieve “similar” documents in the system. This essentially boils down to estimating similarity between documents in a quantitative fashion. We will study this problem in detail later, but for now we want to note that the most natural way to approach this is to create feature vectors for each document that quantitatively describe the document. Later, in section 2.5.6 we will see how to measure the similarity between these vectors, for now let us focus on simply creating descriptor vectors for the documents. A popular way to do this is to choose a set of interesting words – we typically exclude words like “and”, “if”, “to” which are present in all documents from this list - count the number of occurrence of interesting words in each document and make a vector of these. Table 2.2 shows a toy example with 6 documents and corresponding feature vectors. For simplicity, we have considered only two (“gun” and “violence” in plural or singular, upper or lower case) of the possible set of words.
Table 5.8: Point clouds formed by sampling Multivariate Gaussians with same
= [0, 0]T but different Σs. For the red point cloud Σ = [[5, 0] , [0, 5]]. For the blue point cloud Σ = [[5, 0] , [0, 0.5]]. For the green point cloud Σ = [[2.75, 2.25] , [2.25, 2.75]]. For the magenta point cloud Σ = [[2.75, −2.25] , [−2.25, 2.75]]. These point clouds correspond to the bases of the bell curves for multivariate Gaussian probability densities. All the point clouds except the red one may be replaced by an univariate Gaussian after rotation to align coordinate axes with eigenvectors of Σ (dimensionality reduction). See sections 4.3, 4.4, 4.5 for details. Interactive contour plots for the base of the bell curve can be found at https://github.com/krishnonwork/mathematical-methods-in-deep-learningipython/blob/master/python/ch5/5.9.2-normal-distribution.ipynb
![]()

This is an excerpt from Manning's book Deep Learning with Structured Data.
In this book I emphasize providing a practical exploration of the end-to-end process of deep learning with tabular, structured data, starting with the raw input data and going right to the deployed, trained deep learning model. Because this book covers such a broad scope, it means it won’t always be possible to go into details about every related technical topic. However, throughout this book, where appropriate, I will refer to sections in Deep Learning with Python, other Manning publications, and technical articles for more details on related topics. In addition, chapter 9 contains a section with more recommended resources on the theoretical background of deep learning. Deep Learning with Python is an excellent book that covers many topics that are complementary to the topics described in this book, and I highly recommend it as an optional companion to this book.
Iterate through training runs, checking performance and making adjustments (to hyperparameters, including the number of epochs, or iterations through the training data) to get the optimal performance for your model. Once you have completed an initial run to validate that your model code can run without errors, you need to make repeated experiments to observe your model’s behaviour and see what you can adjust to improve performance. These iterative experiments (such as the experiments described in section 6.10) involve a certain amount of trial and error. There’s a temptation when you start training a deep learning model to either start off with long runs with many epochs (in the hope that the model will find its way to an optimal state if it’s just allowed to run long enough) or to change multiple hyperparameters (such as learning rate or batch size) at the same time in the hope that you will find the golden combination of hyperparameter settings that will boost the performance of your model. I strongly recommend starting off slowly. Start with a small number of epochs (iterations through the entire training set) and with a subset of the training set so that you can get a decent number of training runs done quickly. Note that in the case of the streetcar delay prediction model the training set is not huge so we can train with the whole training set right from the start and still complete a modest run of 20 epochs in less than 5 minutes in a standard Paperspace Gradient environment. As the performance of your model improves you can do longer runs with more epochs to observe whether additional iterations through the training data improve the performance of your model. I also strongly recommend that you only adjust one hyperparameter at a time. There are several techniques available to search through hyperparameters Save the model from your optimal training run. Section 6.9 describes how you can explicitly save trained models once you have completed a training run. However, as you do longer training runs with more epochs you will probably find that the model reaches its peak performance on an epoch that is not the last epoch. How can you ensure that you save the model on the epoch where the model has its optimal performance? Section 6.7 describes how you can use the callback facility in Keras to save the model regularly during the training run and to stop training when the model’s performance is no longer improving. With this facility you can do a long training run and be confident that at the end of the run you will have saved the model with the best performance, even if that optimal performance was achieved in the middle of the run. Validate your trained model with the test set and score at least one new, single data point. You want to get an early validation of the deployment process that you will create in chapter 8 by applying your trained model to a data point and examining the result. You can think of this early validation as a dress rehearsal for your trained model. Just as a dress rehearsal is a way for the cast and crew of a performance to go through their paces before they need to face a live audience, applying a single data point to your trained model is a way to get a taste of how it will behave before you go through the effort of deployment. Section 6.8 describes how you can apply your model to the entire test set and also how you can get an early view of how your model will be have once it has been deployed by exercising it on a brand new data point that is completely outside of your original data set. These two activities are related but have different goals. Applying your trained model to the test set gives you the best sense possible of its performance based on the dataset you have available since you are exercising the model with data that was not involved in the training process. By contrast, one-off scoring, or scoring on a net new data point, not taken from your original dataset, forces you to exercise the trained model like it will be exercised when it is deployed but without having to go through all the work required to complete model deployment. One-off scoring gives you a quick glimpse into your model’s behaviour once it is deployed and helps you to anticipate and correct problems (such as your model requiring data that won’t be available when scoring happens) before you deploy the model.
Of the four possible outcomes, we want to get as high a proportion of true negatives and true positives as possible. However, as we go through the iterative process of training the model, we will see that there is a tradeoff between the number of false negatives and the number of false positives. Consider experiment 1 described in section 6.10. In this experiment we do not make any adjustments in the model to account for the training dataset being very imbalanced. Delays are rare. Only around 2% of all route/direction/timeslot combinations in the training dataset have delays. If we don’t account for this imbalance in the training dataset, the training process (which is optimized for accuracy in experiment 1) will generate weights for the model that result in the trained model predicting “no delay” all the time. Such a model will have what looks like great accuracy – over 97%. However, this model would be useless for our user because it never predicts that there will be a delay. While delays may be rare, the users of the model need to know when delays are likely to happen so they can make alternate travel arrangements. Figure 6.3 shows the results of applying the model from experiment 1 to the test dataset: zero true positives and the maximum possible number of false negatives.
Figure 6.3 outcomes of applying to test set to the trained model for experiment 1
![]()
As we go through the experiments in section 6.10 you will see that there can be a tradeoff between false negatives and false positives. As we reduce the false negatives (the number of times the model predicts no delay when there is a delay), the false positives can increase (the number of times the model predicts a delay when there is no delay). Obviously, we want the model to have as low a proportion as possible of both false negatives and false positives, but ff we have to make a tradeoff between false negatives and false positives, what is the best result for our end users? For our users, the worst outcome is that the model predicting no delay and then a delay occurring. In other words, the worst outcome for our users is false negative. With a false positive, if the user takes the model’s advice and walks or takes a taxi to avoid a streetcar delay that never happens, they still have a decent chance of arriving at their destination on time. However, with a false negative, if the user follows the model’s advice and takes the streetcar, they miss the opportunity to take an alternate form of transit and they risk arriving at their destination late.
As you will see in section 6.6, we direct the model training towards our overall goal by monitoring two measurements:

This is an excerpt from Manning's book Deep Learning with PyTorch.
In this case, we defined a
preprocessfunction that will scale the input image to 256 × 256, crop the image to 224 × 224 around the center, transform it to a tensor (a PyTorch multidimensional array: in this case, a 3D array with color, height, and width), and normalize its RGB (red, green, blue) components so that they have defined means and standard deviations. These need to match what was presented to the network during training, if we want the network to produce meaningful answers. We’ll go into more depth about transforms when we dive into making our own image-recognition models in section 7.1.3.
Here, we normalize just a single batch of images because we do not know yet how to operate on an entire dataset. In working with images, it is good practice to compute the mean and standard deviation on all the training data in advance and then subtract and divide by these fixed, precomputed quantities. We saw this in the preprocessing for the image classifier in section 2.1.4.
We can perform several other operations on inputs, such as geometric transformations like rotations, scaling, and cropping. These may help with training or may be required to make an arbitrary input conform to the input requirements of a network, like the size of the image. We will stumble on quite a few of these strategies in section 12.6. For now, just remember that you have image-manipulation options available.

This is an excerpt from Manning's book Probabilistic Deep Learning: With Python, Keras and TensorFlow Probability.
Let’s first have a look at non-probabilistic classification. To make it easy and illustrative, we use the image classification example again. The goal in image classification is to predict, for a given image, which class it corresponds to. In the imageNet competition in section 1.2, there were 1000 different classes. In the face recognition example, there were only two classes - Chantal and Sara.
Section 8.2.1 and 8.2.2 are somewhat involved but give you some insights into the derivation of the VI approximation. You can skip them if you’re not interested in the derivations. In section 8.3, you’ll learn how to use the TFP implementation of the VI method.

This is an excerpt from Manning's book Deep Learning with JavaScript: Neural networks in TensorFlow.js.
Whether you are a frontend JavaScript developer or a Node.js developer, based on what you learned in this chapter, brainstorm a few possible cases in which you can apply machine learning to the system you are working on to make it more intelligent. For inspiration, refer to tables 1.1 and 1.2, as well as section 1.3.3. Some further examples include the following:
- A fashion website that sells accessories such as sunglasses captures images of users’ faces using the webcam and detects facial landmark points using a deep neural network running on TensorFlow.js. The detected landmarks are then used to synthesize an image of the sunglasses overlaid on the user’s face to simulate a try-on experience in the web page. The experience is realistic because the simulated try-on can run with low latency and at a high frame rate thanks to client-side inference. The user’s data privacy is respected because the captured facial image never leaves the browser.
- A mobile sports app written in React Native (a cross-platform JavaScript library for creating native mobile apps) tracks users’ exercise. Using the HTML5 API, the app accesses real-time data from the phone’s gyroscope and accelerometer. The data is run through a TensorFlow.js-powered model that automatically detects the user’s current activity type (for example, resting versus walking versus jogging versus sprinting).
- A browser extension automatically detects whether the person using the device is a child or an adult (by using images captured from the webcam at a frame rate of once per 5 seconds and a computer-vision model powered by TensorFlow.js) and uses the information to block or grant access to certain websites accordingly.
- A browser-based programming environment uses a recurrent neural network implemented with TensorFlow.js to detect typos in code comments.
- A Node.js-based server-side application that coordinates a cargo logistics service uses real-time signals such as carrier status, cargo type and quantity, date/time, and traffic information to predict the estimated time of arrival (ETA) for each transaction. The training and inference pipelines are all written in Node.js, using TensorFlow.js, simplifying the server stack.
Our example loads the pretrained base model from an HTTP server instead of training it from scratch so as not to obscure the workflow’s key parts. Recall from section 4.3.3 that TensorFlow.js provides the tf.loadLayersModel() method for loading pretrained models. This is called in the loader.js file:
Figure 5.5. Transfer learning can be divided into three types according to whether the output shape and activation of the new model are the same as or different from those of the original model. Case A: the output shape and the activation function of the new model match those of the base model. The transfer of the MNIST model onto new digits in section 5.1.1 is an example of this type of transfer learning. Case B: the new model has the same activation type as the base model because the original task and the new task are of the same type (for example, both are multiclass classification). However, the output shapes are different (for instance, the new task involves a different number of classes). Examples of this type of transfer learning can be found in section 5.1.2 (controlling a video game in the style of Pac-ManTM 4 through a webcam) and 5.1.3 (recognizing a new set of spoken words). Case C: the new task is of a different type from the original one (such as regression versus classification). The object-detection model based on MobileNet is an example of this type.
![]()
In some cases, even the type of machine-learning task is different from the one the base model has been trained on. For instance, you can perform a regression task (predict a number, as in case C in figure 5.5) by applying transfer learning on the base model trained on a classification task. In section 5.2, you will see a still more intriguing use of transfer learning—predicting an array of numbers, instead of a single one, for the purpose of detecting and localizing objects in images.