TensorFlow Cheatsheet
Layers
Use this TensorFlow reference while you build software engineering projects, review code for technical interview prep, or polish examples for a software engineer resume.
Core Dense / Linear
from tensorflow.keras import layers layers.Dense( units=64, activation='relu', # string, callable, or None use_bias=True, kernel_initializer='glorot_uniform', bias_initializer='zeros', kernel_regularizer=None, bias_regularizer=None, activity_regularizer=None, kernel_constraint=None, bias_constraint=None, ) layers.EinsumDense('ij,jk->ik', output_shape=64) # explicit einsum mapping
Convolutional Layers
| Layer | Use |
|---|---|
Conv1D | 1-D sequences (text, audio, time series) |
Conv2D | Images |
Conv3D | Video / volumetric |
SeparableConv2D | Depth-wise + point-wise (MobileNet style) |
DepthwiseConv2D | Per-channel convolution only |
Conv2DTranspose | Upsampling / decoder path |
Conv1DTranspose | Sequence upsampling |
layers.Conv2D( filters=32, kernel_size=(3, 3), # int or tuple strides=(1, 1), padding='valid', # 'valid' | 'same' ('causal' is Conv1D-only) data_format='channels_last', # 'channels_first' for GPU-optimized dilation_rate=(1, 1), # atrous convolution groups=1, # group convolution activation='relu', use_bias=True, ) # Transpose (upsampling) layers.Conv2DTranspose(64, 3, strides=2, padding='same') # Depthwise separable layers.SeparableConv2D(64, 3, activation='relu')
Padding rule of thumb
'valid': output shrinks:floor((input - kernel) / stride) + 1'same': output =ceil(input / stride)(zero-pads)
Pooling Layers
| Layer | Description |
|---|---|
MaxPooling1D/2D/3D | Max in each window |
AveragePooling1D/2D/3D | Mean in each window |
GlobalMaxPooling1D/2D/3D | Single max per channel |
GlobalAveragePooling1D/2D/3D | Single mean per channel (common before Dense head) |
layers.MaxPooling2D(pool_size=(2, 2), strides=None, padding='valid') layers.AveragePooling2D(2) layers.GlobalAveragePooling2D(keepdims=False) # (B,H,W,C) → (B,C) layers.GlobalMaxPooling2D()
Normalization Layers
| Layer | Normalizes over | Typical use |
|---|---|---|
BatchNormalization | Batch dimension | Most CNNs |
LayerNormalization | Last axis (features) | Transformers, RNNs |
GroupNormalization | Groups of channels | Small batches |
GroupNormalization(groups=-1) | Spatial dims per sample (instance norm) | Style transfer |
layers.BatchNormalization( axis=-1, # features axis momentum=0.99, # running mean/var EMA epsilon=1e-3, center=True, # add beta scale=True, # add gamma ) # call with training=True during fit; training=False during inference # BN has non_trainable_weights (moving_mean, moving_variance) layers.LayerNormalization(axis=-1, epsilon=1e-6) layers.GroupNormalization(groups=8, axis=-1) layers.GroupNormalization(groups=-1) # groups=-1 → instance normalization
Recurrent Layers
# LSTM — returns last hidden state by default layers.LSTM(128) layers.LSTM(128, return_sequences=True) # return all steps layers.LSTM(128, return_state=True) # (output, h, c) layers.LSTM(128, dropout=0.2, recurrent_dropout=0.2) layers.LSTM(128, go_backwards=True) # GRU layers.GRU(64, return_sequences=True) # SimpleRNN (rarely used in practice) layers.SimpleRNN(32) # Stacked model = keras.Sequential([ layers.LSTM(128, return_sequences=True), layers.LSTM(64), layers.Dense(10), ]) # Bidirectional layers.Bidirectional(layers.LSTM(64, return_sequences=True)) layers.Bidirectional(layers.LSTM(64), merge_mode='concat') # 'sum'|'mul'|'ave'|'concat' # TimeDistributed — apply a layer to every time step layers.TimeDistributed(layers.Dense(32))
return_sequences=Truemust be set on all LSTM/GRU except the last in a stack.
Embedding Layer
layers.Embedding( input_dim=10000, # vocabulary size output_dim=64, # embedding dimension embeddings_initializer='uniform', embeddings_regularizer=None, mask_zero=False, # True to propagate masking (for variable-length) ) # input_length= was removed in Keras 3 — sequence length is inferred from input shape # Load pretrained (e.g., GloVe) emb = layers.Embedding(vocab_size, 100) emb.build((None,)) emb.set_weights([glove_matrix]) emb.trainable = False
Attention Layers
# Dot-product attention (Bahdanau/general) layers.Attention(use_scale=True) # [query, value] or [query, value, key] # Multi-head self-attention (Transformer) layers.MultiHeadAttention( num_heads=8, key_dim=64, # per-head dimension value_dim=None, # defaults to key_dim dropout=0.1, use_bias=True, ) # Usage mha = layers.MultiHeadAttention(num_heads=8, key_dim=64) attn_out, attn_weights = mha(query, value, key, return_attention_scores=True, training=False)
Reshaping Layers
layers.Flatten() layers.Reshape(target_shape=(7, 7, 64)) # excludes batch dim layers.Permute(dims=(2, 1)) # swap axes (1-indexed, no batch) layers.RepeatVector(n=10) # (B, feats) → (B, 10, feats) layers.Cropping2D(cropping=((2, 2), (2, 2))) layers.ZeroPadding2D(padding=(1, 1)) layers.UpSampling2D(size=(2, 2), interpolation='nearest') layers.UpSampling1D(size=2)
Dropout Layers
layers.Dropout(rate=0.5, seed=42) layers.SpatialDropout1D(rate=0.3) # whole channels for 1D (text) layers.SpatialDropout2D(rate=0.3) # whole feature maps for images layers.SpatialDropout3D(rate=0.3) layers.AlphaDropout(rate=0.1) # keeps mean/variance (use with SELU) layers.GaussianDropout(rate=0.1) # multiplicative Gaussian noise layers.GaussianNoise(stddev=0.1) # additive noise (only active at training)
Merge / Combination Layers
layers.Add()([a, b]) layers.Subtract()([a, b]) layers.Multiply()([a, b]) layers.Average()([a, b]) layers.Concatenate(axis=-1)([a, b]) layers.Dot(axes=-1)([a, b]) # dot product along axis
Preprocessing / Input Layers
| Layer | Purpose |
|---|---|
Normalization | Subtract mean, divide std (call .adapt()) |
Rescaling | Scale pixel values, e.g., 1./255 |
Resizing | Resize image tensors |
CenterCrop | Crop from center |
RandomCrop | Random crop (augmentation) |
RandomFlip | Random horizontal/vertical flip |
RandomRotation | Random rotation |
RandomZoom | Random zoom |
RandomBrightness | Random brightness shift |
RandomContrast | Random contrast jitter |
RandomTranslation | Random translation |
TextVectorization | Map text → integer sequences |
Discretization | Continuous → categorical bins |
Hashing | Hash categorical features |
IntegerLookup | Integer → integer id |
StringLookup | String → integer id |
CategoryEncoding | Integer id → one-hot / multi-hot |
rescale = layers.Rescaling(scale=1.0/255) norm = layers.Normalization() norm.adapt(train_images) aug = keras.Sequential([ layers.RandomFlip('horizontal'), layers.RandomRotation(0.1), layers.RandomZoom(0.1), ])
Lambda Layer
layers.Lambda(lambda x: x ** 2) layers.Lambda(lambda x: tf.math.l2_normalize(x, axis=1))
Prefer writing a proper
Layersubclass overLambdafor anything non-trivial —Lambdalayers do not serialize cleanly.
Activation Layers (standalone)
layers.Activation('gelu') layers.ReLU(max_value=6.0, negative_slope=0.0, threshold=0.0) # ReLU6 = max_value=6 layers.LeakyReLU(negative_slope=0.3) layers.PReLU() # learned slope layers.ELU(alpha=1.0) layers.Softmax(axis=-1) # ThresholdedReLU was removed in Keras 3 — use layers.ReLU(threshold=...)