The Ultimate TensorFlow Cheatsheet: From Basics to Advanced

If you are starting your journey in machine learning or deep learning, you will likely come across TensorFlow. Built by Google, TensorFlow is one of the most popular open-source libraries for building and deploying machine learning models. From powering image recognition systems to natural language processing models, TensorFlow is the go-to framework for developers, researchers and data scientists.

The Ultimate TensorFlow Cheatsheet: From Basics to Advanced

However, the TensorFlow ecosystem is vast and can sometimes feel overwhelming. That is why having a TensorFlow Cheatsheet makes your work easier. This guide provides a comprehensive collection of TensorFlow commands, methods and workflows you will need while working on ML and DL projects.

Whether you are a beginner learning the basics or an experienced developer looking for a quick reference, this cheatsheet will be useful.

What is TensorFlow?

TensorFlow is an end-to-end open-source machine learning platform. It provides tools for:

  • Creating neural networks
  • Training and evaluating models
  • Running ML models on CPUs, GPUs and TPUs
  • Deploying models in production

With Keras integrated into TensorFlow, building and training models has become faster and more intuitive.

1. Installation & Setup

pip install tensorflow
import tensorflow as tf
print(tf.__version__)
print("GPU Available:", tf.config.list_physical_devices('GPU'))

Install the core TensorFlow package. Importing TensorFlow confirms the install and prints the version to ensure compatibility with your code. Checking physical GPU devices tells you whether TensorFlow can use hardware acceleration. If you have an NVIDIA GPU, install the appropriate CUDA/cuDNN drivers. Apple Silicon users can install tensorflow-macos.

2. Creating Tensors

tf.constant([1, 2, 3])                  
tf.zeros([3, 3])                        
tf.ones([2, 2])                         
tf.eye(3)                               
tf.range(0, 10, 2)                      
tf.random.uniform([2, 2], 0, 1)         
tf.random.normal([2, 2], mean=0, stddev=1) 


Tensors are multi-dimensional arrays, the fundamental data type in TensorFlow. Use constant for fixed values, zeros/ones/eye for common initializations, range for sequences, and random.* for randomized initialization. Specify shapes as lists/tuples. You can add dtype= (e.g., tf.float32) if you need a specific precision.

Tip: Use tf.Variable when you want parameters that will be updated during training (weights, biases). For immutable data, tf.Tensor is enough.

3. Tensor Operations

x = tf.constant([[1, 2], [3, 4]])
y = tf.constant([[5, 6], [7, 8]])

tf.add(x, y)         
tf.subtract(x, y)    
tf.multiply(x, y)    
tf.matmul(x, y)      
tf.transpose(x)      
tf.reshape(x, (4,))  


These are standard linear-algebra operations. multiply is element-wise; matmul is true matrix multiplication. transpose swaps axes; reshape changes the view of the data without copying it (when possible). TensorFlow supports broadcasting, so shapes that are compatible will auto-expand across dimensions.

4. Data Pipeline with tf.data

dataset = tf.data.Dataset.from_tensor_slices((x_train, y_train))
dataset = dataset.shuffle(buffer_size=1024).batch(32).prefetch(tf.data.AUTOTUNE)

tf.data builds scalable input pipelines. from_tensor_slices converts NumPy arrays or tensors into a dataset. shuffle randomizes example order to reduce overfitting, batch groups samples for efficient training, and prefetch overlaps preprocessing and model execution for better throughput. For large files, use TFRecordDataset and map for parsing/augmentation.

5. Building Models (Keras API)

Sequential API

model = tf.keras.Sequential([
    tf.keras.layers.Flatten(input_shape=(28,28)),
    tf.keras.layers.Dense(128, activation='relu'),
    tf.keras.layers.Dropout(0.2),
    tf.keras.layers.Dense(10, activation='softmax')
])


Use Sequential when layers stack linearly from input to output. Flatten converts 2D images to 1D vectors, Dense creates fully connected layers, Dropout randomly disables neurons to reduce overfitting and softmax converts logits to class probabilities for multi-class classification.

Functional API

inputs = tf.keras.Input(shape=(28, 28))
x = tf.keras.layers.Flatten()(inputs)
x = tf.keras.layers.Dense(128, activation='relu')(x)
outputs = tf.keras.layers.Dense(10, activation='softmax')(x)
model = tf.keras.Model(inputs=inputs, outputs=outputs)


The Functional API supports non-linear topologies (skip connections, multi-input/multi-output). It offers more flexibility than Sequential while keeping the same training interface.

6. Common Layers

tf.keras.layers.Conv2D(32, (3,3), activation='relu')
tf.keras.layers.MaxPooling2D((2,2))
tf.keras.layers.LSTM(64, return_sequences=True)
tf.keras.layers.Embedding(input_dim=1000, output_dim=64)
tf.keras.layers.BatchNormalization()
tf.keras.layers.Dropout(0.5)
  • Conv2D extracts spatial features from images.
  • MaxPooling2D downsamples feature maps to reduce computation and capture invariances.
  • LSTM handles sequences/time-series; return_sequences=True outputs a sequence for stacking or sequence labeling.
  • Embedding learns dense vector representations for tokens/IDs in NLP and recommender systems.
  • BatchNormalization stabilizes training by normalizing activations.
  • Dropout helps generalization by randomly zeroing activations during training.

7. Compiling Models

