Installation & Setup
Installation
# Requires CUDA and cuDNN installed
pip install tensorflow[and-cuda]
# CPU-only version
pip install tensorflow
# Install specific version
pip install tensorflow==2.13.0
# Install with conda
conda install -c conda-forge tensorflow
# Install TensorFlow Addons
pip install tensorflow-addons
import tensorflow.keras as keras
# Check TensorFlow version
print(tf.__version__)
# Check GPU availability
print("GPU Available:", tf.config.list_physical_devices('GPU'))
print("Num GPUs Available:", len(tf.config.list_physical_devices('GPU')))
# Check if eager execution is enabled
print("Eager execution:", tf.executing_eagerly())
Basic Setup
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
import numpy as np
import matplotlib.pyplot as plt
# Set memory growth for GPU
gpus = tf.config.experimental.list_physical_devices('GPU')
if gpus:
try:
for gpu in gpus:
tf.config.experimental.set_memory_growth(gpu, True)
except RuntimeError as e:
print(e)
# Set random seed for reproducibility
tf.random.set_seed(42)
np.random.seed(42)
Tensors
Tensor Creation
t1 = tf.constant([1, 2, 3])
t2 = tf.constant([[1, 2], [3, 4]])
# Special tensors
zeros = tf.zeros([2, 3])
ones = tf.ones([2, 3])
eye = tf.eye(3)
rand = tf.random.uniform([2, 3])
randn = tf.random.normal([2, 3])
# With specific data type
t_float32 = tf.constant([1, 2, 3], dtype=tf.float32)
t_int32 = tf.constant([1, 2, 3], dtype=tf.int32)
# Variable tensors
var_tensor = tf.Variable([1.0, 2.0, 3.0])
# Convert between tensor and numpy
numpy_array = t1.numpy()
tensor_from_numpy = tf.constant(numpy_array)
tf.float32 - 32-bit floating point
tf.float64 - 64-bit floating point
tf.int32 - 32-bit integer
tf.int64 - 64-bit integer
tf.bool - Boolean
tf.string - String
Tensor Operations
a = tf.constant([1, 2, 3])
b = tf.constant([4, 5, 6])
# Element-wise operations
add = tf.add(a, b)
sub = tf.subtract(a, b)
mul = tf.multiply(a, b)
div = tf.divide(a, b)
# Matrix multiplication
mat1 = tf.random.normal([2, 3])
mat2 = tf.random.normal([3, 4])
matmul = tf.matmul(mat1, mat2)
# Reduction operations
x = tf.constant([[1, 2], [3, 4]])
sum_all = tf.reduce_sum(x)
sum_dim0 = tf.reduce_sum(x, axis=0)
mean_all = tf.reduce_mean(x)
max_val = tf.reduce_max(x)
x = tf.random.normal([2, 3, 4])
# Reshape
reshaped = tf.reshape(x, [6, 4])
# Transpose
transposed = tf.transpose(x)
# Expand and squeeze dimensions
expanded = tf.expand_dims(x, axis=0)
squeezed = tf.squeeze(expanded)
# Concatenation
concat = tf.concat([x, x], axis=0)
# Stacking
stacked = tf.stack([x, x], axis=0)
Keras Models
Sequential API
model = keras.Sequential([
layers.Dense(128, activation='relu', input_shape=(784,)),
layers.Dropout(0.2),
layers.Dense(64, activation='relu'),
layers.Dense(10, activation='softmax')
])
# Alternative way to build Sequential model
model = keras.Sequential()
model.add(layers.Dense(128, activation='relu', input_shape=(784,)))
model.add(layers.Dropout(0.2))
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(10, activation='softmax'))
# Model summary
model.summary()
cnn_model = keras.Sequential([
layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),
layers.MaxPooling2D((2, 2)),
layers.Conv2D(64, (3, 3), activation='relu'),
layers.MaxPooling2D((2, 2)),
layers.Flatten(),
layers.Dense(64, activation='relu'),
layers.Dropout(0.5),
layers.Dense(10, activation='softmax')
])
Functional API
# Input layer
inputs = keras.Input(shape=(784,))
# Hidden layers
x = layers.Dense(128, activation='relu')(inputs)
x = layers.Dropout(0.2)(x)
x = layers.Dense(64, activation='relu')(x)
# Output layer
outputs = layers.Dense(10, activation='softmax')(x)
# Create model
model = keras.Model(inputs=inputs, outputs=outputs)
# Model with multiple inputs/outputs
input1 = keras.Input(shape=(32,))
input2 = keras.Input(shape=(32,))
x1 = layers.Dense(16, activation='relu')(input1)
x2 = layers.Dense(16, activation='relu')(input2)
combined = layers.concatenate([x1, x2])
outputs = layers.Dense(1, activation='sigmoid')(combined)
multi_model = keras.Model(inputs=[input1, input2], outputs=outputs)
Multi-input models - Models with multiple input sources
Multi-output models - Models with multiple outputs
Shared layers - Layers shared between different parts
Residual connections - Skip connections in networks
Complex architectures - Any non-sequential architecture
Layers
Common Layers
Dense - Fully connected layer
Conv2D - 2D convolutional layer
LSTM - Long Short-Term Memory layer
Embedding - Embedding layer for sequences
Flatten - Flattens the input
# Dense layer
dense_layer = layers.Dense(
units=64,
activation='relu',
kernel_initializer='he_normal',
use_bias=True
)
# Conv2D layer
conv_layer = layers.Conv2D(
filters=32,
kernel_size=(3, 3),
strides=(1, 1),
padding='same',
activation='relu'
)
# LSTM layer
lstm_layer = layers.LSTM(
units=50,
return_sequences=False,
dropout=0.2
)
Special Layers
ReLU - Rectified Linear Unit
Sigmoid - Sigmoid function
Tanh - Hyperbolic tangent
Softmax - Softmax function
LeakyReLU - Leaky ReLU
# Batch Normalization
batch_norm = layers.BatchNormalization()
# Dropout
dropout = layers.Dropout(0.5)
# Layer Normalization
layer_norm = layers.LayerNormalization()
# Pooling layers
max_pool = layers.MaxPooling2D((2, 2))
avg_pool = layers.AveragePooling2D((2, 2))
global_avg_pool = layers.GlobalAveragePooling2D()
# Reshape layers
reshape = layers.Reshape((28, 28, 1))
flatten = layers.Flatten()
Training
Model Compilation
model.compile(
optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy']
)
# Advanced compilation with custom parameters
model.compile(
optimizer=keras.optimizers.Adam(
learning_rate=0.001,
beta_1=0.9,
beta_2=0.999
),
loss=keras.losses.SparseCategoricalCrossentropy(from_logits=False),
metrics=['accuracy', 'precision', 'recall']
)
BinaryCrossentropy - Binary classification
CategoricalCrossentropy - Multi-class classification
SparseCategoricalCrossentropy - Multi-class with integer labels
MeanSquaredError - Regression tasks
MeanAbsoluteError - Regression tasks
Training Loop
history = model.fit(
x_train, y_train,
batch_size=32,
epochs=10,
validation_data=(x_val, y_val),
verbose=1
)
# Training with callbacks
callbacks = [
keras.callbacks.EarlyStopping(patience=3),
keras.callbacks.ReduceLROnPlateau(factor=0.1, patience=2),
keras.callbacks.ModelCheckpoint('best_model.h5', save_best_only=True)
]
history = model.fit(
x_train, y_train,
batch_size=32,
epochs=50,
validation_split=0.2,
callbacks=callbacks
)
@tf.function
def train_step(x, y):
with tf.GradientTape() as tape:
predictions = model(x, training=True)
loss = loss_fn(y, predictions)
gradients = tape.gradient(loss, model.trainable_variables)
optimizer.apply_gradients(zip(gradients, model.trainable_variables))
return loss
# Training loop
for epoch in range(epochs):
for batch, (x_batch, y_batch) in enumerate(dataset):
loss = train_step(x_batch, y_batch)
if batch % 100 == 0:
print(f'Epoch {epoch}, Batch {batch}, Loss: {loss.numpy():.4f}')
Data Pipeline
Data Loading
# From tensors
dataset = tf.data.Dataset.from_tensor_slices((x_train, y_train))
# From generator
def data_generator():
for i in range(len(x_train)):
yield x_train[i], y_train[i]
dataset = tf.data.Dataset.from_generator(
data_generator,
output_types=(tf.float32, tf.int32),
output_shapes=((784,), ())
)
# From text files
text_dataset = tf.data.TextLineDataset(["file1.txt", "file2.txt"])
# MNIST dataset
(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()
# CIFAR-10 dataset
(x_train, y_train), (x_test, y_test) = keras.datasets.cifar10.load_data()
# IMDB dataset
(x_train, y_train), (x_test, y_test) = keras.datasets.imdb.load_data()
# Preprocess data
x_train = x_train.astype('float32') / 255.0
x_test = x_test.astype('float32') / 255.0
# One-hot encode labels
y_train = keras.utils.to_categorical(y_train, 10)
y_test = keras.utils.to_categorical(y_test, 10)
Data Preprocessing
dataset = tf.data.Dataset.from_tensor_slices((x_train, y_train))
# Apply transformations
dataset = dataset.shuffle(buffer_size=10000)
dataset = dataset.batch(32)
dataset = dataset.prefetch(tf.data.AUTOTUNE)
dataset = dataset.repeat() # For multiple epochs
# Map function for data augmentation
def augment_image(image, label):
image = tf.image.random_flip_left_right(image)
image = tf.image.random_brightness(image, max_delta=0.1)
image = tf.image.random_contrast(image, lower=0.9, upper=1.1)
return image, label
dataset = dataset.map(augment_image)
# Caching
dataset = dataset.cache()
Shuffle - Always shuffle your data
Batch - Use appropriate batch size
Prefetch - Use prefetch for performance
Cache - Cache datasets when possible
Parallelize - Use num_parallel_calls for map operations
Advanced Features
Custom Layers & Models
class CustomDenseLayer(layers.Layer):
def __init__(self, units=32):
super().__init__()
self.units = units
def build(self, input_shape):
self.w = self.add_weight(
shape=(input_shape[-1], self.units),
initializer="random_normal",
trainable=True,
)
self.b = self.add_weight(
shape=(self.units,),
initializer="random_normal",
trainable=True,
)
def call(self, inputs):
return tf.matmul(inputs, self.w) + self.b
# Custom model
class CustomModel(keras.Model):
def __init__(self):
super().__init__()
self.dense1 = layers.Dense(64, activation="relu")
self.dense2 = layers.Dense(10)
def call(self, inputs):
x = self.dense1(inputs)
return self.dense2(x)
Model Saving & Deployment
# Save entire model
model.save('my_model.h5')
loaded_model = keras.models.load_model('my_model.h5')
# Save only weights
model.save_weights('model_weights.h5')
model.load_weights('model_weights.h5')
# Save in SavedModel format
tf.saved_model.save(model, 'saved_model')
loaded_model = tf.saved_model.load('saved_model')
# Convert to TensorFlow Lite
converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()
with open('model.tflite', 'wb') as f:
f.write(tflite_model)
# Save model for serving
model.save('serving_model/1/', save_format='tf')
# Create signature for serving
@tf.function
def serve(x):
return {'predictions': model(x)}
# Export for serving
tf.saved_model.save(
model,
'serving_model',
signatures={'serving_default': serve}
)
Additional Resources
Learning Resources
- Official Guides: TensorFlow Tutorials, Keras Guides
- Courses: TensorFlow Developer Certificate, DeepLearning.AI
- Books: "Hands-On Machine Learning", "Deep Learning with Python"
- Documentation: TensorFlow API Docs, Keras Documentation
- Communities: TensorFlow Forum, Stack Overflow, GitHub
Useful Tools
- Visualization: TensorBoard, Matplotlib
- Deployment: TensorFlow Serving, TF Lite, TF.js
- Extensions: TensorFlow Addons, TFX, TF Agents
- Datasets: TF Datasets, Kaggle Datasets
- Monitoring: Weights & Biases, MLflow
Comprehensive TensorFlow Deep Learning Cheatsheet Reference
This TensorFlow Deep Learning cheatsheet on Nikhil Learn Hub collects syntax, commands, and practical snippets for quick revision. Understand TensorFlow tensors, neural networks, model training, and AI workflows with practical code examples.
Use the reference cards and examples above during coding sessions; return here instead of scattered searches when you need dependable reminders. Follow the Deep learning learning roadmap when you want structured lessons beyond one-page lookups.
Quick lookup coverage
- Syntax, commands, and API signatures
- Copy-ready examples and common patterns
- Terminology for coursework and interviews
- Cross-links to the matching learning roadmap
How to study with this sheet
- Production debugging and tuning reminders
- Security, performance, or scale cautions
- Integration with adjacent stacks on this site
- Deeper study through tutorials and roadmaps
Who Should Use This Cheatsheet
Students, self-taught developers, and professionals who need fast TensorFlow Deep Learning lookups during labs, debugging, or interview revision should keep this page bookmarked.
Related Resources on Nikhil Learn Hub
- Deep learning learning roadmapstructured learning path for the same technology
- Cheatsheets hubbrowse all quick-reference sheets
- Technology hubtutorials, roadmaps, and practice hubs