Models are assembled from these layers; knowing their shapes lets you design and debug architectures.
Keras layers are callables that hold weights and transform tensors. The ones you will use most:
Dense(units, activation=...): fully connected, acts on the last axis.Conv2D(filters, kernel_size, strides, padding): 2-d convolution on channels-last images(batch, height, width, channels).padding="same"keeps height and width at stride 1;"valid"(the default) shrinks them.MaxPooling2D(2),GlobalAveragePooling2D(): downsample or collapse spatial dimensions.Flatten(): reshape to(batch, features).Embedding(input_dim, output_dim): integer ids to vectors.LSTM,GRU,MultiHeadAttention: sequence layers.BatchNormalization,LayerNormalization,Dropout.
Activations can be passed by name ("relu", "gelu", "softmax") or used as separate Activation layers.
A layer creates its weights the first time it sees an input shape (build), so Conv2D(16, 3) does not need to know the number of input channels. After that, layer.weights, layer.kernel and layer.bias are available.
Calling a layer directly on a NumPy array is a quick way to check output shapes before assembling a model.
Example
import numpy as np
import keras
from keras import layers
x = np.random.rand(2, 28, 28, 1).astype("float32") # N, H, W, C
conv = layers.Conv2D(16, kernel_size=3, padding="same", activation="relu")
y = conv(x)
print(tuple(y.shape)) # (2, 28, 28, 16)
print(tuple(layers.MaxPooling2D(2)(y).shape)) # (2, 14, 14, 16)
print(tuple(conv.kernel.shape), tuple(conv.bias.shape)) # (3, 3, 1, 16) (16,)
print(tuple(layers.Flatten()(y).shape)) # (2, 12544)
print(tuple(layers.GlobalAveragePooling2D()(y).shape)) # (2, 16)
emb = layers.Embedding(input_dim=1000, output_dim=8)
print(tuple(emb(np.array([[4, 20, 7]])).shape)) # (1, 3, 8)Remember
- Keras images are channels-last: (N, H, W, C)
- Layers build their weights on first call
padding='same'keeps spatial size at stride 1
Watch out for
- Feeding channels-first image arrays from PyTorch-style pipelines without transposing.