Initializing tensorflow Variable with an array larger than 2GB
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
TensorFlow's protocol buffer serialization has a hard 2GB size limit per tensor. When you try to create a tf.Variable or tf.constant from a NumPy array larger than 2GB, TensorFlow raises ValueError: Cannot create a tensor proto whose content is larger than 2GB. The fix is to use tf.Variable with an initializer function, load the data in chunks via variable.assign(), use tf.data.Dataset, or store the data in a file and read it at runtime.
The Problem
The limit comes from Protocol Buffers (protobuf), which TensorFlow uses internally for tensor serialization. Protobuf messages cannot exceed 2GB.
Fix 1: Use an Initializer Function
Instead of passing the array directly, pass a callable that returns the data:
Fix 2: Assign in Chunks
Create the variable with a placeholder shape, then assign the data in pieces:
Fix 3: Use tf.data.Dataset
For training data that does not need to be a single variable, load it via tf.data:
Fix 4: Load from File at Runtime
Store large arrays in NumPy, HDF5, or TFRecord files:
Fix 5: TF1 — Use tf.placeholder with feed_dict
In TensorFlow 1.x, use placeholders and feed the data:
Large Embedding Tables
The most common use case for >2GB variables is embedding tables in recommendation models:
Common Pitfalls
- Trying to serialize the entire array at once:
tf.constant(large_array)serializes the array through protobuf, hitting the 2GB limit. Always chunk arrays larger than ~500M elements (2GB / 4 bytes per float32) when creating tensors. - Forgetting memory-mapped loading:
np.load("file.npy")loads the entire file into RAM. For very large files, usenp.load("file.npy", mmap_mode="r")to memory-map it, then load chunks into TensorFlow incrementally. - Creating tf.constant in a loop without clearing: Each
tf.constant(chunk)creates a graph node in TF1 or a tensor in TF2. Creating thousands of constants in a loop wastes memory. Assign directly to variable slices instead of accumulating constants. - Not using float16 to halve the size: If full float32 precision is not needed, converting to float16 halves the memory and may bring the tensor under 2GB. Use
large_array.astype(np.float16)andtf.float16. - Ignoring the 2GB limit in SavedModel: Even if you initialize a variable with chunks, saving with
tf.saved_model.save()serializes each variable into protobuf, hitting the limit again. Usetf.train.Checkpointinstead, which stores variables in a chunked format without the protobuf size limit.
Summary
- TensorFlow cannot create a single tensor proto larger than 2GB (protobuf limitation)
- Initialize large variables with a callable (
lambda) or assign data in chunks viavar[i:j].assign() - Use
tf.data.Datasetwith generators for large training data that does not need to be a single tensor - Store large arrays in
.npyfiles and load with memory mapping (mmap_mode="r") - Use
tf.train.Checkpointinstead ofSavedModelto save models with large variables - Consider float16 to halve memory usage and potentially stay under the 2GB limit
Related reading
- Input 0 is incompatible with layer flatten_2 expected min_ndim3, found ndim2
- Input 0 of layer lstm_5 is incompatible with the layer expected ndim3, found ndim2
- Input Permutations in Feed-Forward Neural Networks
- Input to LSTM network tensorflow
- Input 0 of layer conv1d is incompatible with the layer expected min_ndim3, found ndim2. Full shape received None, 30
- Input images with dynamic dimensions in Tensorflow-lite
- Inline instantiation of a constant List
- Inline list initialization in VB.NET

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.