concept base model in category deep learning

This is an excerpt from Manning's book Deep Learning with JavaScript: Neural networks in TensorFlow.js.
In essence, transfer learning is about speeding up a new learning task by reusing the results of previous learning. It involves using a model already trained on a dataset to perform a different but related machine-learning task. The already-trained model is referred to as the base model. Transfer learning sometimes involves retraining the base model and sometimes involves creating a new model on top of the base model. We refer to the new model as the transfer model. As figure 5.1 shows, the amount of data used for this retraining process is usually much smaller compared to the data that went into training the base model (as with the two examples given at the beginning of this chapter). As such, transfer learning is often much less time-and resource-consuming compared to the base model’s training process. This makes it feasible to perform transfer learning in a resource-restricted environment like the browser using TensorFlow.js. And thus transfer learning is an important topic for TensorFlow.js learners.
Figure 5.1. The general workflow of transfer learning. A large dataset goes into the training of the base model. This initial training process is often long and computationally heavy. The base model is then retrained, possibly by becoming part of a new model. The retraining process usually involves a dataset much smaller than the original one. The computation involved in the retraining is significantly less than the initial training and can happen on an edge device, such as a laptop or a phone running TensorFlow.js.
![]()
private createTransferModelFromBaseModel(): void { const layers = this.baseModel.layers; let layerIndex = layers.length - 2; while (layerIndex >= 0) { #1 if (layers[layerIndex].getClassName().toLowerCase() === 'dense') { #1 break; #1 } #1 layerIndex--; #1 } #1 if (layerIndex < 0) { throw new Error('Cannot find a hidden dense layer in the base model.'); } this.secondLastBaseDenseLayer = #2 layers[layerIndex]; #2 const truncatedBaseOutput = layers[layerIndex].output as #3 tf.SymbolicTensor; #3 this.transferHead = tf.layers.dense({ #4 units: this.words.length, #4 activation: 'softmax', #4 inputShape: truncatedBaseOutput.shape.slice(1) #4 })); #4 const transferOutput = #5 this.transferHead.apply(truncatedBaseOutput) as tf.SymbolicTensor; #5 this.model = #6 tf.model({inputs: this.baseModel.inputs, outputs: transferOutput});#6 }
5.1.2. Transfer learning on incompatible output shapes: Creating a new m- model using outputs from the base model
In the example of transfer learning seen in the previous section, the base model had the same output shape as the new output shape. This property doesn’t hold in many other transfer-learning cases (see figure 5.5). For example, if you want to use the base model trained initially on the five digits to classify four new digits, the approach previously described will not work. A more common scenario is the following: given a deep convnet that has been trained on the ImageNet classification dataset consisting of 1,000 output classes, you have an image-classification task at hand that involves a much smaller number of output classes (case B in figure 5.5). Perhaps it is a binary-classification problem—whether the image contains a human face or not—or perhaps it is a multiclass-classification problem with only a handful of classes—what kind of commodity item a picture contains (recall the example at the beginning of this chapter). In such cases, the base model’s output shape doesn’t work for the new problem.
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.
These cases all involve a desired output shape that differs from that of the base model. This makes it necessary to construct a new model. But because we are doing transfer learning, the new model will not be created from scratch. Instead, it will use the base model. We will illustrate how to do this in the webcam-transfer-learning example in the tfjs-examples repository.