Memory usage of neural network, Keras
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Understanding Memory Usage of Neural Networks with Keras
The memory consumption of neural networks during training and inference is a critical aspect that affects performance, scalability, and the feasibility of deploying models on various hardware platforms. In this article, we delve into how memory is utilized by neural networks implemented using Keras, a popular high-level deep learning framework. We will examine how model parameters, intermediate data, batch sizes, and memory optimization techniques play a role in the overall memory usage.
Key Factors Affecting Memory Usage
- Model Parameters: • The primary consumers of memory are the model parameters or weights. Each parameter, usually stored as a 32-bit floating-point number, requires approximately 4 bytes. For a network with millions of parameters, the memory cost can be substantial.
- Activations: • During the forward pass, neural networks store intermediate activations needed for backpropagation. These activations can occupy a large portion of the memory, especially in deep architectures.
- Gradients: • The gradients computed for each parameter during backpropagation also require memory. Like weights and activations, gradients are usually stored as 32-bit floats.
- Batch Size: • The batch size determines how many examples are processed simultaneously. This impacts memory usage since more data is being loaded into memory at once.
- Model Architecture: • Different layers have varying memory consumptions — convolutional layers, for instance, generally require more memory than fully connected layers due to the way activations are convolved and stored.
Memory Usage Calculation Example
Consider a simple neural network with one fully connected layer, a single hidden layer, and an output layer. Let’s calculate the memory usage:
• Input shape: (batch_size, 256) • Hidden layer: 128 neurons • Output layer: 10 neurons • Weights and Biases: • First Layer: • Second Layer: • Output Layer: • Activations: • Calculated similarly and depend on batch size. • Trains models by trading compute for memory. Intermediate activations are discarded and recomputed during backpropagation to save memory. • Using mixed precision (e.g., `float16` instead of `float32`) to reduce memory usage. TensorFlow and Keras support this through the `tf.keras.mixed_precision` API. • Allows memory for activations to be reused after they are no longer needed in backpropagation. • Techniques to reduce the model size and hence, memory footprint. Pruning removes redundant weights, while quantization reduces the precision of the weights. • Reducing batch size can aid in fitting models into memory, albeit at the potential cost of slower convergence rates.
Related reading
- Merge 2 sequential models in Keras
- metric learning and contrastive learning difference
- Minimal `RNN` example in tensorflow
- MinimumPooling in Keras
- MemoryError in TensorFlow; and successful NUMA node read from SysFS had negative value -1 with xen
- Merge multiple BatchEncoding or create tensorflow dataset from list of BatchEncoding objects
- MemoryError Unable to allocate MiB for an array with shape and data type, when using anymodel.fit in sklearn
- Merging approximately equal points in dataset

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.