concept inference in category deep learning

This is an excerpt from Manning's book Deep Learning with Python, Second Edition.
A better way to do inference is to use the
predict()method. It will iterate over the data in small batches, and return a NumPy array of predictions. And unlikecall, it can also process TensorFlow Dataset objects.
Second, since your production model will only be used to output predictions (a phase called inference), rather than for training, you have room to perform various optimizations that can make the model faster and reduce its memory footprint.
The topic of model deployment is covered much more in-depth in chapter TODO; what follows is only a quick overview of the different options that you have available.

This is an excerpt from Manning's book Deep Learning with JavaScript: Neural networks in TensorFlow.js.
Figure 1.3. A more detailed view of the machine-learning paradigm than that in figure 1.2. The workflow of machine learning consists of two phases: training and inference. Training is the process of the machine automatically discovering the rules that convert the data into answers. The learned rules, encapsulated in a trained “model,” are the fruit of the training phase and form the basis of the inference phase. Inference means using the model to obtain answers for new data.
![]()
Lowered inference latency—For certain types of applications, the requirement for latency is so stringent that the deep-learning models must be run on the client side. Any applications that involve real-time audio, image, and video data fall into this category. Consider what will happen if image frames need to be transferred to the server for inference. Suppose images are captured from a webcam at a modest size of 400 × 400 pixels with three color channels (RGB) and an 8-bit depth per color channel at a rate of 10 frames per second. Even with JPEG compression, each image has a size of about 150 Kb. On a typical mobile network with an approximately 300-Kbps upload bandwidth, it can take more than 500 milliseconds to upload each image, leading to a latency that is noticeable and perhaps unacceptable for certain applications (for example, games). This calculation doesn’t take into account the fluctuation in (and possible loss of) network connectivity, the additional time it takes to download the inference results, and the vast amount of mobile data usage, each of which can be a showstopper. Client-side inference addresses these potential latency and connectivity concerns by keeping the data and the computation on the device. It is impossible to run real-time machine-learning applications such as labeling objects and detecting poses in webcam images without the model running purely on the client. Even for applications without latency requirements, the reduction in model inference latency can lead to greater responsiveness and hence an improved user experience. Data privacy—Another benefit of leaving the training and inference data on the client is the protection of users’ privacy. The topic of data privacy is becoming increasingly important today. For certain types of applications, data privacy is an absolute requirement. Applications related to health and medical data are a prominent example. Consider a “skin disease diagnosis aid” that collects images of a patient’s skin from their webcam and uses deep learning to generate possible diagnoses of the skin condition. Health information privacy regulations in many countries will not allow the images to be transferred to a centralized server for inference. By running the model inference in the browser, no data needs to ever leave the user’s phone or be stored anywhere, ensuring the privacy of the user’s health data. Consider another browser-based application that uses deep learning to provide users with suggestions on how to improve the text they write in the application. Some users may use this application to write sensitive content such as legal documents and will not be comfortable with the data being transferred to a remote server via the public internet. Running the model purely in client-side browser JavaScript is an effective way to address this concern. Instant WebGL acceleration—In addition to the availability of data, another prerequisite for running machine-learning models in the web browser is sufficient compute power through GPU acceleration. As mentioned earlier, many state-of-the-art deep-learning models are so computationally intensive that acceleration through parallel computation on the GPU is a must (unless you are willing to let users wait for minutes for a single inference result, which rarely happens in real applications). Fortunately, modern web browsers come equipped with the WebGL API, which, even though it was originally designed for accelerated rendering of 2D and 3D graphics, can be ingeniously leveraged for the kind of parallel computation required for accelerating neural networks. The authors of TensorFlow.js painstakingly wrapped WebGL-based acceleration of the deep-learning components in the library, so the acceleration is available to you through a single line of JavaScript import. WebGL-based acceleration of neural networks may not be perfectly on par with native, tailored GPU acceleration such as NVIDIA’s CUDA and CuDNN (used by Python deep-learning libraries such as TensorFlow and PyTorch), but it still leads to orders of magnitude speedup of neural networks and enables real-time inference such as what PoseNet extraction of a human-body pose offers. If performing inference on pretrained models is expensive, performing training or transfer learning on such models is even more so. Training and transfer learning enable exciting applications such as personalization of deep-learning models, frontend visualization of deep learning, and federated learning (training the same model on many devices, then aggregating the results of the training to obtain a good model). The WebGL acceleration of TensorFlow.js makes it possible to train or fine-tune neural networks with sufficient speed, purely inside the web browser.
When we tried a linear-regression model and an MLP on the task in the previous chapter, we flattened the 2D input features to 1D by using a tf.layers.flatten layer (see listing 8.2 and figure 8.2). The flattening step was necessary because both the linear regressor and the MLP used dense layers to handle the input data, and dense layers require the input data to be 1D for each input example. This means that the information from all the time steps is mixed together in a way that erases the significance of which step comes first and which one next, which time step follows which other one, how far apart two time steps are, and so forth. In other words, it doesn’t matter how we order the 240 time steps when we flatten the 2D tensor of shape [240, 14] into the 1D tensor of shape [3360] as long as we are consistent between training and inference. You can confirm this point experimentally in exercise 1 at the end of this chapter. But from a theoretical point of view, this lack of sensitivity to the order of data elements can be understood in the following way. At the core of a dense layer is a set of linear equations, each of which multiplies every input feature value [x1, x2, …, xn] with a tunable coefficient from the kernel [k1, k2, …, kn]:
Listing 9.9. Using the trained 1D convnet for inference in the frontend
predict(text) { const inputText = #1 text.trim().toLowerCase().replace(/(\.|\,|\!)/g, '').split(' '); #1 const sequence = inputText.map(word => { let wordIndex = #2 this.wordIndex[word] + this.indexFrom; #2 if (wordIndex > this.vocabularySize) { wordIndex = OOV_INDEX; #3 } return wordIndex; }); const paddedSequence = #4 padSequences([sequence], this.maxLen); #4 const input = tf.tensor2d( #5 paddedSequence, [1, this.maxLen]); #5 const beginMs = performance.now(); #6 const predictOut = this.model.predict(input); #7 const score = predictOut.dataSync()[0]; predictOut.dispose(); const endMs = performance.now(); return {score: score, elapsed: (endMs - beginMs)}; }