concept symbolic tensor in category deep learning

This is an excerpt from Manning's book Deep Learning with JavaScript: Neural networks in TensorFlow.js.
Figure 5.10. A schematic illustration of the way in which the new, end-to-end model is created for transfer learning. This figure should be read in conjunction with listing 5.7. Some parts of the figure that correspond to variables in listing 5.7 are labeled in fixed-width font. Step 1: the output symbolic tensor of the second-to-last dense layer of the original model is obtained (indicated by the thick arrow). It will later be used in step 3. Step 2: the new head model, consisting of a single output dense layer (labeled “dense 3”) is created. Step 3: the apply() method of the new head model is invoked with the symbolic tensor from step 1 as the input argument. The call connects the input to the new head model with the truncated base model from step 1. Step 4: the return value of the apply() call is used in conjunction with the input symbolic tensor of the original model during a call to the tf.model() function. This call returns a new model that contains all the layers of the original model from the first layer to the second-last dense layer, in addition to the dense layer in the new head. In effect, this swaps the old head of the original model with the new head, setting the stage for subsequent training on the transfer data. Note that some (seven) layers of the actual speech-command model are omitted in this diagram for the sake of visual simplicity. In this figure, the tinted layers are trainable, while the white-colored layers are untrainable.
![]()
You have seen tensors so far. Tensor is the basic data type (also abbreviated as dtype) in TensorFlow.js. A tensor object carries concrete numeric values of a given shape and dtype, backed by storage on WebGL textures (if in a WebGL-enabled browser) or CPU/GPU memory (if in Node.js). However, SymbolicTensor is another important class in TensorFlow.js. Instead of holding concrete values, a symbolic tensor specifies only a shape and a dtype. A symbolic tensor can be thought of as a “slot” or a “placeholder,” into which an actual tensor value may be inserted later, given that the tensor value has a compatible shape and dtype. In TensorFlow.js, a layer or model object takes one or more inputs (so far, you’ve only seen cases of one input), and those are represented as one or more symbolic tensors.
Let’s use an analogy that might help you understand a symbolic tensor. Consider a function in a programming language like Java or TypeScript (or any other statically typed language you are familiar with). The function takes one or more input arguments. Each argument of a function has a type, which stipulates what kind of variables may be passed in as the argument. However, the argument itself doesn’t hold any concrete values. By itself, the argument is just a placeholder. A symbolic tensor is analogous to a function argument: it specifies what kind (combination of shape[6] and dtype) of tensors may be used in that slot. By parallel, a function in a statically typed language has a return type. This is comparable to the output symbolic tensor of a model or layer object. It is a “blueprint” for the shape and dtype of the actual tensor values that the model or layer object will output.
6A difference between a tensor’s shape and a symbolic tensor’s shape is that the former always has fully specified dimensions (such as [8, 32, 20]), while the latter may have undetermined dimensions (such as [null, null, 20]). You have already seen this in the “Output shape” column of the model summaries.
In TensorFlow.js, two important attributes of a model object are its inputs and outputs. Each of these is an array of symbolic tensors. For a model with exactly one input and exactly one output, both arrays have a length of 1. Similarly, a layer object has two attributes: input and output, each of which is a symbolic tensor. Symbolic tensors can be used to create a new model. This is a new way of creating models in TensorFlow.js, which is different from the approach you’ve seen before: namely, creating sequential models with tf.sequential() and subsequent calls to the add() method. In the new approach, we use the tf.model() function, which takes a configuration object with two mandatory fields: inputs and outputs. The inputs field is required to be a symbolic tensor (or, alternatively, an array of symbolic tensors), and likewise for the outputs field. Therefore, we can obtain the symbolic tensors from the original MobileNet model and feed them to a tf.model() call. The result is a new model that consists of a part of the original MobileNet.
This process is illustrated schematically in figure 5.7. (Note that the figure reduces the number of layers from the actual MobileNet model for the sake of a simple-looking diagram.) The important thing to realize is that the symbolic tensors taken from the original model and handed to the tf.model() call are not isolated objects. Instead, they carry information about what layers they belong to and how the layers are connected to each other. For readers familiar with graphs in data structure, the original model is a graph of symbolic tensors, with the connecting edges being the layers. By specifying the inputs and outputs of the new model as symbolic tensors in the original model, we are extracting a subgraph of the original MobileNet graph. The subgraph, which becomes the new model, contains the first few (in particular, the first 87) layers of MobileNet, while the last 6 layers are left out. The last few layers of a deep convnet are sometimes referred to as the head. What we are doing with the tf.model() call can be referred to as truncating the model. The truncated MobileNet preserves the feature-extracting layers while discarding the head. Why does the head contain six layers? This is because those layers are specific to the 1,000-class classification task that the MobileNet was originally trained on. The layers are not useful for the four-class classification task we are facing.