What is the computational complexity of the forward pass of a convolutional neural network?

How do I determine the computational complexity (big-O notation) of the forward pass of a convolutional neural network?

Let's assume for simplicity that we use zero-padding such that the input size and the output size are the same.

• "assume for simplicity that we use zero-padding such that the input size and the output size are the same." so you are assuming zero-filter as well? Aug 8 '20 at 19:54
• @k.c.sayz'k.csayz' very funny! Anyway I assume the discussion about filters is going to be part of the answer. Aug 9 '20 at 6:26

What is the time complexity?

The time complexity of an algorithm is the number of basic operations, such as multiplications and summations, that the algorithm performs. The time complexity is usually expressed as a function of the input's size $$n$$ (but this does not always have to be the case: for instance, you can express the time complexity as a function of the output's size).

Example

Rather than giving you a full answer to your question, I will try to help you by explaining, with the simplest example, how you should calculate the time complexity.

For simplicity, let's assume that we have a kernel $$\mathbf{H} \in \mathbb{R}^{3 \times 3}$$ and input image $$\mathbf{I} \in \mathbb{R}^{3 \times 3}$$ (i.e. the kernel has the same dimensions as the input), we use a stride of $$1$$ and no padding. If we convolve $$\mathbf{I}$$ with $$\mathbf{H}$$, how many operations will we perform? The convolution is defined as a scalar product, so it is composed of multiplications and summations, so we need to count both of them. We have $$9$$ multiplications and $$8$$ summations, for a total of $$17$$ operations.

\begin{align} \mathbf{I} \circledast \mathbf{H} &= \begin{bmatrix} i_{11} & i_{12} & i_{13} \\ i_{21} & i_{22} & i_{23} \\ i_{31} & i_{32} & i_{33} \end{bmatrix} \odot \begin{bmatrix} h_{11} & h_{12} & h_{13} \\ h_{21} & h_{22} & h_{23} \\ h_{31} & h_{32} & h_{33} \end{bmatrix}\\ &= \sum_{ij} \begin{bmatrix} i_{11} h_{11} & i_{12} h_{12} & i_{13} h_{13} \\ i_{21} h_{21} & i_{22} h_{22} & i_{23} h_{23} \\ i_{31} h_{31} & i_{32} h_{32} & i_{33} h_{33} \end{bmatrix}\\ &= i_{11} h_{11} + i_{12} h_{12} + i_{13} h_{13} + i_{21} h_{21} + i_{22} h_{22} + i_{23} h_{23} + i_{31} h_{31} + i_{32} h_{32} + i_{33} h_{33} \end{align}

Time complexity

What is the time complexity of this convolution? To answer this question, you first need to know the input's size, $$n$$. The input contains $$9$$ elements, so its size is $$n = 9$$. How many operations did we perform with respect to the input's size? We performed $$17$$ operations, so the time complexity $$\mathcal{O}(2*n) = \mathcal{O}(n)$$, i.e. this operation is linear. If you are not familiar with the big-O notation, I suggest that you get familiar with it, otherwise, you will not understand anything about computational complexity.

To calculate the time complexity in the case the input's dimensions are different than the kernel's dimensions, you will need to calculate the number of times you slide the kernel over the input. You can't ignore this (as I ignored the constant $$2$$ above) because the number of times you slide the kernel over the input depends on the input's size, so it's a function of the input. Anyway, the paper A guide to convolution arithmetic for deep learning contains a lot of information about convolution arithmetic, so it will be helpful.

Non-linearities, pooling and fully connected layers

Note that, in the above example, I ignored the non-linearities and pooling layer. You can easily extend my reasoning to include these operations too. I also ignored the operations in the final fully connected layers. You can find how to calculate the number of operations in an MLP in this answer. If you also perform other operations or have other layers other than convolutional, pooling and fully connected, of course, you will also need to take them into account.

Forward pass

Moreover, the time complexity of the forward pass of a CNN depends on all these operations in these different layers, so you need to compute the number of operations in each layer first. However, once you know how to compute the number of operations for one convolutional layer, one pooling layer, and one fully connected layer, you can easily compute the number of operations for the other convolutional, pooling, and fully connected layers. Then you just need to sum all these operations and express your time complexity as a function of the input (and probably number of layers).

Space complexity

If you also want to compute the space complexity, you just need to do the same thing, but as a function of the space that you use, i.e. how many variables you use to perform the convolution.
{"ft_lang_label":"__label__en","ft_lang_prob":0.8792714,"math_prob":0.99857867,"size":4754,"snap":"2022-05-2022-21","text_gpt3_token_len":1215,"char_repetition_ratio":0.1431579,"word_repetition_ratio":0.125,"special_character_ratio":0.28291965,"punctuation_ratio":0.084415585,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9999194,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-01-26T17:47:43Z\",\"WARC-Record-ID\":\"<urn:uuid:2489d60a-6c73-4d20-bb5e-26032ec9adc4>\",\"Content-Length\":\"144481\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:b8af5a6d-0265-491e-9aae-d82edd405209>\",\"WARC-Concurrent-To\":\"<urn:uuid:048852c9-c3b5-40a9-9987-cd36919009a1>\",\"WARC-IP-Address\":\"151.101.129.69\",\"WARC-Target-URI\":\"https://ai.stackexchange.com/questions/22913/what-is-the-computational-complexity-of-the-forward-pass-of-a-convolutional-neur\",\"WARC-Payload-Digest\":\"sha1:VATEYZXAWH7RQPTR6GS2WKV46RY4GIRE\",\"WARC-Block-Digest\":\"sha1:QEZOAGUAP67R666I2LCFONV6RPJQY6AO\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-05/CC-MAIN-2022-05_segments_1642320304959.80_warc_CC-MAIN-20220126162115-20220126192115-00413.warc.gz\"}"}