Creating many feature columns in Tensorflow
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 feature columns (tf.feature_column) bridge raw tabular data and model input layers by defining how each feature should be transformed — numeric columns pass through directly, categorical columns are encoded as one-hot or embeddings, and crossed columns capture feature interactions. When a dataset has many features, you can programmatically generate feature column lists from schema metadata instead of defining each one manually. Note that tf.feature_column is part of TF 1.x/2.x Estimator API; for modern TF 2.x Keras models, use tf.keras.layers preprocessing layers instead.
Numeric Columns
numeric_column handles continuous values. Use normalizer_fn to standardize features during input processing.
Categorical Columns
Categorical columns must be wrapped in indicator_column (one-hot) or embedding_column (dense vector) before feeding into dense layers.
Generating Columns Programmatically
For datasets with dozens or hundreds of features, programmatic generation from schema metadata is essential.
Bucketized and Crossed Columns
Bucketized columns convert continuous values to categorical ranges. Crossed columns combine multiple categorical features to learn interactions.
Using with Estimators
Modern Alternative: Keras Preprocessing Layers
Keras preprocessing layers are the modern replacement for tf.feature_column. They integrate directly into the model graph and support TF Serving.
Common Pitfalls
- Forgetting to wrap categorical columns: Raw
categorical_column_with_*columns cannot be used directly in dense layers. Wrap them inindicator_column(one-hot) orembedding_column(dense vector). - Hash bucket collisions:
categorical_column_with_hash_bucketmaps different values to the same bucket whenhash_bucket_sizeis too small. Use a size at least 2-5x the number of unique values. - Embedding dimension too large: A common rule is
dimension = min(cardinality // 2, 50). An oversized embedding wastes parameters and memory without improving accuracy. - Mixing feature_column and Keras layers:
tf.feature_columnis designed for Estimators. Whiletf.keras.layers.DenseFeaturesbridges them into Keras, the cleaner approach is to use Keras preprocessing layers directly. - Not normalizing numeric features: Numeric features with different scales (age 0-100, income 0-1M) cause training instability. Use
normalizer_fninnumeric_columnor KerasNormalizationlayer.
Summary
tf.feature_columnprovidesnumeric_column,categorical_column_with_*,bucketized_column, andcrossed_column- Categorical columns must be wrapped in
indicator_columnorembedding_columnfor dense layers - Generate feature columns programmatically from DataFrame schema for large datasets
- Use
crossed_columnto capture feature interactions - For modern TF 2.x code, prefer Keras preprocessing layers (
Normalization,StringLookup,Embedding) overtf.feature_column - Always normalize numeric features to avoid training instability
Related reading
- CRITICAL tensorflowCategory has no images - validation
- Cross Validation in Keras
- CrossEntropyLoss in PyTorch
- Cuda 12 tf-nightly 2.12 Could not find cuda drivers on your machine, GPU will not be used, while every checking is fine and in torch it works
- CRITICAL tensorflowCategory has no images - validation
- Crop image to bounding box in Tensorflow Object Detection API
- Creating training data for a Maxent classfier in Java
- Cross-validation and parameters tuning with XGBoost and hyperopt
.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.