concept nn in category deep learning

appears as: nn, nn, nn, n nn
Deep Learning with PyTorch

This is an excerpt from Manning's book Deep Learning with PyTorch.

PyTorch has a whole submodule dedicated to neural networks, called torch.nn. It contains the building blocks needed to create all sorts of neural network architectures. Those building blocks are called modules in PyTorch parlance (such building blocks are often referred to as layers in other frameworks). A PyTorch module is a Python class deriving from the nn.Module base class. A module can have one or more Parameter instances as attributes, which are tensors whose values are optimized during the training process (think w and b in our linear model). A module can also have one or more submodules (subclasses of nn.Module) as attributes, and it will be able to track their parameters as well.

We said earlier that taking a 1D view of our input image and multiplying it by an n_output_features × n_input_features weight matrix, as is done in nn.Linear, means for each channel in the image, computing a weighted sum of all the pixels multiplied by a set of weights, one per output feature.

# In[22]:
model = nn.Sequential(
            nn.Conv2d(3, 16, kernel_size=3, padding=1),
            nn.Tanh(),
            nn.MaxPool2d(2),
            nn.Conv2d(16, 8, kernel_size=3, padding=1),
            nn.Tanh(),
            nn.MaxPool2d(2),
            # ...
            )

The Net class is equivalent to the nn.Sequential model we built earlier in terms of submodules; but by writing the forward function explicitly, we can manipulate the output of self.pool3 directly and call view on it to turn it into a B × N vector. Note that we leave the batch dimension as -1 in the call to view, since in principle we don’t know how many samples will be in the batch.

Figure 8.10 Our baseline convolu-tional network architecture

Here we use a subclass of nn.Module to contain our entire model. We could also use subclasses to define new building blocks for more complex networks. Picking up on the diagram style in chapter 6, our network looks like the one shown in figure 8.10. We are making some ad hoc choices about what information to present where.