model.compile(
    optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),
    loss='categorical_crossentropy',
    metrics=['accuracy']
)


compile wires the training configuration. Choose an optimizer (Adam is a good default), a loss that matches your task (e.g. binary_crossentropy, sparse_categorical_crossentropy), and metrics to monitor.

  • Use sparse_categorical_crossentropy when labels are integer-encoded.
  • Use categorical_crossentropy when labels are one-hot encoded.
  • If the final layer does not use softmax/sigmoid, pass from_logits=True to the loss.

8. Training & Evaluation

model.fit(x_train, y_train, epochs=10, batch_size=32, validation_split=0.2)
model.evaluate(x_test, y_test)


fit starts gradient-based optimization. validation_split reserves a portion of training data for validation-use validation_data=(x_val, y_val) when you have a separate set. evaluate returns loss and metrics on test data to estimate generalization.

Tip: Monitor val_loss and val_accuracy. If training accuracy rises while validation accuracy stalls or drops, you are likely overfitting.

9. Predictions

predictions = model.predict(x_test[:5])
print(tf.argmax(predictions, axis=1).numpy())


predict runs a forward pass and returns probabilities (with softmax) or logits (without). argmax selects the most probable class index. For binary classification, compare prediction values to a threshold (commonly 0.5) instead of argmax.

10. Callbacks

callbacks = [
    tf.keras.callbacks.EarlyStopping(patience=3, monitor='val_loss'),
    tf.keras.callbacks.ModelCheckpoint('best_model.h5', save_best_only=True),
    tf.keras.callbacks.TensorBoard(log_dir='./logs')
]

model.fit(x_train, y_train, epochs=20, validation_data=(x_test, y_test), callbacks=callbacks)


Callbacks automate training control.

  • EarlyStopping stops training when validation performance stops improving, preventing overfitting.
  • ModelCheckpoint saves the best weights for later use or deployment.
  • TensorBoard logs metrics for interactive visual analysis (tensorboard --logdir ./logs).
    Also consider ReduceLROnPlateau to lower learning rate when progress stalls.

11. Custom Training Loop (Advanced)

loss_fn = tf.keras.losses.SparseCategoricalCrossentropy()
optimizer = tf.keras.optimizers.Adam()

for epoch in range(5):
    for x_batch, y_batch in dataset:
        with tf.GradientTape() as tape:
            predictions = model(x_batch, training=True)
            loss = loss_fn(y_batch, predictions)
        grads = tape.gradient(loss, model.trainable_variables)
        optimizer.apply_gradients(zip(grads, model.trainable_variables))
    print(f"Epoch {epoch+1}, Loss: {loss.numpy()}")


When you need fine-grained control (custom losses, multi-task training, RL, contrastive learning), write your own step with tf.GradientTape. Inside the tape context, compute predictions and loss; then derive gradients and apply them. Wrap the inner step with @tf.function to compile it with graph execution for speed if needed.

12. GPU & TPU Usage

print(tf.config.list_physical_devices())

gpus = tf.config.experimental.list_physical_devices('GPU')
for gpu in gpus:
    tf.config.experimental.set_memory_growth(gpu, True)


List available devices to verify hardware is visible. Enabling memory growth prevents TensorFlow from allocating all GPU memory upfront, reducing out-of-memory errors when multiple processes use the GPU. For multi-GPU training, use tf.distribute.MirroredStrategy(). For TPUs (on cloud), use tf.distribute.TPUStrategy().

13. Saving & Loading Models

# Save in H5 format
model.save("model.h5")
model = tf.keras.models.load_model("model.h5")

# Save in TensorFlow SavedModel format
model.save("saved_model")
model = tf.keras.models.load_model("saved_model")

Saving in H5 is convenient and widely supported; SavedMode is TensorFlow’s standard format for serving and cross-language deployment. If you only need weights, use model.save_weights() and model.load_weights(). For custom layers or losses, pass custom_objects when loading.

14. Visualization

import matplotlib.pyplot as plt

history = model.fit(x_train, y_train, epochs=5, validation_data=(x_test, y_test))

plt.plot(history.history['accuracy'], label='Train Acc')
plt.plot(history.history['val_accuracy'], label='Val Acc')
plt.legend()
plt.show()

Explanation:
model.fit returns a History object containing metric traces per epoch. Plotting them helps diagnose underfitting (both curves low), overfitting (training high, validation low) or learning-rate issues (no improvement). For richer diagnostics – scalars, graphs, histograms—use TensorBoard.

Conclusion

This TensorFlow Cheatsheet is your quick reference guide to mastering deep learning with TensorFlow. From creating tensors and performing operations to building, training, visualizing, and saving models, this cheatsheet covers everything you need.

Bookmark this guide, share it with your team, and use it as a handy reference whenever you are working on machine learning projects.

TensorFlow may look complex at first, but with the right cheatsheet, you can build powerful AI models with confidence.

External Resources

GeeksforGeeks TensorFlow Cheat Sheet
https://www.geeksforgeeks.org/blogs/tensorflow-cheat-sheet

Zero to Mastery Cheat Sheet
https://zerotomastery.io/cheatsheets/tensorflow-cheat-sheet

Secret Data Scientist Cheat Sheet
https://secretdatascientist.com/tensor-flow-cheat-sheet


3 thoughts on “The Ultimate TensorFlow Cheatsheet: From Basics to Advanced”

Leave a Comment