Keras
deep learning
constant input
machine learning
neural networks

How to give a constant input to 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.

Practice ML system design

Introduction

In Keras, a true constant usually should not be modeled as a normal input coming from the outside world. If the value never changes, the cleaner design is to keep it inside the model graph as a constant tensor, a non-trainable weight, or a custom layer. Keras can also wrap an existing tensor as an input, but that is a more specialized pattern.

First Decide What "Constant Input" Means

People use the phrase "constant input" for several different needs:

  • A fixed tensor that should be combined with every batch
  • A non-trainable parameter stored inside a layer
  • A default value used when no external input is provided
  • A second model input whose value happens to be the same for all samples

The best Keras solution depends on which of those you actually mean.

Fixed Constant Inside the Model

If the value is truly constant, keep it inside the layer logic. Here is a custom layer that appends the same two constant features to every example in the batch.

python
1import tensorflow as tf
2
3class AppendConstant(tf.keras.layers.Layer):
4    def __init__(self, values, **kwargs):
5        super().__init__(**kwargs)
6        self.values = tf.constant(values, dtype=tf.float32)
7
8    def call(self, inputs):
9        batch_size = tf.shape(inputs)[0]
10        constant_part = tf.broadcast_to(self.values, [batch_size, tf.shape(self.values)[0]])
11        return tf.concat([inputs, constant_part], axis=1)
12
13
14inputs = tf.keras.Input(shape=(3,))
15x = AppendConstant([0.5, 1.0])(inputs)
16outputs = tf.keras.layers.Dense(1)(x)
17
18model = tf.keras.Model(inputs, outputs)
19model.summary()

This is usually the clearest answer when the constant belongs to the model itself rather than to the training data.

Using a Non-Trainable Weight

If the "constant" is model state that should be saved with the model but not updated during training, you can store it as a non-trainable weight.

python
1import tensorflow as tf
2
3class ScaleByFixedFactor(tf.keras.layers.Layer):
4    def build(self, input_shape):
5        self.factor = self.add_weight(
6            name="factor",
7            shape=(),
8            initializer=tf.keras.initializers.Constant(2.0),
9            trainable=False,
10        )
11
12    def call(self, inputs):
13        return inputs * self.factor
14
15
16inputs = tf.keras.Input(shape=(2,))
17outputs = ScaleByFixedFactor()(inputs)
18model = tf.keras.Model(inputs, outputs)

This pattern is good when the fixed value is part of layer configuration and should travel with saved weights.

Wrapping an Existing Tensor with keras.Input

Keras also supports wrapping an existing tensor as an input through the tensor argument of keras.Input. This is an advanced option and is mainly useful when you already have a TensorFlow tensor that should enter the Functional API graph.

python
1import tensorflow as tf
2
3constant_tensor = tf.constant([[1.0, 2.0, 3.0]])
4constant_input = tf.keras.Input(tensor=constant_tensor)
5outputs = tf.keras.layers.Dense(1)(constant_input)
6model = tf.keras.Model(constant_input, outputs)

This works, but it is less common in everyday model-building code. If the tensor never changes, a custom layer is often easier to understand and maintain.

When a Repeated External Input Is Better

Sometimes the value is constant only for one experiment or one dataset, but it conceptually belongs to the data rather than the model. In that case, treating it as a normal second input may be cleaner.

python
1import numpy as np
2import tensorflow as tf
3
4features = tf.keras.Input(shape=(3,), name="features")
5bias_input = tf.keras.Input(shape=(1,), name="bias_input")
6
7x = tf.keras.layers.Concatenate()([features, bias_input])
8outputs = tf.keras.layers.Dense(1)(x)
9
10model = tf.keras.Model([features, bias_input], outputs)
11
12x_train = np.random.rand(4, 3).astype("float32")
13bias_train = np.ones((4, 1), dtype="float32") * 0.25
14y_train = np.random.rand(4, 1).astype("float32")
15
16model.compile(optimizer="adam", loss="mse")
17model.fit({"features": x_train, "bias_input": bias_train}, y_train, epochs=1, verbose=0)

This is not a true constant inside the graph, but it is often the simplest design if the value belongs to the dataset.

Choosing the Right Pattern

Use an internal constant when the value is part of model logic. Use a non-trainable weight when the value should be stored with model state. Use keras.Input(tensor=...) when you already have a tensor and need to integrate it into the graph. Use a normal repeated input when the value conceptually belongs to the data.

That distinction prevents a lot of awkward Keras code.

Common Pitfalls

One common mistake is treating a fixed constant like a regular training input even though it never changes. That complicates data pipelines for no real benefit.

Another issue is confusing constant inputs with constant initializers. Initializing weights to a fixed value does not mean the model receives a fixed input during the forward pass.

Developers also sometimes store a value as a trainable weight by accident. If the value must stay fixed, mark it as trainable=False or keep it as a plain constant tensor.

Finally, be cautious with keras.Input(tensor=...). It is powerful, but it is more specialized than the standard Functional API flow and may be harder for teammates to understand at a glance.

Summary

  • If the value truly never changes, keep it inside the Keras model instead of passing it as normal input data.
  • Use a custom layer with tf.constant for fixed graph values.
  • Use a non-trainable weight when the constant should be saved with model state.
  • Use keras.Input(tensor=...) only when you already have an existing tensor to wrap.
  • If the value belongs to the dataset, pass it as a normal repeated input instead of hiding it inside the model.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Practice ML system design

All Rights Reserved.