# What is the memory cost of a CNN?

I was recently thinking about the memory cost of (a) training a CNN and (b) inference with a CNN. Please note, that I am not talking about the storage (which is simply the number of parameters).

How much memory does a given CNN (e.g. VGG-16 D) need for

• (b) Inference on a single image?

## My thoughts

Basically, I want to make sure that I didn't forget anything with this question. If you have other sources which explain this kind of thought, please share them with me.

### (a) Training

For training with ADAM, I will now assume that I have a Mini-batch size of $$B \in \mathbb{N}$$ and $$w \in \mathbb{N}$$ is the number of parameters of the CNN. Then the memory footprint (the maximum amount of memory I need at any point while training) for a single training pass is:

• $$2w$$: Keep the weights and the weight updates in memory
• $$B \cdot$$ Size of all generated feature maps (forward pass)
• $$w$$: Gradients for each weight (backpropagation)
• $$w$$: Learning rates for each weight (ADAM)

### (b) Inference

In inference, it is not necessary to store a feature map of layer $$i-1$$ if the feature maps of layer $$i$$ are already calculated. So the memory footprint while inference is:

• $$w$$: The model
• The two most expensive successive layers (one which is already calculated, the net one which gets calculated)
• I don't know if it can help, but this post also attempt to consider complexity of a NN: datascience.stackexchange.com/questions/10692/… Jan 13 '17 at 13:17
• Here is a good article for you: Why is so much memory needed for deep neural networks? Hope it helps Mar 29 '17 at 14:57
• Dec 28 '19 at 13:23
• @MartinThoma, during inference, is ur proposed memory requirement is correct? Do we only need memory equivalent to model + two most expensive successive layers? Apr 29 '20 at 1:39