concept nn in category deep learning

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 thenn.Modulebase class. A module can have one or moreParameterinstances as attributes, which are tensors whose values are optimized during the training process (thinkwandbin our linear model). A module can also have one or more submodules (subclasses ofnn.Module) as attributes, and it will be able to track their parameters as well.
# 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
Netclass is equivalent to thenn.Sequentialmodel we built earlier in terms of submodules; but by writing theforwardfunction explicitly, we can manipulate the output ofself.pool3directly and callviewon it to turn it into a B × N vector. Note that we leave the batch dimension as -1 in the call toview, since in principle we don’t know how many samples will be in the batch.Here we use a subclass of
nn.Moduleto 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.