Conv1d (torch.nn.Conv1d
)
A torch.nn.Conv1d
module applies the cross-correlation operation along a given dimension of a tensor. This may seem contradictory at first, because the module's name implies that the underlying operation should be convolution, yet both operations are similar.
Note
Please note that the cross-correlation operation \(\star\) is used instead of convolution \(\ast\) even when the module name suggests the opposite. The main difference between these two operations is the kernel \(g\) ordering, but the number of computations are equivalent. For this reason, we will use the term cross-correlation and convolution interchangeably hereafter. $$ \left(f\ast g\right)[n]=\sum\limits_{k=0}^{K-1}f[n]\times g[n-k]\qquad \left(\text{convolution}\right)\\~\\ \left(f\star g\right)[n]=\sum\limits_{k=0}^{K-1}f[n]\times g[n+k]\qquad \left(\text{cross-correlation}\right) $$
A torch.nn.Conv1d
module expects an input of size \(\left(N,C_{\text{in}}, L_{\text{in}}\right)\) or \(\left(C_{\text{in}}, L_{\text{in}}\right)\) to produce an output of size \(\left(N,C_{\text{out}}, L_{\text{out}}\right)\) or \(\left(C_{\text{out}}, L_{\text{out}}\right)\) performing the following operation
Where
- \(N\) is the batch size.
- \(C_{\text{in}}\) is the number of input channels.
- \(C_{\text{out}}\) is the number of output channels.
- \(L_{\text{in}}\) is the length of the input tensor (i.e.
x.size(-1)
assuming an input tensorx
). - \(L_{\text{out}}\) is the length of the output tensor (i.e.
y.size(-1)
assuming an output tensory
). - \(\star\) is the cross-correlation operator.
Additionally, \(L_{\text{out}}\) (y.size(-1)
) will depend on \(L_{\text{in}}\) (x.size(-1)
), padding
, dilation
, kernel_size
and stride
parameters. The relationship between these can be expressed as
Complexity
Number of filters
In order to calculate the number of operations performed this module, it is necessary to understand the impact of the groups
parameter on the overall complexity, and the number of filters \(\psi\) a network instance will have based on this. According to the official torch.nn.Conv1d
documentation
groups
controls the connections between inputs and outputs.in_channels
andout_channels
must both be divisible bygroups
.For example: At
groups=1
, all inputs are convolved to all outputs. Atgroups=2
, the operation becomes equivalent to having two conv layers side by side, each seeing half the input channels and producing half the output channels, and both subsequently concatenated.At
groups=in_channels
, each input channel is convolved with its own set of filters (of size \(\frac{\text{out\_channels}}{\text{in\_channels}}\) )
Based on this information, the number of filters \(\psi\) can be computed as
Operations per filter
Now the number of filters \(\psi\) are known, it is necessary to compute how many operations each filter performs. As shown in Figure 1, for each kernel position there will be \(\text{kernel\_size}\) multiplications (i.e. each kernel element multiplied by a slice of the input tensor of the same size) and \(\text{kernel\_size}-1\) additions to aggregate the result and obtain one element of the output.
Since each element in \(L_\text{out}\) is the result of the operations carried out for a single kernel position, the number of operations per filter \(\lambda\) can be expressed as
Note
Please note that the batch size \(N\) will be ignored for now, but it will be included later on.
Filter aggregation
Now that the number of filters and the number of operations per filter are known, it is necessary compute the operations needed to aggregate each group of filters \(\gamma\) to produce each output channel \(C_\text{out}\). These operations correspond to simple element-wise additions and can be expressed as
Where the term \(\left(\frac{C_{\text{in}}}{\text{groups}}-1\right)\) corresponds to the number of grouped connections between input and outputs channels \(\frac{C_{\text{in}}}{\text{groups}}\), subtracted by \(1\) because the operation is an addition. The \(L_\text{out}\) factor accounts for the number of elements per filters, and \(C_{\text{out}}\) expands this calculation to all output channels. Finally, the remaining \(+1\) corresponds to the bias term \(b\) that was not included so far, and that is added to each resulting output channel element. Note that this last term is only added if the module is instantiated using bias=True
.
Note
Please note that the bias term \(b\) was not included in Operations per filter and is added here instead. Even though according to PyTorch torch.nn.Conv1d
documentation \(b\) has shape \(\left(C_\text{out}\right)\), in practice this tensor is implicitly broadcasted following PyTorch broadcasting semantics in such a way that each tensor value will be added with its corresponding channel bias.
Total operations
Now putting together all different factors that contribute to the total number of operations as well as including the batch size \(N\)
Where
- \(N\) is the batch size.
- \(\psi\) is the number of filters.
- \(\lambda\) is the number of operations per filter.
- \(\gamma\) is the number of filter aggregation operations.
For the case of bias=True
this can be expanded to
Rearranging terms it can be simplified to
For the case of bias=False
\(\gamma=C_{\text{out}}\times L_\text{out}\times\left(\frac{C_{\text{in}}}{\text{groups}}-1\right)\) and the whole expression can be simplified to
Summary
The number of operations performed by a torch.nn.Conv1d
module can be estimated as
\(\large{\text{Conv1d}_{ops}=N\times\left(\frac{C_{\text{in}}\times C_{\text{out}}\times L_{\text{out}}\times2\times\text{kernel\_size}}{\text{groups}}\right)}\)
\(\large{\text{Conv1d}_{ops}=N\times\left(\frac{C_{\text{out}}\times L_{\text{out}}\times\left( C_\text{in}\times2\times\text{kernel\_size} - \text{groups}\right)}{\text{groups}}\right)}\)
Where
- \(N\) is the batch size.
- \(C_{\text{in}}\) is the number of input channels.
- \(C_{\text{out}}\) is the number of output channels.
- \(L_{\text{out}}\) is the length of the output tensor (i.e.
y.size(-1)
assuming an output tensory
). - \(\text{kernel\_size}\) is the length of the kernel.
- \(\text{groups}\) is the number of groups.