0
0
TensorFlowml~5 mins

Input shape specification in TensorFlow

Choose your learning style9 modes available
Introduction
We tell the model the shape of the data it will see so it can learn correctly.
When building a neural network to process images of a fixed size.
When creating a model that takes sequences like sentences or time series.
When you want to make sure your data fits the model's expected format.
When you start defining the first layer of a neural network.
When you want to avoid errors caused by mismatched data shapes.
Syntax
TensorFlow
tf.keras.layers.Input(shape=(dim1, dim2, ...))
The shape does NOT include the batch size (number of samples).
Use a tuple to specify dimensions, for example (28, 28, 1) for grayscale images.
Examples
Defines an input layer for 28x28 grayscale images.
TensorFlow
input_layer = tf.keras.layers.Input(shape=(28, 28, 1))
Defines an input layer for 1D data with 100 features.
TensorFlow
input_layer = tf.keras.layers.Input(shape=(100,))
Defines an input layer for sequences of any length with 64 features each.
TensorFlow
input_layer = tf.keras.layers.Input(shape=(None, 64))
Sample Model
This code creates a simple model that expects 28x28 grayscale images as input. It flattens the image and outputs 10 class probabilities.
TensorFlow
import tensorflow as tf

# Define input shape for 28x28 grayscale images
input_layer = tf.keras.layers.Input(shape=(28, 28, 1))

# Add a flatten layer to convert 2D image to 1D vector
flatten = tf.keras.layers.Flatten()(input_layer)

# Add a dense layer with 10 outputs (for example, 10 classes)
dense = tf.keras.layers.Dense(10, activation='softmax')(flatten)

# Create the model
model = tf.keras.Model(inputs=input_layer, outputs=dense)

# Compile the model
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])

# Print model summary to show input shape
model.summary()
OutputSuccess
Important Notes
Always exclude the batch size when specifying input shape; TensorFlow handles it automatically.
For color images, use 3 channels (e.g., shape=(height, width, 3)).
Using None in shape allows variable length for that dimension, useful for sequences.
Summary
Input shape tells the model what size and type of data to expect.
It is specified in the first layer using tf.keras.layers.Input with a shape tuple.
Correct input shape helps avoid errors and ensures the model learns properly.