Keras 2D input to 2D output
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
The phrase "2D input to 2D output" can mean two different things in Keras. Sometimes it means a standard tabular tensor shaped like (batch, features) mapped to (batch, targets). Other times it means an image-like grid shaped like (height, width) or (height, width, channels) mapped to another 2D grid.
The architecture depends on which meaning you have in mind. Dense layers are a natural fit for vector-style 2D tensors, while convolutional layers are the usual choice for image-to-image problems.
Case 1: Matrix-Like Input and Matrix-Like Output
If each sample is just a feature vector and each prediction is another vector, Keras already handles this naturally with dense layers.
Input shape here is (batch_size, 8) and output shape is (batch_size, 4). That is already "2D to 2D" in the linear algebra sense.
Use this pattern when the spatial relationship between features does not matter.
Case 2: Image-Like 2D Input to Image-Like 2D Output
If each sample is a 2D grid and you want another 2D grid out, a convolutional model is usually the correct answer. For example, an image denoiser or segmentation head often preserves height and width.
This model maps a 64 x 64 x 1 input to a 64 x 64 x 1 output. The spatial dimensions stay the same because the convolutions use padding="same".
Why Flattening Changes the Problem
You could flatten a 2D image into a vector, pass it through dense layers, and reshape it back:
This works, but it throws away local spatial structure during the dense part of the network. For real image-like tasks, convolutional layers usually learn more efficiently because they preserve neighborhood information.
Choosing the Right Output Layer
The final layer depends on the task:
- regression-style grid output often uses a linear final layer
- binary mask output often uses
sigmoid - multi-class per-pixel output often uses
softmaxover channels
So "2D output" is not enough information by itself. You also need to know whether the output represents continuous values, one class per location, or something else.
Common Pitfalls
- Confusing
(batch, features)with spatial 2D image data. Both are "2D" in a shape listing, but they imply different architectures. - Using dense layers for image-to-image tasks that would be better modeled with convolutions.
- Forgetting the channel dimension on image-like data. Keras often expects
(height, width, channels). - Changing the spatial size accidentally by using pooling or convolutions without appropriate padding.
- Picking the wrong final activation for the target type.
Summary
- "2D input to 2D output" can mean vector-to-vector or grid-to-grid.
- Dense layers are appropriate for ordinary feature vectors.
- Convolutional layers are usually the right choice for image-like 2D data.
- Preserve spatial dimensions with
padding="same"when the output should match the input size. - Always choose the model shape and final activation based on the structure of the target, not just the number of tensor dimensions.
Related reading
- Keras / tensorflow - limit number of cores intra_op_parallelism_threads not working
- Keras / Tensorflow Predict Using tf.data.Dataset API
- Keras / Tensorflow Weird dropout behaviour
- Keras accuracy does not change
- Keras and Error Setting an array element with a sequence
- Keras AttributeError 'list' object has no attribute 'ndim
- Keras and TensorBoard - AttributeError 'Sequential' object has no attribute '_get_distribution_strategy
- keras AssertionError Duplicate registrations for type 'experimentalOptimizer
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free 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.