Keras
TensorFlow
Deep Learning
Neural Networks
Model Input

How to properly feed specific tensor to keras model

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Feeding a specific tensor to a Keras model is mostly about matching the model's declared inputs exactly: the right shape, dtype, and input mapping. Problems usually happen when developers confuse a symbolic KerasTensor from model construction with a real runtime tensor, or when a multi-input model receives its inputs in the wrong order. Once you distinguish those cases, the feeding rules are straightforward.

Single-Input Model: Pass A Real Tensor

If the model has one input, feed a real tensor with the expected batch shape.

python
1import tensorflow as tf
2
3model = tf.keras.Sequential([
4    tf.keras.layers.Input(shape=(4,)),
5    tf.keras.layers.Dense(8, activation="relu"),
6    tf.keras.layers.Dense(1),
7])
8
9x = tf.constant([[1.0, 2.0, 3.0, 4.0]], dtype=tf.float32)
10y = model(x)
11print(y)

The important detail is that x is a real runtime tensor, not the symbolic input created during model definition.

Symbolic Tensor Versus Runtime Tensor

During model construction, Keras creates symbolic tensors such as:

python
inputs = tf.keras.Input(shape=(4,))

That object is part of the model graph definition. It is not the data you feed at inference or training time.

At runtime, you pass actual tensors such as:

python
x = tf.random.normal((32, 4))
model(x)

A lot of confusion comes from trying to "feed the input tensor" when the object in hand is really just the symbolic placeholder used to build the model.

Multi-Input Model: Match By Order Or By Name

For multi-input models, you can feed data either by list order or by input names.

python
1import tensorflow as tf
2
3image_input = tf.keras.Input(shape=(32, 32, 3), name="image")
4meta_input = tf.keras.Input(shape=(5,), name="meta")
5
6x1 = tf.keras.layers.Flatten()(image_input)
7x2 = tf.keras.layers.Dense(8, activation="relu")(meta_input)
8combined = tf.keras.layers.Concatenate()([x1, x2])
9outputs = tf.keras.layers.Dense(1)(combined)
10
11model = tf.keras.Model(inputs=[image_input, meta_input], outputs=outputs)
12
13image_tensor = tf.random.normal((2, 32, 32, 3))
14meta_tensor = tf.random.normal((2, 5))
15
16result = model([image_tensor, meta_tensor])
17print(result.shape)

This works because the list order matches the model input order.

A safer version for complex models is named input mapping:

python
1result = model({
2    "image": image_tensor,
3    "meta": meta_tensor,
4})

That avoids subtle bugs when the input order is easy to mix up.

Feeding Specific Inputs In fit

The same mapping rules apply during training.

python
1labels = tf.random.normal((2, 1))
2
3model.compile(optimizer="adam", loss="mse")
4model.fit(
5    {"image": image_tensor, "meta": meta_tensor},
6    labels,
7    epochs=1,
8)

If a model has multiple inputs, a dict keyed by input name is often the clearest interface.

Inspect The Model's Expected Inputs

When feeding fails, inspect what the model expects.

python
1print(model.input_shape)
2print(model.inputs)
3for t in model.inputs:
4    print(t.name, t.shape, t.dtype)

This tells you:

  • how many inputs exist
  • their names
  • their shapes
  • their dtypes

Most feeding mistakes are obvious once you print this information.

Common Shape Mistakes

A very common error is forgetting the batch dimension.

If the model expects shape (None, 4), this is wrong:

python
x = tf.constant([1.0, 2.0, 3.0, 4.0])

This is correct:

python
x = tf.constant([[1.0, 2.0, 3.0, 4.0]])

The extra outer dimension is the batch dimension, even for one example.

Feeding A Specific Intermediate Tensor

If you want to start from an intermediate layer instead of the model's declared input, the usual solution is not to force-feed a hidden tensor into the original model. Instead, build a new submodel whose inputs and outputs match the part you actually want to run.

That keeps the graph explicit and avoids abusing internals.

Common Pitfalls

  • Confusing symbolic tf.keras.Input(...) tensors with real runtime data tensors.
  • Feeding multi-input models with the wrong list order.
  • Forgetting the batch dimension on otherwise correct sample shapes.
  • Ignoring dtype mismatches such as feeding integers where the model expects float32.
  • Trying to inject data into an intermediate layer instead of defining a proper submodel for that path.

Summary

  • Feed Keras models with real runtime tensors, not symbolic build-time tensors.
  • For single-input models, pass a tensor with the expected batch shape.
  • For multi-input models, use either the correct list order or a dict keyed by input names.
  • Inspect model.inputs and model.input_shape when debugging feed errors.
  • If you need to start from an intermediate point, define a submodel instead of forcing data into hidden internals.

Course illustration
Course illustration

All Rights Reserved.