Detailed TensorFlow Keras Cheat
Sheet
Comprehensive Guide for Deep
Learning
Introduction to TensorFlow Keras
• Keras is a high-level API for building and
training deep learning models.
• Integrated tightly with TensorFlow for efficient
computation.
• Offers Sequential, Functional, and Subclassing
approaches.
Sequential API
• Sequential Model: model =
tf.keras.Sequential([...])
• Example:
• model = tf.keras.Sequential([
• tf.keras.layers.Dense(128, activation='relu'),
• tf.keras.layers.Dense(10,
activation='softmax')
• ])
• Best for simple, layer-by-layer stacking.
Functional API
• Define complex models with shared layers and
custom outputs.
• Example:
• inputs = tf.keras.Input(shape=(784,))
• x = tf.keras.layers.Dense(128, activation='relu')
(inputs)
• outputs = tf.keras.layers.Dense(10,
activation='softmax')(x)
• model = tf.keras.Model(inputs, outputs)
Subclassing Model
• Subclass tf.keras.Model for full control over
architecture.
• Example:
• class MyModel(tf.keras.Model):
• def __init__(self):
• super(MyModel, self).__init__()
• self.dense1 = tf.keras.layers.Dense(128,
activation='relu')
• self.dense2 = tf.keras.layers.Dense(10,
Common Layers
• Dense Layer: Fully connected layer.
• Example: tf.keras.layers.Dense(64,
activation='relu')
• Conv2D Layer: 2D convolutional layer.
• Example: tf.keras.layers.Conv2D(32, (3, 3),
activation='relu')
• Dropout Layer: Regularization to prevent
overfitting.
• Example: tf.keras.layers.Dropout(0.5)
Model Compilation
• Compile the model with optimizer, loss, and
metrics.
• Example:
• model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
• Loss Functions: 'mse',
'categorical_crossentropy', etc.
• Metrics: ['accuracy', 'mae', etc.]
Model Training
• Fit the model to your training data.
• Example:
• model.fit(X_train, y_train, epochs=10,
batch_size=32, validation_data=(X_val, y_val))
• Supports callbacks for dynamic training
adjustments.
Model Evaluation
• Evaluate model performance on test data.
• Example:
• loss, accuracy = model.evaluate(X_test, y_test)
• Use metrics like accuracy, precision, and recall.
Making Predictions
• Generate predictions for new data.
• Example:
• predictions = model.predict(new_data)
• Post-process predictions for interpretation.
Data Preprocessing
• Standardization: tf.keras.layers.Normalization()
• Text Tokenization:
tf.keras.preprocessing.text.Tokenizer()
• Image Augmentation:
tf.keras.layers.RandomFlip(),
tf.keras.layers.RandomRotation()
Callbacks
• EarlyStopping: Stop training when
performance stops improving.
• ModelCheckpoint: Save model checkpoints
during training.
• TensorBoard: Monitor training metrics and
logs.
Custom Training Loops
• Full control over training steps using
GradientTape.
• Example:
• with tf.GradientTape() as tape:
• predictions = model(X_train)
• loss = loss_fn(y_train, predictions)
• gradients = tape.gradient(loss,
model.trainable_variables)
• optimizer.apply_gradients(zip(gradients,
Saving and Loading Models
• Save entire model:
• model.save('model.h5')
• Load saved model:
• model =
tf.keras.models.load_model('model.h5')
• Save weights only:
• model.save_weights('weights.h5')
• Load weights only:
• model.load_weights('weights.h5')
Transfer Learning
• Reuse pre-trained models for new tasks.
• Example:
• base_model =
tf.keras.applications.MobileNetV2(include_top
=False, input_shape=(224, 224, 3))
• base_model.trainable = False
• model = tf.keras.Sequential([
• base_model,
• tf.keras.layers.GlobalAveragePooling2D(),
Hyperparameter Tuning
• Use libraries like Keras Tuner for automated
optimization.
• Example:
• def model_builder(hp):
• model = tf.keras.Sequential()
• hp_units = hp.Int('units', min_value=32,
max_value=512, step=32)
•
model.add(tf.keras.layers.Dense(units=hp_unit
s, activation='relu'))
Optimizers
• Adam: Adaptive Moment Estimation.
• Example:
tf.keras.optimizers.Adam(learning_rate=0.001)
• SGD: Stochastic Gradient Descent.
• Example:
tf.keras.optimizers.SGD(learning_rate=0.01,
momentum=0.9)
• RMSprop: Root Mean Square Propagation.
• Example:
tf.keras.optimizers.RMSprop(learning_rate=0.0
Loss Functions
• Mean Squared Error:
tf.keras.losses.MeanSquaredError()
• Sparse Categorical Crossentropy:
tf.keras.losses.SparseCategoricalCrossentropy(
)
• Binary Crossentropy:
tf.keras.losses.BinaryCrossentropy()
Metrics
• Accuracy: tf.keras.metrics.Accuracy()
• Precision: tf.keras.metrics.Precision()
• Recall: tf.keras.metrics.Recall()
• Mean Absolute Error:
tf.keras.metrics.MeanAbsoluteError()
Custom Layers
• Create layers by subclassing
tf.keras.layers.Layer.
• Example:
• class MyLayer(tf.keras.layers.Layer):
• def __init__(self, units=32):
• super(MyLayer, self).__init__()
• self.units = units
• def build(self, input_shape):
• self.w =

Detailed_TensorFlow_Keras_CheatSheet.pptx

  • 1.
    Detailed TensorFlow KerasCheat Sheet Comprehensive Guide for Deep Learning
  • 2.
    Introduction to TensorFlowKeras • Keras is a high-level API for building and training deep learning models. • Integrated tightly with TensorFlow for efficient computation. • Offers Sequential, Functional, and Subclassing approaches.
  • 3.
    Sequential API • SequentialModel: model = tf.keras.Sequential([...]) • Example: • model = tf.keras.Sequential([ • tf.keras.layers.Dense(128, activation='relu'), • tf.keras.layers.Dense(10, activation='softmax') • ]) • Best for simple, layer-by-layer stacking.
  • 4.
    Functional API • Definecomplex models with shared layers and custom outputs. • Example: • inputs = tf.keras.Input(shape=(784,)) • x = tf.keras.layers.Dense(128, activation='relu') (inputs) • outputs = tf.keras.layers.Dense(10, activation='softmax')(x) • model = tf.keras.Model(inputs, outputs)
  • 5.
    Subclassing Model • Subclasstf.keras.Model for full control over architecture. • Example: • class MyModel(tf.keras.Model): • def __init__(self): • super(MyModel, self).__init__() • self.dense1 = tf.keras.layers.Dense(128, activation='relu') • self.dense2 = tf.keras.layers.Dense(10,
  • 6.
    Common Layers • DenseLayer: Fully connected layer. • Example: tf.keras.layers.Dense(64, activation='relu') • Conv2D Layer: 2D convolutional layer. • Example: tf.keras.layers.Conv2D(32, (3, 3), activation='relu') • Dropout Layer: Regularization to prevent overfitting. • Example: tf.keras.layers.Dropout(0.5)
  • 7.
    Model Compilation • Compilethe model with optimizer, loss, and metrics. • Example: • model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) • Loss Functions: 'mse', 'categorical_crossentropy', etc. • Metrics: ['accuracy', 'mae', etc.]
  • 8.
    Model Training • Fitthe model to your training data. • Example: • model.fit(X_train, y_train, epochs=10, batch_size=32, validation_data=(X_val, y_val)) • Supports callbacks for dynamic training adjustments.
  • 9.
    Model Evaluation • Evaluatemodel performance on test data. • Example: • loss, accuracy = model.evaluate(X_test, y_test) • Use metrics like accuracy, precision, and recall.
  • 10.
    Making Predictions • Generatepredictions for new data. • Example: • predictions = model.predict(new_data) • Post-process predictions for interpretation.
  • 11.
    Data Preprocessing • Standardization:tf.keras.layers.Normalization() • Text Tokenization: tf.keras.preprocessing.text.Tokenizer() • Image Augmentation: tf.keras.layers.RandomFlip(), tf.keras.layers.RandomRotation()
  • 12.
    Callbacks • EarlyStopping: Stoptraining when performance stops improving. • ModelCheckpoint: Save model checkpoints during training. • TensorBoard: Monitor training metrics and logs.
  • 13.
    Custom Training Loops •Full control over training steps using GradientTape. • Example: • with tf.GradientTape() as tape: • predictions = model(X_train) • loss = loss_fn(y_train, predictions) • gradients = tape.gradient(loss, model.trainable_variables) • optimizer.apply_gradients(zip(gradients,
  • 14.
    Saving and LoadingModels • Save entire model: • model.save('model.h5') • Load saved model: • model = tf.keras.models.load_model('model.h5') • Save weights only: • model.save_weights('weights.h5') • Load weights only: • model.load_weights('weights.h5')
  • 15.
    Transfer Learning • Reusepre-trained models for new tasks. • Example: • base_model = tf.keras.applications.MobileNetV2(include_top =False, input_shape=(224, 224, 3)) • base_model.trainable = False • model = tf.keras.Sequential([ • base_model, • tf.keras.layers.GlobalAveragePooling2D(),
  • 16.
    Hyperparameter Tuning • Uselibraries like Keras Tuner for automated optimization. • Example: • def model_builder(hp): • model = tf.keras.Sequential() • hp_units = hp.Int('units', min_value=32, max_value=512, step=32) • model.add(tf.keras.layers.Dense(units=hp_unit s, activation='relu'))
  • 17.
    Optimizers • Adam: AdaptiveMoment Estimation. • Example: tf.keras.optimizers.Adam(learning_rate=0.001) • SGD: Stochastic Gradient Descent. • Example: tf.keras.optimizers.SGD(learning_rate=0.01, momentum=0.9) • RMSprop: Root Mean Square Propagation. • Example: tf.keras.optimizers.RMSprop(learning_rate=0.0
  • 18.
    Loss Functions • MeanSquared Error: tf.keras.losses.MeanSquaredError() • Sparse Categorical Crossentropy: tf.keras.losses.SparseCategoricalCrossentropy( ) • Binary Crossentropy: tf.keras.losses.BinaryCrossentropy()
  • 19.
    Metrics • Accuracy: tf.keras.metrics.Accuracy() •Precision: tf.keras.metrics.Precision() • Recall: tf.keras.metrics.Recall() • Mean Absolute Error: tf.keras.metrics.MeanAbsoluteError()
  • 20.
    Custom Layers • Createlayers by subclassing tf.keras.layers.Layer. • Example: • class MyLayer(tf.keras.layers.Layer): • def __init__(self, units=32): • super(MyLayer, self).__init__() • self.units = units • def build(self, input_shape): • self.w =