How can Keras be used to create a model where the input shape of model is specified in advance?

Keras is a high-level deep learning API that runs on top of TensorFlow. When building neural networks, layers need to know the input shape to initialize their weights properly. Keras provides flexible ways to specify input shapes in advance.

Understanding Layer Weight Initialization

Keras layers create weights only when they know the input shape. This happens either when data is first passed through the layer or when the input shape is explicitly specified ?

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers

print("Creating a dense layer without specifying input shape")
layer = layers.Dense(3)
print("Initial weights:", layer.weights)

# Call the layer with test data
print("\nCalling layer with test data")
x = tf.ones((2, 3))
y = layer(x)
print("Weights after first call:")
for weight in layer.weights:
    print(f"Shape: {weight.shape}, Name: {weight.name}")
Creating a dense layer without specifying input shape
Initial weights: []

Calling layer with test data
Weights after first call:
Shape: (3, 3), Name: dense/kernel:0
Shape: (3,), Name: dense/bias:0

Method 1: Using input_shape Parameter

You can specify the input shape when creating the layer to initialize weights immediately ?

import tensorflow as tf
from tensorflow.keras import layers

# Create layer with specified input shape
layer_with_shape = layers.Dense(3, input_shape=(4,))
print("Layer created with input_shape=(4,)")
print("Weights immediately available:")
for weight in layer_with_shape.weights:
    print(f"Shape: {weight.shape}, Name: {weight.name}")

# Test with data
test_input = tf.ones((2, 4))
output = layer_with_shape(test_input)
print(f"\nOutput shape: {output.shape}")
Layer created with input_shape=(4,)
Weights immediately available:
Shape: (4, 3), Name: dense_1/kernel:0
Shape: (3,), Name: dense_1/bias:0

Output shape: (2, 3)

Method 2: Using build() Method

You can manually build the layer by calling build() with the input shape ?

import tensorflow as tf
from tensorflow.keras import layers

# Create layer and build it manually
layer = layers.Dense(5)
layer.build((None, 10))  # None for batch dimension, 10 for input features

print("Layer built with input shape (None, 10)")
print("Available weights:")
for weight in layer.weights:
    print(f"Shape: {weight.shape}, Name: {weight.name}")

# Verify it works with appropriate input
test_input = tf.random.normal((3, 10))
output = layer(test_input)
print(f"\nInput shape: {test_input.shape}")
print(f"Output shape: {output.shape}")
Layer built with input shape (None, 10)
Available weights:
Shape: (10, 5), Name: dense_2/kernel:0
Shape: (5,), Name: dense_2/bias:0

Input shape: (3, 10)
Output shape: (3, 5)

Method 3: Using Sequential Model with Input Layer

For complete models, use an Input layer to specify the input shape ?

import tensorflow as tf
from tensorflow.keras import layers, Sequential

# Create model with explicit Input layer
model = Sequential([
    layers.Input(shape=(8,)),
    layers.Dense(16, activation='relu'),
    layers.Dense(8, activation='relu'), 
    layers.Dense(1, activation='sigmoid')
])

print("Model created with Input layer")
print("Model summary:")
model.summary()
Model created with Input layer
Model summary:
Model: "sequential"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 dense_3 (Dense)             (None, 16)                144       
                                                                 
 dense_4 (Dense)             (None, 8)                 136       
                                                                 
 dense_5 (Dense)             (None, 1)                 9         
                                                                 
=================================================================
Total params: 289
Trainable params: 289
Non-trainable params: 0
_________________________________________________________________

Comparison

Method When to Use Advantage
input_shape Single layers Simple and direct
build() Manual control Explicit weight creation
Input layer Complete models Clear model architecture

Conclusion

Specify input shapes in Keras using input_shape parameter, build() method, or Input layers. This ensures proper weight initialization and enables immediate model inspection before training.

Updated on: 2026-03-25T14:44:01+05:30

241 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements