concept layer in category javascript

appears as: layers, layers, A layer, layer, layer, The layers, The layer
Deep Learning with JavaScript: Neural networks in TensorFlow.js

This is an excerpt from Manning's book Deep Learning with JavaScript: Neural networks in TensorFlow.js.

  • Neural networks are an approach in machine learning wherein the transformation of data representation is performed by successive steps (or layers) of mathematical operations. The field of deep learning concerns deep neural networks— neural networks with many layers.
  • 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.

    Figure 5.7. A schematic drawing that explains how the new (“truncated”) model is created from MobileNet. See the tf.model() call in listing 5.3 for the corresponding code. Each layer has an input and an output, both of which are SymbolicTensor instances. In the original model, SymbolicTensor0 is the input of the first layer and the input of the entire model. It is used as the input symbolic tensor of the new model. In addition, we take the output symbolic tensor of an intermediate layer (equivalent to conv_pw_13_relu) as the output tensor of the new model. Hence, we get a model that consists of the first two layers of the original model, shown in the bottom part of the diagram. The last layer of the original model, which is the output layer and sometimes referred to as the model’s head, is discarded. This is why approaches like this are sometimes referred to as truncating a model. Note that this diagram depicts models with small numbers of layers for the sake of clarity. What actually happens with the code in listing 5.3 involves a model with many more (93) layers compared to the one shown in this diagram.

    What does that do? By default, the trainable property of each of the model’s layers is true after the model is loaded via the loadLayersModel() method or created from scratch. The trainable property is used during training (that is, calls to the fit() or fitDataset() method). It tells the optimizer whether the layer’s weights should be updated. By default, the weights of all layers of a model are updated during training. But if you set the property to false for some of the model’s layers, the weights of those layers will not be updated during training. In TensorFlow.js terminology, those layers become untrainable, or frozen. The code in listing 5.1 freezes the first seven layers of the model, from the input conv2d layer to the flatten layer, while leaving the last several layers (the dense layers) trainable.

    Listing 8.4. Visualizing the weight distribution of layers (from jena-weather/index.js)
    function visualizeModelLayers(tab, layers, layerNames) {
      layers.forEach((layer, i) => {
        const surface = tfvis.visor().surface({name: layerNames[i], tab});
        tfvis.show.layer(surface, layer);
      });
    }
    JavaScript Application Design: A Build First Approach

    This is an excerpt from Manning's book JavaScript Application Design: A Build First Approach.

    Designing the application itself, including a REST API that can effectively increase maintainability, is only possible by identifying clear cut components with clear purposes so they’re orthogonal (meaning that they don’t fight for resources on any particular concern). In chapter 9 we’ll explore a multi-tiered approach to application design which can help you quickly isolate the web interface from your data and your business logic by strictly defining layers and the communication paths between those layers.

    Armed with the knowledge needed to design a REST API, in the next section you’ll explore the possibility of creating a series of layers for the API. These layers will define the API and help you keep your service modularly structured and testable.