Building Models with PyTorch¶
Follow along with the video below or on youtube.
In this video, we’ll be discussing some of the tools PyTorch makes available for building deep learning networks.
Parameter, the classes we discuss in this video are all
torch.nn.Module. This is the PyTorch base class meant
to encapsulate behaviors specific to PyTorch Models and their
One important behavior of
torch.nn.Module is registering parameters.
If a particular
Module subclass has learning weights, these weights
are expressed as instances of
class is a subclass of
torch.Tensor, with the special behavior that
when they are assigned as attributes of a
Module, they are added to
the list of that modules parameters. These parameters may be accessed
parameters() method on the
As a simple example, here’s a very simple model with two linear layers and an activation function. We’ll create an instance of it and ask it to report on its parameters:
import torch class TinyModel(torch.nn.Module): def __init__(self): super(TinyModel, self).__init__() self.linear1 = torch.nn.Linear(100, 200) self.activation = torch.nn.ReLU() self.linear2 = torch.nn.Linear(200, 10) self.softmax = torch.nn.Softmax() def forward(self, x): x = self.linear1(x) x = self.activation(x) x = self.linear2(x) x = self.softmax(x) return x tinymodel = TinyModel() print('The model:') print(tinymodel) print('\n\nJust one layer:') print(tinymodel.linear2) print('\n\nModel params:') for param in tinymodel.parameters(): print(param) print('\n\nLayer params:') for param in tinymodel.linear2.parameters(): print(param)
The model: TinyModel( (linear1): Linear(in_features=100, out_features=200, bias=True) (activation): ReLU() (linear2): Linear(in_features=200, out_features=10, bias=True) (softmax): Softmax(dim=None) ) Just one layer: Linear(in_features=200, out_features=10, bias=True) Model params: Parameter containing: tensor([[-0.0993, 0.0624, -0.0443, ..., -0.0280, 0.0326, -0.0171], [-0.0722, 0.0167, -0.0857, ..., -0.0923, 0.0327, -0.0033], [ 0.0704, -0.0360, -0.0967, ..., -0.0348, -0.0425, -0.0233], ..., [ 0.0150, 0.0344, -0.0978, ..., -0.0341, -0.0220, -0.0806], [ 0.0060, 0.0648, -0.0464, ..., -0.0549, -0.0622, -0.0252], [ 0.0849, -0.0319, 0.0362, ..., 0.0401, -0.0580, 0.0413]], requires_grad=True) Parameter containing: tensor([ 0.0916, -0.0946, -0.0708, 0.0103, 0.0403, -0.0784, -0.0104, -0.0242, 0.0886, -0.0861, -0.0526, 0.0517, 0.0390, -0.0574, -0.0513, -0.0146, -0.0442, -0.0303, 0.0030, 0.0609, -0.0446, -0.0196, 0.0894, 0.0324, 0.0053, -0.0031, 0.0580, -0.0584, -0.0073, -0.0510, 0.0510, 0.0878, 0.0528, -0.0913, 0.0906, 0.0220, -0.0550, -0.0015, -0.0757, -0.0216, 0.0553, 0.0047, -0.0786, -0.0146, -0.0068, -0.0974, 0.0381, 0.0056, -0.0144, -0.0078, 0.0149, 0.0598, 0.0844, 0.0587, -0.0268, 0.0605, 0.0488, -0.0579, 0.0413, -0.0907, -0.0040, -0.0349, 0.0102, 0.0365, -0.0131, 0.0877, 0.0582, -0.0324, 0.0653, -0.0285, 0.0432, -0.0441, -0.0716, 0.0160, -0.0692, 0.0830, -0.0275, 0.0240, -0.0668, -0.0686, -0.0580, 0.0627, 0.0106, -0.0661, -0.0467, -0.0791, -0.0118, -0.0578, 0.0090, -0.0318, 0.0454, -0.0758, -0.0375, 0.0330, 0.0476, -0.0029, 0.0342, -0.0192, -0.0870, 0.0605, -0.0762, 0.0030, -0.0420, 0.0363, -0.0781, 0.0681, -0.0455, 0.0618, -0.0996, -0.0597, 0.0763, 0.0481, 0.0336, 0.0788, 0.0171, 0.0140, -0.0795, -0.0191, 0.0858, -0.0930, -0.0514, -0.0031, 0.0455, 0.0342, 0.0999, -0.0037, -0.0328, -0.0304, -0.0857, 0.0976, 0.0575, -0.0161, 0.0552, -0.0376, 0.0811, -0.0849, -0.0481, 0.0233, 0.0678, 0.0284, 0.0284, 0.0876, -0.0287, 0.0356, -0.0447, 0.0633, -0.0451, -0.0045, -0.0486, 0.0809, 0.0954, -0.0748, -0.0857, 0.0972, -0.0404, -0.0921, 0.0401, 0.0502, -0.0595, -0.0315, -0.0994, 0.0262, 0.0731, 0.0621, 0.0830, 0.0495, -0.0370, -0.0252, -0.0638, -0.0704, -0.0838, -0.0220, -0.0891, -0.0568, 0.0095, -0.0381, 0.0462, -0.0576, -0.0096, -0.0115, -0.0645, -0.0480, -0.0279, -0.0054, -0.0102, 0.0983, 0.0890, 0.0441, 0.0840, 0.0630, 0.0021, 0.0361, -0.0023, 0.0316, 0.0658, 0.0173, -0.0197, 0.0997, -0.0250, -0.0844], requires_grad=True) Parameter containing: tensor([[ 0.0210, 0.0611, 0.0258, ..., 0.0030, 0.0088, -0.0265], [ 0.0618, 0.0070, 0.0518, ..., -0.0593, 0.0046, 0.0599], [ 0.0471, -0.0160, 0.0474, ..., -0.0527, 0.0416, 0.0298], ..., [-0.0031, -0.0299, 0.0290, ..., -0.0194, -0.0702, 0.0648], [ 0.0366, -0.0202, -0.0536, ..., 0.0502, -0.0096, -0.0349], [ 0.0517, -0.0451, 0.0526, ..., -0.0585, 0.0274, 0.0351]], requires_grad=True) Parameter containing: tensor([-0.0396, -0.0594, 0.0490, 0.0223, 0.0097, -0.0079, -0.0467, 0.0082, 0.0668, -0.0246], requires_grad=True) Layer params: Parameter containing: tensor([[ 0.0210, 0.0611, 0.0258, ..., 0.0030, 0.0088, -0.0265], [ 0.0618, 0.0070, 0.0518, ..., -0.0593, 0.0046, 0.0599], [ 0.0471, -0.0160, 0.0474, ..., -0.0527, 0.0416, 0.0298], ..., [-0.0031, -0.0299, 0.0290, ..., -0.0194, -0.0702, 0.0648], [ 0.0366, -0.0202, -0.0536, ..., 0.0502, -0.0096, -0.0349], [ 0.0517, -0.0451, 0.0526, ..., -0.0585, 0.0274, 0.0351]], requires_grad=True) Parameter containing: tensor([-0.0396, -0.0594, 0.0490, 0.0223, 0.0097, -0.0079, -0.0467, 0.0082, 0.0668, -0.0246], requires_grad=True)
This shows the fundamental structure of a PyTorch model: there is an
__init__() method that defines the layers and other components of a
model, and a
forward() method where the computation gets done. Note
that we can print the model, or any of its submodules, to learn about
Common Layer Types¶
The most basic type of neural network layer is a linear or fully connected layer. This is a layer where every input influences every output of the layer to a degree specified by the layer’s weights. If a model has m inputs and n outputs, the weights will be an m x n matrix. For example:
lin = torch.nn.Linear(3, 2) x = torch.rand(1, 3) print('Input:') print(x) print('\n\nWeight and Bias parameters:') for param in lin.parameters(): print(param) y = lin(x) print('\n\nOutput:') print(y)
Input: tensor([[0.3235, 0.1506, 0.5374]]) Weight and Bias parameters: Parameter containing: tensor([[-0.2206, -0.0738, -0.3799], [-0.5620, -0.1013, 0.5443]], requires_grad=True) Parameter containing: tensor([ 0.4365, -0.0098], requires_grad=True) Output: tensor([[0.1499, 0.0855]], grad_fn=<AddmmBackward0>)
If you do the matrix multiplication of
x by the linear layer’s
weights, and add the biases, you’ll find that you get the output vector
One other important feature to note: When we checked the weights of our
lin.weight, it reported itself as a
is a subclass of
Tensor), and let us know that it’s tracking
gradients with autograd. This is a default behavior for
that differs from
Linear layers are used widely in deep learning models. One of the most common places you’ll see them is in classifier models, which will usually have one or more linear layers at the end, where the last layer will have n outputs, where n is the number of classes the classifier addresses.
Convolutional layers are built to handle data with a high degree of spatial correlation. They are very commonly used in computer vision, where they detect close groupings of features which the compose into higher-level features. They pop up in other contexts too - for example, in NLP applications, where a word’s immediate context (that is, the other words nearby in the sequence) can affect the meaning of a sentence.
We saw convolutional layers in action in LeNet5 in an earlier video:
import torch.functional as F class LeNet(torch.nn.Module): def __init__(self): super(LeNet, self).__init__() # 1 input image channel (black & white), 6 output channels, 5x5 square convolution # kernel self.conv1 = torch.nn.Conv2d(1, 6, 5) self.conv2 = torch.nn.Conv2d(6, 16, 3) # an affine operation: y = Wx + b self.fc1 = torch.nn.Linear(16 * 6 * 6, 120) # 6*6 from image dimension self.fc2 = torch.nn.Linear(120, 84) self.fc3 = torch.nn.Linear(84, 10) def forward(self, x): # Max pooling over a (2, 2) window x = F.max_pool2d(F.relu(self.conv1(x)), (2, 2)) # If the size is a square you can only specify a single number x = F.max_pool2d(F.relu(self.conv2(x)), 2) x = x.view(-1, self.num_flat_features(x)) x = F.relu(self.fc1(x)) x = F.relu(self.fc2(x)) x = self.fc3(x) return x def num_flat_features(self, x): size = x.size()[1:] # all dimensions except the batch dimension num_features = 1 for s in size: num_features *= s return num_features
Let’s break down what’s happening in the convolutional layers of this
model. Starting with
- LeNet5 is meant to take in a 1x32x32 black & white image. The first argument to a convolutional layer’s constructor is the number of input channels. Here, it is 1. If we were building this model to look at 3-color channels, it would be 3.
- A convolutional layer is like a window that scans over the image, looking for a pattern it recognizes. These patterns are called features, and one of the parameters of a convolutional layer is the number of features we would like it to learn. This is the second argument to the constructor is the number of output features. Here, we’re asking our layer to learn 6 features.
- Just above, I likened the convolutional layer to a window - but how
big is the window? The third argument is the window or kernel
size. Here, the “5” means we’ve chosen a 5x5 kernel. (If you want a
kernel with height different from width, you can specify a tuple for
this argument - e.g.,
(3, 5)to get a 3x5 convolution kernel.)
The output of a convolutional layer is an activation map - a spatial
representation of the presence of features in the input tensor.
conv1 will give us an output tensor of 6x28x28; 6 is the number of
features, and 28 is the height and width of our map. (The 28 comes from
the fact that when scanning a 5-pixel window over a 32-pixel row, there
are only 28 valid positions.)
We then pass the output of the convolution through a ReLU activation function (more on activation functions later), then through a max pooling layer. The max pooling layer takes features near each other in the activation map and groups them together. It does this by reducing the tensor, merging every 2x2 group of cells in the output into a single cell, and assigning that cell the maximum value of the 4 cells that went into it. This gives us a lower-resolution version of the activation map, with dimensions 6x14x14.
Our next convolutional layer,
conv2, expects 6 input channels
(corresponding to the 6 features sought by the first layer), has 16
output channels, and a 3x3 kernel. It puts out a 16x12x12 activation
map, which is again reduced by a max pooling layer to 16x6x6. Prior to
passing this output to the linear layers, it is reshaped to a 16 * 6 *
6 = 576-element vector for consumption by the next layer.
There are convolutional layers for addressing 1D, 2D, and 3D tensors. There are also many more optional arguments for a conv layer constructor, including stride length(e.g., only scanning every second or every third position) in the input, padding (so you can scan out to the edges of the input), and more. See the documentation for more information.
Recurrent neural networks (or RNNs) are used for sequential data - anything from time-series measurements from a scientific instrument to natural language sentences to DNA nucleotides. An RNN does this by maintaining a hidden state that acts as a sort of memory for what it has seen in the sequence so far.
The internal structure of an RNN layer - or its variants, the LSTM (long short-term memory) and GRU (gated recurrent unit) - is moderately complex and beyond the scope of this video, but we’ll show you what one looks like in action with an LSTM-based part-of-speech tagger (a type of classifier that tells you if a word is a noun, verb, etc.):
class LSTMTagger(torch.nn.Module): def __init__(self, embedding_dim, hidden_dim, vocab_size, tagset_size): super(LSTMTagger, self).__init__() self.hidden_dim = hidden_dim self.word_embeddings = torch.nn.Embedding(vocab_size, embedding_dim) # The LSTM takes word embeddings as inputs, and outputs hidden states # with dimensionality hidden_dim. self.lstm = torch.nn.LSTM(embedding_dim, hidden_dim) # The linear layer that maps from hidden state space to tag space self.hidden2tag = torch.nn.Linear(hidden_dim, tagset_size) def forward(self, sentence): embeds = self.word_embeddings(sentence) lstm_out, _ = self.lstm(embeds.view(len(sentence), 1, -1)) tag_space = self.hidden2tag(lstm_out.view(len(sentence), -1)) tag_scores = F.log_softmax(tag_space, dim=1) return tag_scores
The constructor has four arguments:
vocab_sizeis the number of words in the input vocabulary. Each word is a one-hot vector (or unit vector) in a
tagset_sizeis the number of tags in the output set.
embedding_dimis the size of the embedding space for the vocabulary. An embedding maps a vocabulary onto a low-dimensional space, where words with similar meanings are close together in the space.
hidden_dimis the size of the LSTM’s memory.
The input will be a sentence with the words represented as indices of
one-hot vectors. The embedding layer will then map these down to an
embedding_dim-dimensional space. The LSTM takes this sequence of
embeddings and iterates over it, fielding an output vector of length
hidden_dim. The final linear layer acts as a classifier; applying
log_softmax() to the output of the final layer converts the output
into a normalized set of estimated probabilities that a given word maps
to a given tag.
If you’d like to see this network in action, check out the Sequence Models and LSTM Networks tutorial on pytorch.org.
Transformers are multi-purpose networks that have taken over the state
of the art in NLP with models like BERT. A discussion of transformer
architecture is beyond the scope of this video, but PyTorch has a
Transformer class that allows you to define the overall parameters
of a transformer model - the number of attention heads, the number of
encoder & decoder layers, dropout and activation functions, etc. (You
can even build the BERT model from this single class, with the right
torch.nn.Transformer class also has classes to
encapsulate the individual components (
TransformerDecoder) and subcomponents (
TransformerDecoderLayer). For details, check out the
on transformer classes, and the relevant
Other Layers and Functions¶
Data Manipulation Layers¶
There are other layer types that perform important functions in models, but don’t participate in the learning process themselves.
Max pooling (and its twin, min pooling) reduce a tensor by combining cells, and assigning the maximum value of the input cells to the output cell (we saw this). For example:
my_tensor = torch.rand(1, 6, 6) print(my_tensor) maxpool_layer = torch.nn.MaxPool2d(3) print(maxpool_layer(my_tensor))
tensor([[[0.4935, 0.8704, 0.0103, 0.3195, 0.3675, 0.0873], [0.7177, 0.4367, 0.1490, 0.7831, 0.8283, 0.6564], [0.4627, 0.7938, 0.9428, 0.7153, 0.1846, 0.9042], [0.8884, 0.8680, 0.9323, 0.6886, 0.2312, 0.2239], [0.3913, 0.8011, 0.6357, 0.9006, 0.0403, 0.9013], [0.6836, 0.5982, 0.5565, 0.2122, 0.5918, 0.4793]]]) tensor([[[0.9428, 0.9042], [0.9323, 0.9013]]])
If you look closely at the values above, you’ll see that each of the values in the maxpooled output is the maximum value of each quadrant of the 6x6 input.
Normalization layers re-center and normalize the output of one layer before feeding it to another. Centering the and scaling the intermediate tensors has a number of beneficial effects, such as letting you use higher learning rates without exploding/vanishing gradients.
my_tensor = torch.rand(1, 4, 4) * 20 + 5 print(my_tensor) print(my_tensor.mean()) norm_layer = torch.nn.BatchNorm1d(4) normed_tensor = norm_layer(my_tensor) print(normed_tensor) print(normed_tensor.mean())
tensor([[[22.0853, 10.5511, 16.1128, 20.4367], [19.0003, 5.8676, 15.7677, 19.4006], [ 6.8539, 14.3712, 10.9359, 20.7301], [ 9.1778, 5.2112, 6.4339, 14.7063]]]) tensor(13.6026) tensor([[[ 1.0728, -1.5112, -0.2652, 0.7035], [ 0.7307, -1.6735, 0.1389, 0.8039], [-1.2522, 0.2258, -0.4496, 1.4761], [ 0.0808, -1.0040, -0.6696, 1.5928]]], grad_fn=<NativeBatchNormBackward0>) tensor(-2.9802e-08, grad_fn=<MeanBackward0>)
Running the cell above, we’ve added a large scaling factor and offset to
an input tensor; you should see the input tensor’s
in the neighborhood of 15. After running it through the normalization
layer, you can see that the values are smaller, and grouped around zero
- in fact, the mean should be very small (> 1e-8).
This is beneficial because many activation functions (discussed below) have their strongest gradients near 0, but sometimes suffer from vanishing or exploding gradients for inputs that drive them far away from zero. Keeping the data centered around the area of steepest gradient will tend to mean faster, better learning and higher feasible learning rates.
Dropout layers are a tool for encouraging sparse representations in your model - that is, pushing it to do inference with less data.
Dropout layers work by randomly setting parts of the input tensor during training - dropout layers are always turned off for inference. This forces the model to learn against this masked or reduced dataset. For example:
my_tensor = torch.rand(1, 4, 4) dropout = torch.nn.Dropout(p=0.4) print(dropout(my_tensor)) print(dropout(my_tensor))
tensor([[[1.2010e-01, 9.9795e-01, 0.0000e+00, 8.8106e-04], [0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00], [1.4940e+00, 0.0000e+00, 0.0000e+00, 6.7827e-02], [1.3470e+00, 1.3466e-01, 1.5454e-01, 0.0000e+00]]]) tensor([[[1.2010e-01, 9.9795e-01, 0.0000e+00, 8.8106e-04], [1.4537e+00, 1.0243e+00, 1.2697e+00, 0.0000e+00], [1.4940e+00, 1.5261e+00, 0.0000e+00, 0.0000e+00], [1.3470e+00, 0.0000e+00, 1.5454e-01, 0.0000e+00]]])
Above, you can see the effect of dropout on a sample tensor. You can use
p argument to set the probability of an individual
weight dropping out; if you don’t it defaults to 0.5.
Activation functions make deep learning possible. A neural network is really a program - with many parameters - that simulates a mathematical function. If all we did was multiple tensors by layer weights repeatedly, we could only simulate linear functions; further, there would be no point to having many layers, as the whole network would reduce could be reduced to a single matrix multiplication. Inserting non-linear activation functions between layers is what allows a deep learning model to simulate any function, rather than just linear ones.
torch.nn.Module has objects encapsulating all of the major
activation functions including ReLU and its many variants, Tanh,
Hardtanh, sigmoid, and more. It also includes other functions, such as
Softmax, that are most useful at the output stage of a model.
Loss functions tell us how far a model’s prediction is from the correct answer. PyTorch contains a variety of loss functions, including common MSE (mean squared error = L2 norm), Cross Entropy Loss and Negative Likelihood Loss (useful for classifiers), and others.
Total running time of the script: ( 0 minutes 0.082 seconds)