Deep Learning model with Different data types in 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.
Introduction
Keras can handle different kinds of inputs in the same model, but you usually do not solve that by forcing every feature into one raw array. The practical approach is to build a multi-input model where each branch uses the right dtype and preprocessing steps before the features are combined.
Why Different Data Types Need Different Treatment
Numerical columns, integer categories, text tokens, and images do not mean the same thing to a neural network. A Dense layer can consume floating-point tensors directly, but strings and categories must be encoded first, and images usually need convolutional layers or at least normalization.
That is why the Keras Functional API is a much better fit than the Sequential API for mixed-input problems. It lets you create a separate input path for each data type and join them later.
Example: Numeric and Categorical Inputs Together
Here is a small model that mixes floating-point features with a string category.
The important part is that each input keeps a dtype that matches its meaning until preprocessing converts it into a numeric representation the network can learn from.
Example: Adding an Image Branch
If one of the inputs is an image, give it its own branch.
This pattern is common in recommendation, fraud detection, medical imaging, and tabular-plus-image systems.
Dtype Versus Modality
It helps to separate two ideas:
- dtype means the tensor storage type, such as
float32,int32, orstring - modality means the kind of data, such as image, text, category, or continuous numeric feature
Different modalities often imply different model branches, while dtypes mostly affect how preprocessing layers interpret the values.
Preprocessing Layers Matter
Modern Keras makes mixed data much easier because preprocessing layers can live directly in the model graph. Some common examples are:
- '
Normalizationfor continuous numeric features' - '
StringLookupfor string categories' - '
IntegerLookupfor integer categories' - '
TextVectorizationfor text' - '
Rescalingfor images'
Keeping preprocessing near the model reduces training-serving skew because the same transformations can run both during training and inference.
Why One Big Dense Input Is Usually Wrong
Beginners often try to force everything into one 2D numeric matrix before modeling. That can work for small classical machine-learning pipelines, but it throws away structure. Images stop looking like images, categorical strings lose their vocabulary handling, and text becomes awkward unless you manually encode everything beforehand.
Keras is most expressive when you preserve input meaning for as long as possible.
Common Pitfalls
The first pitfall is feeding a string or categorical tensor directly into a Dense layer. Dense layers expect numeric tensors, so you must encode strings and categories first.
Another pitfall is forgetting to call adapt() on lookup or normalization layers. Without adaptation, those layers do not know the vocabulary or scaling statistics they need.
A third pitfall is letting pandas object columns leak into the model pipeline without explicit conversion. Object dtype is a warning sign that your preprocessing needs cleanup.
Finally, keep output shapes consistent. When you feed a dictionary of inputs into model.fit, the keys and tensor shapes must match the named Input layers exactly.
Summary
- Mixed data types in Keras are best handled with the Functional API and separate input branches
- Each input should keep the right dtype until preprocessing converts it to learnable numeric features
- Use preprocessing layers such as
StringLookup,Normalization, andRescaling - Combine branches only after each data type has been encoded appropriately
- Avoid flattening every feature into one raw matrix unless you are intentionally simplifying the problem
Related reading
- Deep learning Udacity course Prob 2 assignment 1 notMNIST
- Deep neural network skip connection implemented as summation vs concatenation?
- deeplearning4j - using an RNN/LSTM for audio signal processing
- Default Adam optimizer doesn't work in tf.keras but string adam does
- Deleting all but a few nodes in TensorFlow graph
- ''Dense'' object has no attribute ''op''
- Deep Q Network is not learning
- Denormalization of predicted data in neural networks
.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.