├── LICENSE ├── Tensorflow Fundamentals ├── 03 Advanced TensorFlow Concepts │ ├── 04 Deployment │ │ ├── README.md │ │ └── deployment.py │ ├── 03 Custom Extensions │ │ ├── README.md │ │ └── custom_extensions.py │ ├── 02 Advanced Architectures │ │ ├── README.md │ │ └── advanced_architectures.py │ └── 01 Distributed Training │ │ ├── README.md │ │ └── distributed_training.py ├── 01 Core TensorFlow Foundations │ ├── 01 Tensors and Operations │ │ ├── README.md │ │ └── tensors_and_operations.py │ ├── 02 Automatic Differentiation │ │ ├── README.md │ │ └── automatic_differentiation.py │ ├── 03 Neural Networks (tf.keras) │ │ ├── README.md │ │ └── neural_networks_keras.py │ ├── 04 Datasets and Data Loading │ │ ├── README.md │ │ └── datasets_and_data_loading.py │ └── 05 Training Pipeline │ │ ├── README.md │ │ └── training_pipeline.py ├── 02 Intermediate TensorFlow Concepts │ ├── 03 Optimization │ │ ├── README.md │ │ └── optimization.py │ ├── 01 Model Architectures │ │ ├── README.md │ │ └── model_architectures.py │ └── 02 Customization │ │ ├── README.md │ │ └── customization.py └── 04 Specialized TensorFlow Libraries │ ├── README.md │ └── specialized_libraries.py ├── README.md └── Tensorflow Interview Questions └── README.md /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2025 rohanmistry231 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /Tensorflow Fundamentals/03 Advanced TensorFlow Concepts/04 Deployment/README.md: -------------------------------------------------------------------------------- 1 | # Deployment (`tensorflow`) 2 | 3 | ## 📖 Introduction 4 | Deploying TensorFlow models enables production use. This guide covers model export (SavedModel), serving (TensorFlow Serving), and edge deployment (TensorFlow Lite, TensorFlow.js), with practical examples and interview insights. 5 | 6 | ## 🎯 Learning Objectives 7 | - Export models as SavedModel for serving. 8 | - Serve models with TensorFlow Serving. 9 | - Deploy models on edge devices with TensorFlow Lite/JS. 10 | 11 | ## 🔑 Key Concepts 12 | - **SavedModel**: Standard TensorFlow format for export. 13 | - **TensorFlow Serving**: Scalable serving via REST/gRPC. 14 | - **TensorFlow Lite**: Lightweight models for mobile/edge. 15 | - **TensorFlow.js**: Browser-based inference. 16 | 17 | ## 📝 Example Walkthrough 18 | The `deployment.py` file demonstrates: 19 | 1. **Model**: Training a CNN on MNIST. 20 | 2. **SavedModel**: Exporting the model. 21 | 3. **TensorFlow Serving**: Instructions for serving. 22 | 4. **TensorFlow Lite/JS**: Converting and evaluating. 23 | 5. **Visualization**: Visualizing predictions. 24 | 25 | Example code: 26 | ```python 27 | import tensorflow as tf 28 | model = tf.keras.Sequential([...]) 29 | model.save("saved_model/mnist_cnn") 30 | ``` 31 | 32 | ## 🛠️ Practical Tasks 33 | 1. Export a trained model as SavedModel. 34 | 2. Set up TensorFlow Serving for the model. 35 | 3. Convert a model to TensorFlow Lite and evaluate it. 36 | 4. Prepare a model for TensorFlow.js deployment. 37 | 5. Visualize predictions from a deployed model. 38 | 39 | ## 💡 Interview Tips 40 | - **Common Questions**: 41 | - What is the SavedModel format? 42 | - How does TensorFlow Serving handle requests? 43 | - Why use TensorFlow Lite for edge devices? 44 | - **Tips**: 45 | - Explain SavedModel’s portability. 46 | - Highlight TensorFlow Lite’s quantization benefits. 47 | - Be ready to describe a deployment pipeline. 48 | 49 | ## 📚 Resources 50 | - [TensorFlow SavedModel Guide](https://www.tensorflow.org/guide/saved_model) 51 | - [TensorFlow Serving](https://www.tensorflow.org/tfx/guide/serving) 52 | - [TensorFlow Lite](https://www.tensorflow.org/lite) 53 | - [TensorFlow.js](https://www.tensorflow.org/js) -------------------------------------------------------------------------------- /Tensorflow Fundamentals/03 Advanced TensorFlow Concepts/03 Custom Extensions/README.md: -------------------------------------------------------------------------------- 1 | # Custom Extensions (`tensorflow`) 2 | 3 | ## 📖 Introduction 4 | Custom extensions in TensorFlow allow tailored functionality. This guide covers custom gradient functions, TensorFlow Addons-inspired losses, and custom optimizers, with practical examples and interview insights. 5 | 6 | ## 🎯 Learning Objectives 7 | - Implement custom gradient functions with `@tf.custom_gradient`. 8 | - Use advanced losses inspired by TensorFlow Addons. 9 | - Create custom optimizers for specialized training. 10 | 11 | ## 🔑 Key Concepts 12 | - **Custom Gradients**: Define custom backpropagation logic. 13 | - **TensorFlow Addons**: Advanced losses/metrics (emulated here due to deprecation). 14 | - **Custom Optimizers**: Extend `tf.keras.optimizers.Optimizer` for unique updates. 15 | 16 | ## 📝 Example Walkthrough 17 | The `custom_extensions.py` file demonstrates: 18 | 1. **Custom Gradient**: Clipping operation with custom gradients. 19 | 2. **Focal Loss**: Emulating Addons-style loss for MNIST. 20 | 3. **Custom Optimizer**: Momentum-based optimizer. 21 | 4. **Visualization**: Comparing model accuracy. 22 | 23 | Example code: 24 | ```python 25 | import tensorflow as tf 26 | @tf.custom_gradient 27 | def clip_by_value(x, clip_min, clip_max): 28 | y = tf.clip_by_value(x, clip_min, clip_max) 29 | def grad(dy): 30 | return dy * tf.where((x >= clip_min) & (x <= clip_max), 1.0, 0.0), None, None 31 | return y, grad 32 | ``` 33 | 34 | ## 🛠️ Practical Tasks 35 | 1. Implement a custom gradient for a non-linear operation. 36 | 2. Create a focal loss for MNIST classification. 37 | 3. Build a custom optimizer with momentum and test it. 38 | 4. Compare performance of custom vs. standard optimizers. 39 | 40 | ## 💡 Interview Tips 41 | - **Common Questions**: 42 | - How do you implement a custom gradient? 43 | - What is the purpose of a focal loss? 44 | - How would you design a custom optimizer? 45 | - **Tips**: 46 | - Explain `@tf.custom_gradient` structure. 47 | - Highlight focal loss for imbalanced data. 48 | - Be ready to code a custom optimizer. 49 | 50 | ## 📚 Resources 51 | - [TensorFlow Custom Gradients](https://www.tensorflow.org/guide/autodiff) 52 | - [TensorFlow Optimizers](https://www.tensorflow.org/api_docs/python/tf/keras/optimizers) 53 | - [Kaggle: TensorFlow Tutorials](https://www.kaggle.com/learn/intro-to-deep-learning) -------------------------------------------------------------------------------- /Tensorflow Fundamentals/03 Advanced TensorFlow Concepts/02 Advanced Architectures/README.md: -------------------------------------------------------------------------------- 1 | # Advanced Architectures (`tensorflow`) 2 | 3 | ## 📖 Introduction 4 | Advanced architectures push TensorFlow’s capabilities for complex tasks. This guide covers Transformers (Vision Transformers), Generative Models (VAEs), and Reinforcement Learning (TF-Agents), with practical examples and interview insights. 5 | 6 | ## 🎯 Learning Objectives 7 | - Understand Vision Transformers for image tasks. 8 | - Implement VAEs for generative modeling. 9 | - Apply TF-Agents for reinforcement learning. 10 | 11 | ## 🔑 Key Concepts 12 | - **Vision Transformers**: Patch-based attention for images. 13 | - **VAEs**: Encoder-decoder with latent space for generation. 14 | - **Reinforcement Learning**: DQN with TF-Agents for decision-making. 15 | 16 | ## 📝 Example Walkthrough 17 | The `advanced_architectures.py` file demonstrates: 18 | 1. **Vision Transformer**: Simplified ViT for CIFAR-10. 19 | 2. **VAE**: Generating CIFAR-10 images. 20 | 3. **DQN**: Training on CartPole with TF-Agents. 21 | 4. **Visualization**: Comparing original and generated images. 22 | 23 | Example code: 24 | ```python 25 | import tensorflow as tf 26 | class ViT(tf.keras.Model): 27 | def __init__(self, num_classes, patch_size, num_patches, d_model, num_heads): 28 | super().__init__() 29 | self.transformer = tf.keras.layers.MultiHeadAttention(num_heads=num_heads, key_dim=d_model) 30 | self.dense = tf.keras.layers.Dense(num_classes, activation='softmax') 31 | ``` 32 | 33 | ## 🛠️ Practical Tasks 34 | 1. Train a Vision Transformer on CIFAR-10. 35 | 2. Build a VAE for image generation and visualize outputs. 36 | 3. Implement a DQN agent for CartPole and evaluate rewards. 37 | 4. Experiment with Transformer hyperparameters (e.g., num_heads). 38 | 39 | ## 💡 Interview Tips 40 | - **Common Questions**: 41 | - How do Transformers differ from CNNs? 42 | - What is the role of the latent space in VAEs? 43 | - How does DQN balance exploration and exploitation? 44 | - **Tips**: 45 | - Explain ViT’s patch embedding process. 46 | - Highlight VAE’s reconstruction and KL losses. 47 | - Be ready to code a simple RL agent. 48 | 49 | ## 📚 Resources 50 | - [TensorFlow Transformers Guide](https://www.tensorflow.org/text/tutorials/transformer) 51 | - [TensorFlow Agents](https://www.tensorflow.org/agents) 52 | - [Kaggle: TensorFlow Tutorials](https://www.kaggle.com/learn/intro-to-deep-learning) -------------------------------------------------------------------------------- /Tensorflow Fundamentals/03 Advanced TensorFlow Concepts/01 Distributed Training/README.md: -------------------------------------------------------------------------------- 1 | # Distributed Training (`tensorflow`) 2 | 3 | ## 📖 Introduction 4 | Distributed training scales TensorFlow models across multiple devices (GPUs/TPUs). This guide covers Data Parallelism (`MirroredStrategy`), Multi-GPU/TPU Training (`TPUStrategy`), and Distributed Datasets, with practical examples and interview insights. 5 | 6 | ## 🎯 Learning Objectives 7 | - Understand data parallelism with `MirroredStrategy`. 8 | - Implement multi-GPU/TPU training with `TPUStrategy`. 9 | - Optimize datasets for distributed training. 10 | 11 | ## 🔑 Key Concepts 12 | - **Data Parallelism**: `MirroredStrategy` replicates model across GPUs with synchronized gradients. 13 | - **Multi-GPU/TPU Training**: `TPUStrategy` leverages TPUs for high-performance training. 14 | - **Distributed Datasets**: Shard datasets across devices for efficient processing. 15 | 16 | ## 📝 Example Walkthrough 17 | The `distributed_training.py` file demonstrates: 18 | 1. **Dataset**: Loading and preprocessing CIFAR-10. 19 | 2. **MirroredStrategy**: Training a CNN with data parallelism. 20 | 3. **TPUStrategy**: Training with TPU support (fallback to GPUs). 21 | 4. **Distributed Datasets**: Custom training loop with distributed datasets. 22 | 5. **Visualization**: Comparing accuracy across strategies. 23 | 24 | Example code: 25 | ```python 26 | import tensorflow as tf 27 | strategy = tf.distribute.MirroredStrategy() 28 | with strategy.scope(): 29 | model = tf.keras.Sequential([...]) 30 | model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) 31 | ``` 32 | 33 | ## 🛠️ Practical Tasks 34 | 1. Train a CNN on CIFAR-10 using `MirroredStrategy`. 35 | 2. Adapt the model for `TPUStrategy` in a cloud environment (e.g., Colab). 36 | 3. Create a distributed dataset and implement a custom training loop. 37 | 4. Compare training speed and accuracy across strategies. 38 | 39 | ## 💡 Interview Tips 40 | - **Common Questions**: 41 | - How does `MirroredStrategy` synchronize gradients? 42 | - What are the benefits of `TPUStrategy`? 43 | - How do you shard datasets for distributed training? 44 | - **Tips**: 45 | - Explain gradient aggregation in data parallelism. 46 | - Highlight TPU’s matrix multiplication efficiency. 47 | - Be ready to code a model with `MirroredStrategy`. 48 | 49 | ## 📚 Resources 50 | - [TensorFlow Distributed Training Guide](https://www.tensorflow.org/guide/distributed_training) 51 | - [TensorFlow TPU Guide](https://www.tensorflow.org/guide/tpu) 52 | - [Kaggle: TensorFlow Tutorials](https://www.kaggle.com/learn/intro-to-deep-learning) -------------------------------------------------------------------------------- /Tensorflow Fundamentals/01 Core TensorFlow Foundations/01 Tensors and Operations/README.md: -------------------------------------------------------------------------------- 1 | # Tensors and Operations (`tensorflow`) 2 | 3 | ## 📖 Introduction 4 | Tensors are the core data structure in TensorFlow, representing multi-dimensional arrays. This guide covers tensor creation, attributes, operations, CPU/GPU interoperability, and NumPy integration, with practical examples and interview insights. 5 | 6 | ## 🎯 Learning Objectives 7 | - Understand TensorFlow tensors and their properties. 8 | - Master tensor creation (`tf.constant`, `tf.zeros`, `tf.random`) and manipulation. 9 | - Perform operations like indexing, reshaping, matrix multiplication, and broadcasting. 10 | - Explore CPU/GPU interoperability and NumPy integration. 11 | 12 | ## 🔑 Key Concepts 13 | - **Tensor Creation**: Use `tf.constant`, `tf.zeros`, `tf.ones`, `tf.random` to create tensors. 14 | - **Attributes**: Shape, dtype, and device define tensor properties. 15 | - **Operations**: Indexing, reshaping, matrix multiplication (`tf.matmul`), and broadcasting. 16 | - **CPU/GPU Interoperability**: TensorFlow manages device placement (`tf.device`). 17 | - **NumPy Integration**: Seamless conversion between tensors and NumPy arrays. 18 | 19 | ## 📝 Example Walkthrough 20 | The `tensors_and_operations.py` file demonstrates: 21 | 1. **Tensor Creation**: Creating constant, zero, and random tensors. 22 | 2. **Attributes**: Inspecting shape, dtype, and device. 23 | 3. **Operations**: Indexing, reshaping, matrix multiplication, and broadcasting. 24 | 4. **Interoperability**: Running operations on CPU/GPU and converting to/from NumPy. 25 | 5. **Visualization**: Plotting a tensor as a heatmap. 26 | 27 | Example code: 28 | ```python 29 | import tensorflow as tf 30 | const_tensor = tf.constant([[1, 2], [3, 4]], dtype=tf.float32) 31 | matmul_result = tf.matmul(const_tensor, const_tensor) 32 | ``` 33 | 34 | ## 🛠️ Practical Tasks 35 | 1. Create a 2x3 tensor using `tf.random.normal` and print its shape and dtype. 36 | 2. Reshape a 4x4 tensor into a 2x8 tensor and verify the result. 37 | 3. Perform matrix multiplication on two 3x3 tensors and check the output. 38 | 4. Convert a NumPy array to a TensorFlow tensor, perform an operation, and convert back. 39 | 5. Run a matrix multiplication on CPU and GPU (if available) using `tf.device`. 40 | 41 | ## 💡 Interview Tips 42 | - **Common Questions**: 43 | - What is a TensorFlow tensor, and how does it differ from a NumPy array? 44 | - How does broadcasting work in TensorFlow? 45 | - Why is device placement important in TensorFlow? 46 | - **Tips**: 47 | - Explain tensor attributes (shape, dtype, device) clearly. 48 | - Highlight broadcasting’s role in handling shape mismatches. 49 | - Be ready to code a tensor manipulation task (e.g., extract diagonal, compute sum). 50 | 51 | ## 📚 Resources 52 | - [TensorFlow Core Guide](https://www.tensorflow.org/guide/tensor) 53 | - [TensorFlow API Documentation](https://www.tensorflow.org/api_docs/python/tf) 54 | - [Kaggle: TensorFlow Tutorials](https://www.kaggle.com/learn/intro-to-deep-learning) -------------------------------------------------------------------------------- /Tensorflow Fundamentals/01 Core TensorFlow Foundations/02 Automatic Differentiation/README.md: -------------------------------------------------------------------------------- 1 | # Automatic Differentiation (`tensorflow`) 2 | 3 | ## 📖 Introduction 4 | Automatic differentiation is a cornerstone of TensorFlow, enabling gradient-based optimization for machine learning models. This guide covers computational graphs, gradient computation with `tf.GradientTape`, gradient application using `optimizer.apply_gradients`, and no-gradient context with `tf.stop_gradient`, with practical examples and interview insights. 5 | 6 | ## 🎯 Learning Objectives 7 | - Understand computational graphs and their role in differentiation. 8 | - Master `tf.GradientTape` for gradient computation. 9 | - Apply gradients using optimizers for model training. 10 | - Use `tf.stop_gradient` to control gradient flow. 11 | 12 | ## 🔑 Key Concepts 13 | - **Computational Graphs**: Track operations for automatic differentiation. 14 | - **tf.GradientTape**: Records operations dynamically to compute gradients. 15 | - **Gradient Application**: Optimizers (e.g., SGD, Adam) update variables using gradients. 16 | - **tf.stop_gradient**: Prevents gradients from flowing through specified tensors. 17 | 18 | ## 📝 Example Walkthrough 19 | The `automatic_differentiation.py` file demonstrates: 20 | 1. **Computational Graphs**: Computing gradients for a polynomial. 21 | 2. **Gradient Computation**: Gradients for a linear layer using `tf.GradientTape`. 22 | 3. **Higher-Order Gradients**: Second derivatives with nested tapes. 23 | 4. **Gradient Application**: Optimizing a linear regression model. 24 | 5. **No-Gradient Context**: Using `tf.stop_gradient` to block gradient flow. 25 | 6. **Visualization**: Plotting loss curves and model fits. 26 | 27 | Example code: 28 | ```python 29 | import tensorflow as tf 30 | x = tf.Variable(3.0) 31 | with tf.GradientTape() as tape: 32 | y = x**2 33 | dy_dx = tape.gradient(y, x) 34 | ``` 35 | 36 | ## 🛠️ Practical Tasks 37 | 1. Compute the gradient of `y = x^3 + 2x` at `x = 2` using `tf.GradientTape`. 38 | 2. Train a linear regression model with `tf.GradientTape` and Adam optimizer. 39 | 3. Use `tf.stop_gradient` to block gradients for a portion of a computation graph. 40 | 4. Compute second-order gradients for `y = x^4` and verify the result. 41 | 5. Debug a case where gradients are `None` due to non-differentiable operations. 42 | 43 | ## 💡 Interview Tips 44 | - **Common Questions**: 45 | - How does `tf.GradientTape` work in TensorFlow? 46 | - What causes gradients to be `None`, and how do you debug it? 47 | - When would you use `tf.stop_gradient`? 48 | - **Tips**: 49 | - Explain the dynamic computation graph in `tf.GradientTape`. 50 | - Highlight common gradient issues (e.g., non-differentiable ops like `tf.cast`). 51 | - Be ready to code a gradient computation for a simple function or a training loop. 52 | 53 | ## 📚 Resources 54 | - [TensorFlow Automatic Differentiation Guide](https://www.tensorflow.org/guide/autodiff) 55 | - [TensorFlow API Documentation](https://www.tensorflow.org/api_docs/python/tf/GradientTape) 56 | - [Kaggle: TensorFlow Tutorials](https://www.kaggle.com/learn/intro-to-deep-learning) -------------------------------------------------------------------------------- /Tensorflow Fundamentals/02 Intermediate TensorFlow Concepts/03 Optimization/README.md: -------------------------------------------------------------------------------- 1 | # Optimization (`tensorflow`) 2 | 3 | ## 📖 Introduction 4 | Optimization in TensorFlow enhances model performance and efficiency. This guide covers hyperparameter tuning (learning rate, batch size), regularization (dropout, L2), mixed precision training (`tf.keras.mixed_precision`), and model quantization, with practical examples and interview insights. 5 | 6 | ## 🎯 Learning Objectives 7 | - Understand hyperparameter tuning for learning rate and batch size. 8 | - Apply regularization techniques like dropout and L2. 9 | - Implement mixed precision training for faster computation. 10 | - Perform model quantization for efficient deployment. 11 | 12 | ## 🔑 Key Concepts 13 | - **Hyperparameter Tuning**: Optimize learning rate and batch size for better accuracy. 14 | - **Regularization**: Use dropout and L2 to prevent overfitting. 15 | - **Mixed Precision Training**: Use `mixed_float16` for faster training on GPUs. 16 | - **Model Quantization**: Reduce model size and latency with TensorFlow Lite. 17 | 18 | ## 📝 Example Walkthrough 19 | The `optimization.py` file demonstrates: 20 | 1. **Dataset**: Loading and preprocessing CIFAR-10. 21 | 2. **Hyperparameter Tuning**: Testing learning rates and batch sizes. 22 | 3. **Regularization**: Applying dropout and L2 to a CNN. 23 | 4. **Mixed Precision**: Training with `mixed_float16` policy. 24 | 5. **Quantization**: Converting a model to TensorFlow Lite. 25 | 6. **Visualization**: Comparing accuracy and visualizing tuning results. 26 | 27 | Example code: 28 | ```python 29 | import tensorflow as tf 30 | import tensorflow.keras.mixed_precision as mixed_precision 31 | policy = mixed_precision.Policy('mixed_float16') 32 | mixed_precision.set_global_policy(policy) 33 | model = tf.keras.Sequential([...]) 34 | model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) 35 | ``` 36 | 37 | ## 🛠️ Practical Tasks 38 | 1. Tune learning rate and batch size for a CNN on CIFAR-10 and select the best combination. 39 | 2. Add dropout and L2 regularization to a model and compare performance. 40 | 3. Train a model with mixed precision and measure training time savings. 41 | 4. Quantize a trained model with TensorFlow Lite and evaluate its accuracy. 42 | 5. Visualize hyperparameter tuning results using a scatter plot. 43 | 44 | ## 💡 Interview Tips 45 | - **Common Questions**: 46 | - How do you choose an optimal learning rate? 47 | - What is the benefit of mixed precision training? 48 | - Why would you quantize a model? 49 | - **Tips**: 50 | - Explain dropout’s role in reducing overfitting. 51 | - Highlight mixed precision’s speed and memory benefits. 52 | - Be ready to code a model with regularization or quantization. 53 | 54 | ## 📚 Resources 55 | - [TensorFlow Optimization Guide](https://www.tensorflow.org/guide/keras/training_with_built_in_methods) 56 | - [TensorFlow Mixed Precision Guide](https://www.tensorflow.org/guide/mixed_precision) 57 | - [TensorFlow Lite Guide](https://www.tensorflow.org/lite) 58 | - [Kaggle: TensorFlow Tutorials](https://www.kaggle.com/learn/intro-to-deep-learning) -------------------------------------------------------------------------------- /Tensorflow Fundamentals/02 Intermediate TensorFlow Concepts/01 Model Architectures/README.md: -------------------------------------------------------------------------------- 1 | # Model Architectures (`tensorflow`) 2 | 3 | ## 📖 Introduction 4 | TensorFlow supports a variety of neural network architectures tailored to specific tasks. This guide covers Feedforward Neural Networks (FNNs), Convolutional Neural Networks (CNNs), Recurrent Neural Networks (RNNs, LSTMs, GRUs), and Transfer Learning with `tf.keras.applications`, with practical examples and interview insights. 5 | 6 | ## 🎯 Learning Objectives 7 | - Understand the structure and use cases of FNNs, CNNs, RNNs, and transfer learning. 8 | - Master building and training these architectures with `tf.keras`. 9 | - Apply transfer learning using pre-trained models. 10 | - Compare architectures for different data types and tasks. 11 | 12 | ## 🔑 Key Concepts 13 | - **FNNs**: Fully connected layers for tabular data or simple tasks. 14 | - **CNNs**: Convolutional and pooling layers for image data. 15 | - **RNNs (LSTMs, GRUs)**: Recurrent layers for sequential or time-series data. 16 | - **Transfer Learning**: Use pre-trained models (e.g., MobileNetV2) for efficient training. 17 | 18 | ## 📝 Example Walkthrough 19 | The `model_architectures.py` file demonstrates: 20 | 1. **FNN**: Classification on MNIST with Dense layers. 21 | 2. **CNN**: Classification on MNIST with Conv2D and MaxPooling2D. 22 | 3. **RNNs**: LSTM and GRU for synthetic sequence classification. 23 | 4. **Transfer Learning**: Fine-tuning MobileNetV2 on CIFAR-10. 24 | 5. **Visualization**: Comparing validation accuracy and visualizing CNN predictions. 25 | 26 | Example code: 27 | ```python 28 | import tensorflow as tf 29 | cnn_model = tf.keras.Sequential([ 30 | tf.keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)), 31 | tf.keras.layers.MaxPooling2D((2, 2)), 32 | tf.keras.layers.Flatten(), 33 | tf.keras.layers.Dense(10, activation='softmax') 34 | ]) 35 | cnn_model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) 36 | ``` 37 | 38 | ## 🛠️ Practical Tasks 39 | 1. Build an FNN for MNIST classification and evaluate its accuracy. 40 | 2. Create a CNN for MNIST with additional Conv2D layers and compare performance. 41 | 3. Train an LSTM or GRU on a synthetic sequence dataset for binary classification. 42 | 4. Fine-tune a pre-trained MobileNetV2 model on CIFAR-10 and evaluate improvements. 43 | 5. Visualize predictions and compare parameter counts across architectures. 44 | 45 | ## 💡 Interview Tips 46 | - **Common Questions**: 47 | - When would you use a CNN over an FNN? 48 | - What are the differences between LSTMs and GRUs? 49 | - How does transfer learning improve training efficiency? 50 | - **Tips**: 51 | - Explain CNN’s spatial feature extraction vs. FNN’s fully connected layers. 52 | - Highlight LSTM’s memory cells for long-term dependencies. 53 | - Be ready to code a simple CNN or fine-tune a pre-trained model. 54 | 55 | ## 📚 Resources 56 | - [TensorFlow Keras Guide](https://www.tensorflow.org/guide/keras) 57 | - [TensorFlow Applications Documentation](https://www.tensorflow.org/api_docs/python/tf/keras/applications) 58 | - [Kaggle: TensorFlow Tutorials](https://www.kaggle.com/learn/intro-to-deep-learning) 59 | - [TensorFlow Official Tutorials](https://www.tensorflow.org/tutorials) -------------------------------------------------------------------------------- /Tensorflow Fundamentals/04 Specialized TensorFlow Libraries/README.md: -------------------------------------------------------------------------------- 1 | # Specialized TensorFlow Libraries (`tensorflow`) 2 | 3 | ## 📖 Introduction 4 | TensorFlow’s specialized libraries streamline data handling, model reuse, and deployment. This guide covers **TensorFlow Datasets**, **TensorFlow Hub**, **Keras**, **TensorFlow Lite**, and **TensorFlow.js**, with practical examples and interview insights. 5 | 6 | ## 🎯 Learning Objectives 7 | - Load and preprocess datasets with TensorFlow Datasets. 8 | - Use pre-trained models from TensorFlow Hub for transfer learning. 9 | - Build models rapidly with Keras. 10 | - Deploy models on edge devices with TensorFlow Lite. 11 | - Prepare models for browser-based inference with TensorFlow.js. 12 | 13 | ## 🔑 Key Concepts 14 | - **TensorFlow Datasets**: Curated, ready-to-use datasets with `tfds.load`. 15 | - **TensorFlow Hub**: Pre-trained models for transfer learning via `hub.KerasLayer`. 16 | - **Keras**: High-level API for quick model prototyping. 17 | - **TensorFlow Lite**: Lightweight models for mobile and edge devices. 18 | - **TensorFlow.js**: JavaScript-based ML for browser environments. 19 | 20 | ## 📝 Example Walkthrough 21 | The `specialized_libraries.py` file demonstrates: 22 | 1. **TensorFlow Datasets**: Loading CIFAR-10 with preprocessing. 23 | 2. **TensorFlow Hub**: Transfer learning with MobileNetV2. 24 | 3. **Keras**: Building a CNN for CIFAR-10. 25 | 4. **TensorFlow Lite**: Converting and evaluating a model. 26 | 5. **TensorFlow.js**: Instructions for browser deployment. 27 | 6. **Visualization**: Dataset samples and model predictions. 28 | 29 | Example code: 30 | ```python 31 | import tensorflow as tf 32 | import tensorflow_datasets as tfds 33 | ds, info = tfds.load('cifar10', with_info=True, as_supervised=True) 34 | train_ds = ds['train'].map(lambda x, y: (x / 255.0, tf.one_hot(y, 10))).batch(32) 35 | ``` 36 | 37 | ## 🛠️ Practical Tasks 38 | 1. Load a dataset (e.g., CIFAR-10) using TensorFlow Datasets and preprocess it. 39 | 2. Use a TensorFlow Hub model for transfer learning on CIFAR-10. 40 | 3. Build and train a Keras model for image classification. 41 | 4. Convert a Keras model to TensorFlow Lite and evaluate its accuracy. 42 | 5. Prepare a model for TensorFlow.js and outline browser deployment steps. 43 | 6. Combine TensorFlow Datasets, Hub, and Keras in a transfer learning workflow. 44 | 45 | ## 💡 Interview Tips 46 | - **Common Questions**: 47 | - How does TensorFlow Datasets simplify data preprocessing? 48 | - What are the benefits of using TensorFlow Hub for transfer learning? 49 | - Why choose TensorFlow Lite over a full TensorFlow model for edge devices? 50 | - **Tips**: 51 | - Explain `tfds.load` and its preprocessing pipeline. 52 | - Highlight Keras’s prototyping speed and TensorFlow Lite’s efficiency. 53 | - Be ready to code a transfer learning model with TensorFlow Hub or convert to TensorFlow Lite. 54 | 55 | ## 📚 Resources 56 | - [TensorFlow Datasets Guide](https://www.tensorflow.org/datasets) 57 | - [TensorFlow Hub Guide](https://www.tensorflow.org/hub) 58 | - [Keras Guide](https://www.tensorflow.org/guide/keras) 59 | - [TensorFlow Lite Guide](https://www.tensorflow.org/lite) 60 | - [TensorFlow.js Guide](https://www.tensorflow.org/js) 61 | - [Kaggle: TensorFlow Tutorials](https://www.kaggle.com/learn/intro-to-deep-learning) -------------------------------------------------------------------------------- /Tensorflow Fundamentals/01 Core TensorFlow Foundations/03 Neural Networks (tf.keras)/README.md: -------------------------------------------------------------------------------- 1 | # Neural Networks (`tf.keras`) 2 | 3 | ## 📖 Introduction 4 | `tf.keras` is TensorFlow’s high-level API for building and training neural networks. This guide covers defining models (`tf.keras.Sequential`, `tf.keras.Model`), layers (Dense, Convolutional, Pooling, Normalization), activations (ReLU, Sigmoid, Softmax), loss functions (MSE, Categorical Crossentropy), optimizers (SGD, Adam, RMSprop), and learning rate schedules, with practical examples and interview insights. 5 | 6 | ## 🎯 Learning Objectives 7 | - Understand `tf.keras` for building neural networks. 8 | - Master model definition with `Sequential` and `Model` APIs. 9 | - Apply layers, activations, loss functions, and optimizers. 10 | - Implement learning rate schedules for improved training. 11 | 12 | ## 🔑 Key Concepts 13 | - **Model Definition**: `tf.keras.Sequential` for linear stacks, `tf.keras.Model` for custom architectures. 14 | - **Layers**: Dense (fully connected), Conv2D (convolutional), MaxPooling2D (pooling), BatchNormalization. 15 | - **Activations**: ReLU (non-linearity), Sigmoid (binary), Softmax (multi-class). 16 | - **Loss Functions**: MSE (regression), Categorical Crossentropy (classification). 17 | - **Optimizers**: SGD (gradient descent), Adam (adaptive), RMSprop (momentum-based). 18 | - **Learning Rate Schedules**: Adjust learning rate dynamically (e.g., exponential decay). 19 | 20 | ## 📝 Example Walkthrough 21 | The `neural_networks_keras.py` file demonstrates: 22 | 1. **Sequential Model**: Regression with Dense layers. 23 | 2. **Custom Model**: Classification using `tf.keras.Model` subclassing. 24 | 3. **CNN**: Image classification with Conv2D, Pooling, and BatchNormalization. 25 | 4. **Activations and Losses**: ReLU, Sigmoid, Softmax, MSE, and Crossentropy. 26 | 5. **Optimizers**: Comparing SGD, Adam, RMSprop. 27 | 6. **Learning Rate Schedules**: Exponential decay for regression. 28 | 7. **Visualization**: Plotting training loss curves. 29 | 30 | Example code: 31 | ```python 32 | import tensorflow as tf 33 | model = tf.keras.Sequential([ 34 | tf.keras.layers.Dense(64, activation='relu', input_shape=(5,)), 35 | tf.keras.layers.Dense(1) 36 | ]) 37 | model.compile(optimizer='adam', loss='mse') 38 | ``` 39 | 40 | ## 🛠️ Practical Tasks 41 | 1. Build a `Sequential` model for regression on synthetic data and evaluate MSE. 42 | 2. Create a custom `tf.keras.Model` for multi-class classification and train it. 43 | 3. Design a CNN with Conv2D, MaxPooling2D, and BatchNormalization for image data. 44 | 4. Compare SGD, Adam, and RMSprop on a regression task. 45 | 5. Implement an exponential decay learning rate schedule and plot the loss curve. 46 | 47 | ## 💡 Interview Tips 48 | - **Common Questions**: 49 | - What is the difference between `Sequential` and `Model` APIs? 50 | - When would you use ReLU vs. Sigmoid? 51 | - How does Adam differ from SGD? 52 | - **Tips**: 53 | - Explain the role of activations in introducing non-linearity. 54 | - Highlight Adam’s adaptive learning rate for faster convergence. 55 | - Be ready to code a simple neural network or CNN architecture. 56 | 57 | ## 📚 Resources 58 | - [TensorFlow Keras Guide](https://www.tensorflow.org/guide/keras) 59 | - [TensorFlow API Documentation](https://www.tensorflow.org/api_docs/python/tf/keras) 60 | - [Kaggle: TensorFlow Tutorials](https://www.kaggle.com/learn/intro-to-deep-learning) -------------------------------------------------------------------------------- /Tensorflow Fundamentals/01 Core TensorFlow Foundations/04 Datasets and Data Loading/README.md: -------------------------------------------------------------------------------- 1 | # Datasets and Data Loading (`tensorflow`) 2 | 3 | ## 📖 Introduction 4 | Efficient data loading and preprocessing are essential for machine learning pipelines. This guide covers built-in datasets (`tf.keras.datasets`), TensorFlow Datasets (`tfds.load`), data pipelines (`tf.data.Dataset`, map, batch, shuffle), preprocessing (`tf.keras.preprocessing`), and handling large datasets, with practical examples and interview insights. 5 | 6 | ## 🎯 Learning Objectives 7 | - Understand TensorFlow’s dataset loading mechanisms. 8 | - Master `tf.data.Dataset` for efficient data pipelines. 9 | - Apply preprocessing and data augmentation with `tf.keras.preprocessing`. 10 | - Handle large datasets with optimized pipelines. 11 | 12 | ## 🔑 Key Concepts 13 | - **Built-in Datasets**: `tf.keras.datasets` provides datasets like MNIST. 14 | - **TensorFlow Datasets**: `tfds.load` accesses curated datasets (e.g., CIFAR-10). 15 | - **Data Pipeline**: `tf.data.Dataset` supports transformations (map, batch, shuffle, prefetch). 16 | - **Preprocessing**: `tf.keras.preprocessing` for data augmentation (e.g., rotation, zoom). 17 | - **Large Datasets**: Optimize pipelines with caching, prefetching, and parallel processing. 18 | 19 | ## 📝 Example Walkthrough 20 | The `datasets_and_data_loading.py` file demonstrates: 21 | 1. **Built-in Datasets**: Loading and normalizing MNIST. 22 | 2. **TensorFlow Datasets**: Loading CIFAR-10 with `tfds.load` (if installed). 23 | 3. **Data Pipeline**: Creating a `tf.data.Dataset` pipeline for MNIST with shuffle, batch, and augmentation. 24 | 4. **Preprocessing**: Applying `tf.keras.preprocessing` for image augmentation. 25 | 5. **Large Datasets**: Building an efficient pipeline for synthetic data. 26 | 6. **Visualization**: Comparing original vs. augmented images and plotting training progress. 27 | 28 | Example code: 29 | ```python 30 | import tensorflow as tf 31 | (x_train, y_train), _ = tf.keras.datasets.mnist.load_data() 32 | dataset = tf.data.Dataset.from_tensor_slices((x_train, y_train)) 33 | dataset = dataset.shuffle(1000).batch(32).prefetch(tf.data.AUTOTUNE) 34 | ``` 35 | 36 | ## 🛠️ Practical Tasks 37 | 1. Load MNIST using `tf.keras.datasets` and normalize the data. 38 | 2. Create a `tf.data.Dataset` pipeline with shuffle, batch, and a custom preprocessing function. 39 | 3. Apply data augmentation (e.g., rotation, flip) using `tf.keras.preprocessing`. 40 | 4. Build an optimized pipeline for a large synthetic dataset with caching and prefetching. 41 | 5. Train a CNN using a `tf.data.Dataset` pipeline and evaluate its performance. 42 | 43 | ## 💡 Interview Tips 44 | - **Common Questions**: 45 | - How does `tf.data.Dataset` improve training efficiency? 46 | - What is the purpose of prefetching and caching? 47 | - When would you use `tf.keras.preprocessing` for augmentation? 48 | - **Tips**: 49 | - Explain the role of `shuffle`, `batch`, and `prefetch` in pipelines. 50 | - Highlight optimization techniques like `cache()` for small datasets. 51 | - Be ready to code a data pipeline with preprocessing and augmentation. 52 | 53 | ## 📚 Resources 54 | - [TensorFlow Data Guide](https://www.tensorflow.org/guide/data) 55 | - [TensorFlow Datasets Documentation](https://www.tensorflow.org/datasets) 56 | - [TensorFlow Keras Preprocessing](https://www.tensorflow.org/api_docs/python/tf/keras/preprocessing) 57 | - [Kaggle: TensorFlow Tutorials](https://www.kaggle.com/learn/intro-to-deep-learning) -------------------------------------------------------------------------------- /Tensorflow Fundamentals/01 Core TensorFlow Foundations/05 Training Pipeline/README.md: -------------------------------------------------------------------------------- 1 | # Training Pipeline (`tensorflow`) 2 | 3 | ## 📖 Introduction 4 | A training pipeline orchestrates model training, evaluation, checkpointing, and monitoring in TensorFlow. This guide covers training/evaluation loops, model checkpointing (`model.save`, `model.load`), GPU/TPU training (`tf.device`), and monitoring with TensorBoard, with practical examples and interview insights. 5 | 6 | ## 🎯 Learning Objectives 7 | - Understand TensorFlow’s training and evaluation loops. 8 | - Master model checkpointing for saving and loading models. 9 | - Implement GPU/TPU training with `tf.device`. 10 | - Monitor training with TensorBoard for performance analysis. 11 | 12 | ## 🔑 Key Concepts 13 | - **Training/Evaluation Loops**: Custom loops using `tf.GradientTape` for fine-grained control. 14 | - **Model Checkpointing**: Save and load models with `model.save` and `model.load`. 15 | - **GPU/TPU Training**: Use `tf.device` to leverage hardware accelerators. 16 | - **TensorBoard**: Visualize metrics (loss, accuracy) during training. 17 | 18 | ## 📝 Example Walkthrough 19 | The `training_pipeline.py` file demonstrates: 20 | 1. **Dataset**: Loading and preprocessing MNIST with `tf.data.Dataset`. 21 | 2. **Model**: Building a CNN for digit classification. 22 | 3. **Training Loop**: Custom loop with `tf.GradientTape` and TensorBoard logging. 23 | 4. **Checkpointing**: Saving and loading the model. 24 | 5. **GPU/TPU Training**: Training on available hardware using `tf.device`. 25 | 6. **TensorBoard**: Logging metrics for visualization. 26 | 7. **Visualization**: Plotting training loss and accuracy. 27 | 28 | Example code: 29 | ```python 30 | import tensorflow as tf 31 | model = tf.keras.Sequential([...]) 32 | optimizer = tf.keras.optimizers.Adam() 33 | loss_fn = tf.keras.losses.SparseCategoricalCrossentropy() 34 | 35 | @tf.function 36 | def train_step(x, y): 37 | with tf.GradientTape() as tape: 38 | logits = model(x, training=True) 39 | loss = loss_fn(y, logits) 40 | gradients = tape.gradient(loss, model.trainable_variables) 41 | optimizer.apply_gradients(zip(gradients, model.trainable_variables)) 42 | return loss 43 | ``` 44 | 45 | ## 🛠️ Practical Tasks 46 | 1. Implement a custom training loop for a CNN on MNIST using `tf.GradientTape`. 47 | 2. Save and load a trained model using `model.save` and `model.load`. 48 | 3. Train a model on GPU/CPU using `tf.device` and verify device placement. 49 | 4. Set up TensorBoard to monitor loss and accuracy during training. 50 | 5. Use `ModelCheckpoint` callback to save the best model based on validation accuracy. 51 | 52 | ## 💡 Interview Tips 53 | - **Common Questions**: 54 | - How does a custom training loop differ from `model.fit`? 55 | - What is the purpose of `ModelCheckpoint` callback? 56 | - How do you ensure efficient GPU training in TensorFlow? 57 | - **Tips**: 58 | - Explain the role of `tf.GradientTape` in custom loops. 59 | - Highlight TensorBoard’s utility for debugging and optimization. 60 | - Be ready to code a training loop or set up TensorBoard logging. 61 | 62 | ## 📚 Resources 63 | - [TensorFlow Training Guide](https://www.tensorflow.org/guide/keras/training_with_built_in_methods) 64 | - [TensorFlow TensorBoard Guide](https://www.tensorflow.org/tensorboard) 65 | - [TensorFlow API Documentation](https://www.tensorflow.org/api_docs/python/tf) 66 | - [Kaggle: TensorFlow Tutorials](https://www.kaggle.com/learn/intro-to-deep-learning) -------------------------------------------------------------------------------- /Tensorflow Fundamentals/02 Intermediate TensorFlow Concepts/02 Customization/README.md: -------------------------------------------------------------------------------- 1 | # Customization (`tensorflow`) 2 | 3 | ## 📖 Introduction 4 | TensorFlow’s customization capabilities enable flexible model design and optimization. This guide covers custom layers and loss functions, Functional and Subclassing APIs, and debugging gradient issues, with practical examples and interview insights. 5 | 6 | ## 🎯 Learning Objectives 7 | - Understand how to create custom layers and loss functions. 8 | - Master Functional and Subclassing APIs for complex models. 9 | - Learn to debug gradient issues in TensorFlow. 10 | 11 | ## 🔑 Key Concepts 12 | - **Custom Layers**: Extend `tf.keras.layers.Layer` for specialized operations. 13 | - **Custom Loss Functions**: Define task-specific loss functions. 14 | - **Functional API**: Build models with flexible, non-sequential architectures. 15 | - **Subclassing API**: Create fully customizable models via class inheritance. 16 | - **Gradient Debugging**: Identify and fix issues like `None` gradients. 17 | 18 | ## 📝 Example Walkthrough 19 | The `customization.py` file demonstrates: 20 | 1. **Custom Layer**: A `ScaledDense` layer with a learnable scaling factor. 21 | 2. **Custom Loss**: Weighted categorical crossentropy for class imbalance. 22 | 3. **Functional API**: A CNN with explicit input-output connections. 23 | 4. **Subclassing API**: A custom CNN with modular layers. 24 | 5. **Gradient Debugging**: Handling non-differentiable ops and disconnected graphs. 25 | 6. **Visualization**: Comparing model accuracy and gradient norms. 26 | 27 | Example code: 28 | ```python 29 | import tensorflow as tf 30 | class ScaledDense(tf.keras.layers.Layer): 31 | def __init__(self, units, activation=None): 32 | super().__init__() 33 | self.units = units 34 | self.activation = tf.keras.activations.get(activation) 35 | 36 | def build(self, input_shape): 37 | self.dense = tf.keras.layers.Dense(self.units) 38 | self.scale = self.add_weight('scale', shape=(), initializer='ones', trainable=True) 39 | 40 | def call(self, inputs): 41 | x = self.dense(inputs) 42 | x = x * self.scale 43 | return self.activation(x) if self.activation else x 44 | ``` 45 | 46 | ## 🛠️ Practical Tasks 47 | 1. Create a custom layer that applies a polynomial transformation and test it on MNIST. 48 | 2. Implement a custom loss function that penalizes specific classes and train a model. 49 | 3. Build a model using the Functional API with multiple branches. 50 | 4. Use the Subclassing API to create a CNN with custom logic. 51 | 5. Debug a `None` gradient issue caused by a non-differentiable operation. 52 | 53 | ## 💡 Interview Tips 54 | - **Common Questions**: 55 | - How do you implement a custom layer in TensorFlow? 56 | - What causes `None` gradients, and how do you debug them? 57 | - When would you use the Functional API over Subclassing? 58 | - **Tips**: 59 | - Explain the `build` and `call` methods in custom layers. 60 | - Highlight common gradient issues (e.g., `tf.cast`, `tf.stop_gradient`). 61 | - Be ready to code a custom layer or debug a gradient issue. 62 | 63 | ## 📚 Resources 64 | - [TensorFlow Custom Layers Guide](https://www.tensorflow.org/guide/keras/custom_layers_and_models) 65 | - [TensorFlow Functional API Guide](https://www.tensorflow.org/guide/keras/functional_api) 66 | - [TensorFlow Gradient Debugging](https://www.tensorflow.org/guide/autodiff) 67 | - [Kaggle: TensorFlow Tutorials](https://www.kaggle.com/learn/intro-to-deep-learning) -------------------------------------------------------------------------------- /Tensorflow Fundamentals/03 Advanced TensorFlow Concepts/04 Deployment/deployment.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | import numpy as np 3 | from tensorflow.keras.datasets import mnist 4 | import os 5 | 6 | # %% [1. Introduction to Deployment] 7 | # Deployment involves exporting, serving, and running TensorFlow models in production. 8 | # Covers SavedModel, TensorFlow Serving, and TensorFlow Lite/JS. 9 | 10 | print("TensorFlow version:", tf.__version__) 11 | 12 | # %% [2. Preparing the Dataset and Model] 13 | # Load and preprocess MNIST dataset. 14 | (x_train, y_train), (x_test, y_test) = mnist.load_data() 15 | x_train = x_train.astype('float32') / 255.0 16 | x_test = x_test.astype('float32') / 255.0 17 | x_train = x_train[..., np.newaxis] 18 | x_test = x_test[..., np.newaxis] 19 | train_ds = tf.data.Dataset.from_tensor_slices((x_train, y_train)).shuffle(1000).batch(32).prefetch(tf.data.AUTOTUNE) 20 | test_ds = tf.data.Dataset.from_tensor_slices((x_test, y_test)).batch(32).prefetch(tf.data.AUTOTUNE) 21 | print("\nMNIST Dataset:") 22 | print("Train Shape:", x_train.shape, "Test Shape:", x_test.shape) 23 | 24 | # Train a simple CNN 25 | model = tf.keras.Sequential([ 26 | tf.keras.layers.Conv2D(16, (3, 3), activation='relu', input_shape=(28, 28, 1)), 27 | tf.keras.layers.MaxPooling2D((2, 2)), 28 | tf.keras.layers.Flatten(), 29 | tf.keras.layers.Dense(64, activation='relu'), 30 | tf.keras.layers.Dense(10, activation='softmax') 31 | ]) 32 | model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) 33 | model.fit(train_ds, epochs=3, validation_data=test_ds, verbose=1) 34 | print("\nModel Test Accuracy:", model.evaluate(test_ds, verbose=0)[1].round(4)) 35 | 36 | # %% [3. Model Export: SavedModel] 37 | # Export the model as SavedModel. 38 | saved_model_path = "saved_model/mnist_cnn" 39 | model.save(saved_model_path) 40 | print("\nSavedModel Exported to:", saved_model_path) 41 | 42 | # Load and test SavedModel 43 | loaded_model = tf.keras.models.load_model(saved_model_path) 44 | print("Loaded SavedModel Test Accuracy:", loaded_model.evaluate(test_ds, verbose=0)[1].round(4)) 45 | 46 | # %% [4. Serving with TensorFlow Serving] 47 | # Note: TensorFlow Serving requires separate installation and setup. 48 | # Instructions for serving the SavedModel. 49 | print("\nTensorFlow Serving Instructions:") 50 | print("1. Install TensorFlow Serving: `docker pull tensorflow/serving`") 51 | print(f"2. Serve model: `docker run -p 8501:8501 --mount type=bind,source={os.path.abspath(saved_model_path)},target=/models/mnist_cnn -e MODEL_NAME=mnist_cnn -t tensorflow/serving`") 52 | print("3. Query model: Use REST API at http://localhost:8501/v1/models/mnist_cnn:predict") 53 | 54 | # %% [5. Edge Deployment: TensorFlow Lite] 55 | # Convert model to TensorFlow Lite. 56 | converter = tf.lite.TFLiteConverter.from_keras_model(model) 57 | tflite_model = converter.convert() 58 | tflite_path = "mnist_cnn.tflite" 59 | with open(tflite_path, 'wb') as f: 60 | f.write(tflite_model) 61 | print("\nTensorFlow Lite Model Saved to:", tflite_path) 62 | 63 | # Evaluate TFLite model 64 | interpreter = tf.lite.Interpreter(model_path=tflite_path) 65 | interpreter.allocate_tensors() 66 | input_index = interpreter.get_input_details()[0]['index'] 67 | output_index = interpreter.get_output_details()[0]['index'] 68 | correct = 0 69 | total = 0 70 | for x, y in test_ds.unbatch().take(100): 71 | x = x.numpy()[np.newaxis, ...] 72 | interpreter.set_tensor(input_index, x) 73 | interpreter.invoke() 74 | pred = interpreter.get_tensor(output_index) 75 | if np.argmax(pred) == y: 76 | correct += 1 77 | total += 1 78 | print("TFLite Model Accuracy (Subset):", (correct / total).round(4)) 79 | 80 | # %% [6. Edge Deployment: TensorFlow.js] 81 | # Note: TensorFlow.js conversion requires `tensorflowjs` package. 82 | print("\nTensorFlow.js Conversion Instructions:") 83 | print("1. Install: `pip install tensorflowjs`") 84 | print(f"2. Convert: `tensorflowjs_converter --input_format=tf_saved_model {saved_model_path} tfjs_model`") 85 | print("3. Use in browser: Load `tfjs_model/model.json` with TensorFlow.js") 86 | 87 | # %% [7. Visualizing Predictions] 88 | # Visualize predictions from the original model. 89 | predictions = model.predict(x_test[:5]) 90 | plt.figure(figsize=(15, 3)) 91 | for i in range(5): 92 | plt.subplot(1, 5, i + 1) 93 | plt.imshow(x_test[i, :, :, 0], cmap='gray') 94 | plt.title(f"Pred: {np.argmax(predictions[i])}\nTrue: {y_test[i]}") 95 | plt.axis('off') 96 | plt.savefig('deployment_predictions.png') 97 | 98 | # %% [8. Interview Scenario: Model Deployment] 99 | # Discuss deploying a model for production. 100 | print("\nInterview Scenario: Model Deployment") 101 | print("1. SavedModel: Standard format for TensorFlow Serving.") 102 | print("2. TensorFlow Serving: Scalable REST/gRPC API for production.") 103 | print("3. TensorFlow Lite: Lightweight for mobile/edge devices.") 104 | print("4. TensorFlow.js: Browser-based inference with WebGL.") -------------------------------------------------------------------------------- /Tensorflow Fundamentals/01 Core TensorFlow Foundations/01 Tensors and Operations/tensors_and_operations.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | import numpy as np 3 | 4 | # %% [1. Introduction to Tensors and Operations] 5 | # Tensors are multi-dimensional arrays, the core data structure in TensorFlow. 6 | # TensorFlow provides functions for tensor creation, manipulation, and operations. 7 | 8 | print("TensorFlow version:", tf.__version__) 9 | 10 | # %% [2. Tensor Creation] 11 | # Create tensors using tf.constant, tf.zeros, tf.ones, and tf.random. 12 | # tf.constant: Creates a tensor from a fixed value. 13 | const_tensor = tf.constant([[1, 2], [3, 4]], dtype=tf.float32) 14 | print("\nConstant Tensor:") 15 | print(const_tensor) 16 | 17 | # tf.zeros: Creates a tensor filled with zeros. 18 | zeros_tensor = tf.zeros([2, 3], dtype=tf.int32) 19 | print("\nZeros Tensor:") 20 | print(zeros_tensor) 21 | 22 | # tf.random: Creates a tensor with random values. 23 | random_tensor = tf.random.normal([2, 2], mean=0.0, stddev=1.0, seed=42) 24 | print("\nRandom Normal Tensor:") 25 | print(random_tensor) 26 | 27 | # %% [3. Tensor Attributes] 28 | # Tensors have attributes: shape, dtype, and device. 29 | print("\nTensor Attributes for const_tensor:") 30 | print("Shape:", const_tensor.shape) 31 | print("Data Type:", const_tensor.dtype) 32 | print("Device:", const_tensor.device) 33 | 34 | # %% [4. Indexing and Slicing] 35 | # Access tensor elements using indexing and slicing. 36 | tensor = tf.constant([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) 37 | print("\nOriginal Tensor:") 38 | print(tensor) 39 | print("First Row:", tensor[0].numpy()) 40 | print("Element at [1, 2]:", tensor[1, 2].numpy()) 41 | print("Slice [0:2, 1:3]:") 42 | print(tensor[0:2, 1:3].numpy()) 43 | 44 | # %% [5. Reshaping] 45 | # Reshape tensors using tf.reshape. 46 | reshaped_tensor = tf.reshape(tensor, [1, 9]) 47 | print("\nReshaped Tensor (1x9):") 48 | print(reshaped_tensor) 49 | reshaped_tensor_2 = tf.reshape(tensor, [9, 1]) 50 | print("Reshaped Tensor (9x1):") 51 | print(reshaped_tensor_2) 52 | 53 | # %% [6. Matrix Multiplication] 54 | # Perform matrix multiplication using tf.matmul. 55 | matrix_a = tf.constant([[1, 2], [3, 4]], dtype=tf.float32) 56 | matrix_b = tf.constant([[5, 6], [7, 8]], dtype=tf.float32) 57 | matmul_result = tf.matmul(matrix_a, matrix_b) 58 | print("\nMatrix A:") 59 | print(matrix_a) 60 | print("Matrix B:") 61 | print(matrix_b) 62 | print("Matrix Multiplication (A @ B):") 63 | print(matmul_result) 64 | 65 | # %% [7. Broadcasting] 66 | # Broadcasting allows operations on tensors of different shapes. 67 | scalar = tf.constant(2.0, dtype=tf.float32) 68 | broadcast_result = matrix_a * scalar 69 | print("\nBroadcasting (Matrix A * Scalar):") 70 | print(broadcast_result) 71 | 72 | # Example with different shapes 73 | tensor_1 = tf.constant([[1, 2, 3]], dtype=tf.float32) 74 | tensor_2 = tf.constant([[4], [5], [6]], dtype=tf.float32) 75 | broadcast_sum = tensor_1 + tensor_2 76 | print("Broadcasting (1x3 + 3x1):") 77 | print(broadcast_sum) 78 | 79 | # %% [8. CPU/GPU Interoperability] 80 | # TensorFlow automatically places tensors on GPU if available, or CPU otherwise. 81 | # Explicitly place operations on CPU or GPU using tf.device. 82 | with tf.device('/CPU:0'): 83 | cpu_tensor = tf.constant([[1, 2], [3, 4]], dtype=tf.float32) 84 | cpu_result = tf.matmul(cpu_tensor, cpu_tensor) 85 | print("\nCPU Tensor Device:", cpu_tensor.device) 86 | print("CPU Matmul Result:") 87 | print(cpu_result) 88 | 89 | if tf.config.list_physical_devices('GPU'): 90 | with tf.device('/GPU:0'): 91 | gpu_tensor = tf.constant([[1, 2], [3, 4]], dtype=tf.float32) 92 | gpu_result = tf.matmul(gpu_tensor, gpu_tensor) 93 | print("GPU Tensor Device:", gpu_tensor.device) 94 | print("GPU Matmul Result:") 95 | print(gpu_result) 96 | else: 97 | print("No GPU available, skipping GPU test.") 98 | 99 | # %% [9. NumPy Integration] 100 | # Convert between TensorFlow tensors and NumPy arrays. 101 | numpy_array = np.array([[1, 2], [3, 4]]) 102 | tensor_from_numpy = tf.convert_to_tensor(numpy_array, dtype=tf.float32) 103 | print("\nTensor from NumPy Array:") 104 | print(tensor_from_numpy) 105 | 106 | # Convert tensor back to NumPy 107 | numpy_from_tensor = tensor_from_numpy.numpy() 108 | print("NumPy Array from Tensor:") 109 | print(numpy_from_tensor) 110 | 111 | # Perform NumPy-style operations 112 | numpy_result = np.matmul(numpy_array, numpy_array) 113 | tensor_result = tf.matmul(tensor_from_numpy, tensor_from_numpy) 114 | print("NumPy Matmul Result:") 115 | print(numpy_result) 116 | print("TensorFlow Matmul Result:") 117 | print(tensor_result) 118 | 119 | # %% [10. Interview Scenario: Tensor Manipulation] 120 | # Demonstrate a practical tensor manipulation task. 121 | # Task: Create a 3x3 tensor, extract its diagonal, and compute its sum. 122 | interview_tensor = tf.constant([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype=tf.float32) 123 | diagonal = tf.linalg.diag_part(interview_tensor) 124 | diagonal_sum = tf.reduce_sum(diagonal) 125 | print("\nInterview Task:") 126 | print("Original Tensor:") 127 | print(interview_tensor) 128 | print("Diagonal:", diagonal.numpy()) 129 | print("Sum of Diagonal:", diagonal_sum.numpy()) 130 | 131 | # Visualize tensor as a heatmap 132 | import matplotlib.pyplot as plt 133 | plt.figure() 134 | plt.imshow(interview_tensor, cmap='viridis') 135 | plt.colorbar() 136 | plt.title('Tensor Heatmap') 137 | plt.savefig('tensor_heatmap.png') -------------------------------------------------------------------------------- /Tensorflow Fundamentals/03 Advanced TensorFlow Concepts/01 Distributed Training/distributed_training.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | from tensorflow.keras.datasets import cifar10 5 | 6 | # %% [1. Introduction to Distributed Training] 7 | # Distributed training scales TensorFlow models across multiple GPUs/TPUs. 8 | # Covers Data Parallelism, Multi-GPU/TPU Training, and Distributed Datasets. 9 | 10 | print("TensorFlow version:", tf.__version__) 11 | 12 | # %% [2. Preparing the Dataset] 13 | # Load and preprocess CIFAR-10 dataset. 14 | (x_train, y_train), (x_test, y_test) = cifar10.load_data() 15 | x_train = x_train.astype('float32') / 255.0 16 | x_test = x_test.astype('float32') / 255.0 17 | y_train = tf.keras.utils.to_categorical(y_train, 10) 18 | y_test = tf.keras.utils.to_categorical(y_test, 10) 19 | print("\nCIFAR-10 Dataset:") 20 | print("Train Shape:", x_train.shape, "Test Shape:", x_test.shape) 21 | 22 | # Create tf.data.Dataset pipelines 23 | batch_size = 64 24 | train_ds = tf.data.Dataset.from_tensor_slices((x_train, y_train)).shuffle(1000).batch(batch_size).prefetch(tf.data.AUTOTUNE) 25 | test_ds = tf.data.Dataset.from_tensor_slices((x_test, y_test)).batch(batch_size).prefetch(tf.data.AUTOTUNE) 26 | 27 | # %% [3. Data Parallelism with MirroredStrategy] 28 | # Use MirroredStrategy for data parallelism across GPUs. 29 | mirrored_strategy = tf.distribute.MirroredStrategy() 30 | print("\nMirroredStrategy Devices:", mirrored_strategy.num_replicas_in_sync) 31 | 32 | # Define and compile model within strategy scope 33 | with mirrored_strategy.scope(): 34 | model_mirrored = tf.keras.Sequential([ 35 | tf.keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)), 36 | tf.keras.layers.MaxPooling2D((2, 2)), 37 | tf.keras.layers.Flatten(), 38 | tf.keras.layers.Dense(128, activation='relu'), 39 | tf.keras.layers.Dense(10, activation='softmax') 40 | ]) 41 | model_mirrored.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) 42 | 43 | print("\nMirroredStrategy Model Summary:") 44 | model_mirrored.summary() 45 | mirrored_history = model_mirrored.fit(train_ds, epochs=5, validation_data=test_ds, verbose=1) 46 | print("MirroredStrategy Test Accuracy:", mirrored_history.history['val_accuracy'][-1].round(4)) 47 | 48 | # %% [4. Multi-GPU/TPU Training with TPUStrategy] 49 | # Use TPUStrategy (fallback to MirroredStrategy if TPU unavailable). 50 | try: 51 | resolver = tf.distribute.cluster_resolver.TPUClusterResolver() 52 | tf.config.experimental_connect_to_cluster(resolver) 53 | tf.tpu.experimental.initialize_tpu_system(resolver) 54 | tpu_strategy = tf.distribute.TPUStrategy(resolver) 55 | print("\nTPUStrategy Initialized") 56 | except ValueError: 57 | tpu_strategy = mirrored_strategy 58 | print("\nTPU Unavailable, Using MirroredStrategy") 59 | 60 | with tpu_strategy.scope(): 61 | model_tpu = tf.keras.Sequential([ 62 | tf.keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)), 63 | tf.keras.layers.MaxPooling2D((2, 2)), 64 | tf.keras.layers.Flatten(), 65 | tf.keras.layers.Dense(128, activation='relu'), 66 | tf.keras.layers.Dense(10, activation='softmax') 67 | ]) 68 | model_tpu.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) 69 | 70 | print("\nTPUStrategy Model Summary:") 71 | model_tpu.summary() 72 | tpu_history = model_tpu.fit(train_ds, epochs=5, validation_data=test_ds, verbose=1) 73 | print("TPUStrategy Test Accuracy:", tpu_history.history['val_accuracy'][-1].round(4)) 74 | 75 | # %% [5. Distributed Datasets] 76 | # Optimize dataset for distributed training. 77 | dist_train_ds = mirrored_strategy.experimental_distribute_dataset(train_ds) 78 | dist_test_ds = mirrored_strategy.experimental_distribute_dataset(test_ds) 79 | print("\nDistributed Dataset Created") 80 | 81 | # Custom training loop for distributed dataset 82 | @tf.function 83 | def dist_train_step(inputs): 84 | def step_fn(inputs): 85 | x, y = inputs 86 | with tf.GradientTape() as tape: 87 | logits = model_mirrored(x, training=True) 88 | loss = tf.keras.losses.categorical_crossentropy(y, logits) 89 | gradients = tape.gradient(loss, model_mirrored.trainable_variables) 90 | model_mirrored.optimizer.apply_gradients(zip(gradients, model_mirrored.trainable_variables)) 91 | return loss 92 | per_replica_losses = mirrored_strategy.run(step_fn, args=(inputs,)) 93 | return mirrored_strategy.reduce(tf.distribute.ReduceOp.MEAN, per_replica_losses, axis=None) 94 | 95 | print("\nCustom Distributed Training Loop:") 96 | for epoch in range(2): 97 | total_loss = 0.0 98 | num_batches = 0 99 | for inputs in dist_train_ds: 100 | total_loss += dist_train_step(inputs) 101 | num_batches += 1 102 | print(f"Epoch {epoch + 1}, Average Loss: {total_loss / num_batches:.4f}") 103 | 104 | # %% [6. Visualizing Training Progress] 105 | # Plot validation accuracy for MirroredStrategy and TPUStrategy. 106 | plt.figure() 107 | plt.plot(mirrored_history.history['val_accuracy'], label='MirroredStrategy') 108 | plt.plot(tpu_history.history['val_accuracy'], label='TPUStrategy') 109 | plt.xlabel('Epoch') 110 | plt.ylabel('Validation Accuracy') 111 | plt.title('Distributed Training Comparison') 112 | plt.legend() 113 | plt.savefig('distributed_training_comparison.png') 114 | 115 | # %% [7. Interview Scenario: Scaling Training] 116 | # Discuss strategies for scaling training to multiple devices. 117 | print("\nInterview Scenario: Scaling Training") 118 | print("1. MirroredStrategy: Synchronous data parallelism for multi-GPU setups.") 119 | print("2. TPUStrategy: Optimized for TPU clusters in cloud environments.") 120 | print("3. Distributed Datasets: Use experimental_distribute_dataset for efficient data sharding.") 121 | print("Key: Ensure model and data pipeline are compatible with strategy scope.") -------------------------------------------------------------------------------- /Tensorflow Fundamentals/03 Advanced TensorFlow Concepts/03 Custom Extensions/custom_extensions.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | from tensorflow.keras.datasets import mnist 5 | 6 | # %% [1. Introduction to Custom Extensions] 7 | # Custom extensions enhance TensorFlow with custom gradients, addons, and optimizers. 8 | # This file demonstrates custom gradient functions, TensorFlow Addons, and a custom optimizer. 9 | 10 | print("TensorFlow version:", tf.__version__) 11 | 12 | # %% [2. Preparing the Dataset] 13 | # Load and preprocess MNIST dataset. 14 | (x_train, y_train), (x_test, y_test) = mnist.load_data() 15 | x_train = x_train.astype('float32') / 255.0 16 | x_test = x_test.astype('float32') / 255.0 17 | x_train = x_train[..., np.newaxis] 18 | x_test = x_test[..., np.newaxis] 19 | train_ds = tf.data.Dataset.from_tensor_slices((x_train, y_train)).shuffle(1000).batch(32).prefetch(tf.data.AUTOTUNE) 20 | test_ds = tf.data.Dataset.from_tensor_slices((x_test, y_test)).batch(32).prefetch(tf.data.AUTOTUNE) 21 | print("\nMNIST Dataset:") 22 | print("Train Shape:", x_train.shape, "Test Shape:", x_test.shape) 23 | 24 | # %% [3. Custom Gradient Functions] 25 | # Define a custom gradient for a clipping operation. 26 | @tf.custom_gradient 27 | def clip_by_value(x, clip_min, clip_max): 28 | y = tf.clip_by_value(x, clip_min, clip_max) 29 | def grad(dy): 30 | return dy * tf.where((x >= clip_min) & (x <= clip_max), 1.0, 0.0), None, None 31 | return y, grad 32 | 33 | # Test custom gradient in a model 34 | model_custom_grad = tf.keras.Sequential([ 35 | tf.keras.layers.Flatten(input_shape=(28, 28, 1)), 36 | tf.keras.layers.Dense(64, activation='relu'), 37 | tf.keras.layers.Dense(10, activation='softmax') 38 | ]) 39 | optimizer = tf.keras.optimizers.Adam() 40 | @tf.function 41 | def train_step(x, y): 42 | with tf.GradientTape() as tape: 43 | logits = model_custom_grad(x) 44 | logits = clip_by_value(logits, 1e-7, 1.0 - 1e-7) 45 | loss = tf.keras.losses.sparse_categorical_crossentropy(y, logits) 46 | gradients = tape.gradient(loss, model_custom_grad.trainable_variables) 47 | optimizer.apply_gradients(zip(gradients, model_custom_grad.trainable_variables)) 48 | return loss 49 | 50 | print("\nCustom Gradient Training:") 51 | for epoch in range(3): 52 | for x, y in train_ds: 53 | loss = train_step(x, y) 54 | print(f"Epoch {epoch + 1}, Loss: {loss.numpy():.4f}") 55 | 56 | # %% [4. TensorFlow Addons] 57 | # Note: TensorFlow Addons is deprecated; use Keras 3 or alternatives for advanced metrics/losses. 58 | # Example: Custom loss inspired by Addons (e.g., focal loss). 59 | class FocalLoss(tf.keras.losses.Loss): 60 | def __init__(self, gamma=2.0, alpha=0.25): 61 | super().__init__() 62 | self.gamma = gamma 63 | self.alpha = alpha 64 | 65 | def call(self, y_true, y_pred): 66 | y_true = tf.cast(y_true, tf.float32) 67 | y_pred = tf.clip_by_value(y_pred, 1e-7, 1.0 - 1e-7) 68 | ce = -y_true * tf.math.log(y_pred) 69 | weight = self.alpha * y_true * tf.pow(1.0 - y_pred, self.gamma) 70 | return tf.reduce_mean(weight * ce) 71 | 72 | model_addons = tf.keras.Sequential([ 73 | tf.keras.layers.Flatten(input_shape=(28, 28, 1)), 74 | tf.keras.layers.Dense(64, activation='relu'), 75 | tf.keras.layers.Dense(10, activation='softmax') 76 | ]) 77 | model_addons.compile(optimizer='adam', loss=FocalLoss(), metrics=['accuracy']) 78 | print("\nFocal Loss Model Summary:") 79 | model_addons.summary() 80 | addons_history = model_addons.fit(train_ds, epochs=3, validation_data=test_ds, verbose=1) 81 | print("Focal Loss Test Accuracy:", addons_history.history['val_accuracy'][-1].round(4)) 82 | 83 | # %% [5. Custom Optimizers] 84 | # Define a custom optimizer with momentum. 85 | class CustomMomentumOptimizer(tf.keras.optimizers.Optimizer): 86 | def __init__(self, learning_rate=0.01, momentum=0.9, name="CustomMomentum"): 87 | super().__init__(name=name) 88 | self.learning_rate = learning_rate 89 | self.momentum = momentum 90 | 91 | def _create_slots(self, var_list): 92 | for var in var_list: 93 | self.add_slot(var, 'velocity', initializer='zeros') 94 | 95 | def _resource_apply_dense(self, grad, var, apply_state=None): 96 | velocity = self.get_slot(var, 'velocity') 97 | velocity_t = velocity * self.momentum - self.learning_rate * grad 98 | var_t = var + velocity_t 99 | velocity.assign(velocity_t) 100 | var.assign(var_t) 101 | return tf.no_op() 102 | 103 | model_custom_opt = tf.keras.Sequential([ 104 | tf.keras.layers.Flatten(input_shape=(28, 28, 1)), 105 | tf.keras.layers.Dense(64, activation='relu'), 106 | tf.keras.layers.Dense(10, activation='softmax') 107 | ]) 108 | model_custom_opt.compile(optimizer=CustomMomentumOptimizer(), loss='sparse_categorical_crossentropy', metrics=['accuracy']) 109 | print("\nCustom Optimizer Model Summary:") 110 | model_custom_opt.summary() 111 | custom_opt_history = model_custom_opt.fit(train_ds, epochs=3, validation_data=test_ds, verbose=1) 112 | print("Custom Optimizer Test Accuracy:", custom_opt_history.history['val_accuracy'][-1].round(4)) 113 | 114 | # %% [6. Visualizing Training Progress] 115 | # Plot validation accuracy for models. 116 | plt.figure() 117 | plt.plot(addons_history.history['val_accuracy'], label='Focal Loss') 118 | plt.plot(custom_opt_history.history['val_accuracy'], label='Custom Optimizer') 119 | plt.xlabel('Epoch') 120 | plt.ylabel('Validation Accuracy') 121 | plt.title('Custom Extensions Comparison') 122 | plt.legend() 123 | plt.savefig('custom_extensions_comparison.png') 124 | 125 | # %% [7. Interview Scenario: Custom Gradient] 126 | # Discuss implementing a custom gradient for a non-standard operation. 127 | print("\nInterview Scenario: Custom Gradient") 128 | print("Use @tf.custom_gradient to define forward and backward passes.") 129 | print("Example: Clip operation with gradients only in valid range.") 130 | print("Key: Ensure gradient function matches operation’s logic.") -------------------------------------------------------------------------------- /Tensorflow Fundamentals/01 Core TensorFlow Foundations/02 Automatic Differentiation/automatic_differentiation.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # %% [1. Introduction to Automatic Differentiation] 6 | # Automatic differentiation computes gradients for optimization in TensorFlow. 7 | # Key components: computational graphs, tf.GradientTape, optimizer.apply_gradients, and tf.stop_gradient. 8 | 9 | print("TensorFlow version:", tf.__version__) 10 | 11 | # %% [2. Computational Graphs] 12 | # TensorFlow builds computational graphs to track operations for gradient computation. 13 | # tf.GradientTape records operations dynamically for automatic differentiation. 14 | x = tf.constant(3.0) 15 | with tf.GradientTape() as tape: 16 | tape.watch(x) # Ensure x is tracked 17 | y = x**2 + 2*x + 1 # Polynomial: y = x^2 + 2x + 1 18 | dy_dx = tape.gradient(y, x) 19 | print("\nComputational Graph Example:") 20 | print("Function: y = x^2 + 2x + 1, x =", x.numpy()) 21 | print("Gradient dy/dx =", dy_dx.numpy()) # Expected: 2x + 2 = 8 at x=3 22 | 23 | # %% [3. Gradient Computation with tf.GradientTape] 24 | # Compute gradients for a simple neural network layer: y = Wx + b. 25 | W = tf.Variable([[1.0, 2.0], [3.0, 4.0]]) 26 | b = tf.Variable([1.0, 1.0]) 27 | x = tf.constant([[1.0], [2.0]]) 28 | with tf.GradientTape() as tape: 29 | y = tf.matmul(W, x) + b # Linear transformation 30 | loss = tf.reduce_sum(y**2) # Dummy loss: sum of squared outputs 31 | grad_W, grad_b = tape.gradient(loss, [W, b]) 32 | print("\nGradient Computation (Linear Layer):") 33 | print("W:\n", W.numpy()) 34 | print("b:", b.numpy()) 35 | print("x:\n", x.numpy()) 36 | print("Loss:", loss.numpy()) 37 | print("Gradient w.r.t. W:\n", grad_W.numpy()) 38 | print("Gradient w.r.t. b:", grad_b.numpy()) 39 | 40 | # %% [4. Higher-Order Gradients] 41 | # Compute second-order gradients (e.g., Hessian) using nested tapes. 42 | x = tf.constant(2.0) 43 | with tf.GradientTape() as outer_tape: 44 | with tf.GradientTape() as inner_tape: 45 | inner_tape.watch(x) 46 | y = x**3 # Function: y = x^3 47 | dy_dx = inner_tape.gradient(y, x) # First derivative: 3x^2 48 | d2y_dx2 = outer_tape.gradient(dy_dx, x) # Second derivative: 6x 49 | print("\nHigher-Order Gradients:") 50 | print("Function: y = x^3, x =", x.numpy()) 51 | print("First Derivative (dy/dx):", dy_dx.numpy()) # Expected: 3x^2 = 12 at x=2 52 | print("Second Derivative (d2y/dx2):", d2y_dx2.numpy()) # Expected: 6x = 12 at x=2 53 | 54 | # %% [5. Gradient Application with Optimizer] 55 | # Use an optimizer to update variables based on gradients. 56 | W = tf.Variable([[1.0, 2.0]], name='W') 57 | b = tf.Variable([0.0], name='b') 58 | x = tf.constant([[1.0, 2.0]]) 59 | y_true = tf.constant([5.0]) 60 | optimizer = tf.keras.optimizers.SGD(learning_rate=0.1) 61 | for _ in range(3): # Simulate 3 optimization steps 62 | with tf.GradientTape() as tape: 63 | y_pred = tf.matmul(W, x, transpose_b=True) + b # y = Wx + b 64 | loss = tf.reduce_mean((y_pred - y_true)**2) # MSE loss 65 | grad_W, grad_b = tape.gradient(loss, [W, b]) 66 | optimizer.apply_gradients(zip([grad_W, grad_b], [W, b])) 67 | print(f"\nStep {_+1} - Loss: {loss.numpy():.4f}, W: {W.numpy().flatten()}, b: {b.numpy()}") 68 | 69 | # %% [6. No-Gradient Context with tf.stop_gradient] 70 | # tf.stop_gradient prevents gradients from flowing through a tensor. 71 | x = tf.constant(2.0) 72 | with tf.GradientTape() as tape: 73 | tape.watch(x) 74 | y = x**2 # y = x^2 75 | z = tf.stop_gradient(y) # Treat y as a constant 76 | w = z * x # w = y * x 77 | dw_dx = tape.gradient(w, x) 78 | print("\nNo-Gradient Context:") 79 | print("Function: w = (x^2) * x, with x^2 stopped") 80 | print("Gradient dw/dx:", dw_dx.numpy()) # Expected: y = x^2 = 4 at x=2 81 | 82 | # %% [7. Practical Application: Linear Regression] 83 | # Train a linear regression model using tf.GradientTape. 84 | np.random.seed(42) 85 | X = np.random.rand(100, 1).astype(np.float32) 86 | y = 3 * X + 2 + np.random.normal(0, 0.1, (100, 1)).astype(np.float32) 87 | W = tf.Variable([[0.0]], name='weight') 88 | b = tf.Variable([0.0], name='bias') 89 | optimizer = tf.keras.optimizers.Adam(learning_rate=0.1) 90 | losses = [] 91 | for epoch in range(50): 92 | with tf.GradientTape() as tape: 93 | y_pred = tf.matmul(X, W) + b 94 | loss = tf.reduce_mean(tf.square(y_pred - y)) 95 | grad_W, grad_b = tape.gradient(loss, [W, b]) 96 | optimizer.apply_gradients(zip([grad_W, grad_b], [W, b])) 97 | losses.append(loss.numpy()) 98 | if epoch % 10 == 0: 99 | print(f"Epoch {epoch}, Loss: {loss.numpy():.4f}") 100 | print("\nLearned Parameters: W =", W.numpy().flatten(), "b =", b.numpy()) 101 | 102 | # %% [8. Visualizing Training Progress] 103 | # Plot loss curve for linear regression. 104 | plt.figure() 105 | plt.plot(losses) 106 | plt.xlabel('Epoch') 107 | plt.ylabel('Loss') 108 | plt.title('Linear Regression Loss Curve') 109 | plt.savefig('loss_curve.png') 110 | 111 | # Plot predictions 112 | plt.figure() 113 | plt.scatter(X, y, label='Data') 114 | plt.plot(X, tf.matmul(X, W) + b, color='red', label='Fit') 115 | plt.xlabel('X') 116 | plt.ylabel('y') 117 | plt.title('Linear Regression Fit') 118 | plt.legend() 119 | plt.savefig('linear_fit.png') 120 | 121 | # %% [9. Interview Scenario: Gradient Debugging] 122 | # Debug a case where gradients are None due to non-differentiable operations. 123 | x = tf.Variable(1.0) 124 | with tf.GradientTape() as tape: 125 | y = tf.cast(x, tf.int32) # Non-differentiable operation 126 | loss = y**2 127 | grad = tape.gradient(loss, x) 128 | print("\nGradient Debugging:") 129 | print("Operation: y = cast(x to int), loss = y^2") 130 | print("Gradient:", grad) # Expected: None due to non-differentiable cast 131 | print("Fix: Ensure operations are differentiable (e.g., use float operations).") 132 | 133 | # %% [10. Custom Gradient Computation] 134 | # Compute gradients for a custom function: f(x) = sin(x) + x^2. 135 | x = tf.Variable(1.0) 136 | with tf.GradientTape() as tape: 137 | y = tf.sin(x) + x**2 # f(x) = sin(x) + x^2 138 | dy_dx = tape.gradient(y, x) 139 | print("\nCustom Gradient:") 140 | print("Function: f(x) = sin(x) + x^2, x =", x.numpy()) 141 | print("Gradient: df/dx =", dy_dx.numpy()) # Expected: cos(x) + 2x = cos(1) + 2 -------------------------------------------------------------------------------- /Tensorflow Fundamentals/01 Core TensorFlow Foundations/03 Neural Networks (tf.keras)/neural_networks_keras.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | from sklearn.datasets import make_regression, make_classification 5 | from sklearn.model_selection import train_test_split 6 | from sklearn.preprocessing import StandardScaler 7 | 8 | # %% [1. Introduction to Neural Networks with tf.keras] 9 | # tf.keras is TensorFlow's high-level API for building and training neural networks. 10 | # Covers model definition, layers, activations, losses, optimizers, and learning rate schedules. 11 | 12 | print("TensorFlow version:", tf.__version__) 13 | 14 | # %% [2. Defining Models with tf.keras.Sequential] 15 | # tf.keras.Sequential creates a linear stack of layers for simple models. 16 | # Example: Regression model for synthetic data. 17 | X_reg, y_reg = make_regression(n_samples=1000, n_features=5, noise=10, random_state=42) 18 | X_reg_train, X_reg_test, y_reg_train, y_reg_test = train_test_split(X_reg, y_reg, test_size=0.2, random_state=42) 19 | scaler = StandardScaler() 20 | X_reg_train = scaler.fit_transform(X_reg_train) 21 | X_reg_test = scaler.transform(X_reg_test) 22 | 23 | seq_model = tf.keras.Sequential([ 24 | tf.keras.layers.Dense(64, activation='relu', input_shape=(5,)), 25 | tf.keras.layers.Dense(32, activation='relu'), 26 | tf.keras.layers.Dense(1) # No activation for regression 27 | ]) 28 | seq_model.compile(optimizer='adam', loss='mse') 29 | print("\nSequential Model Summary:") 30 | seq_model.summary() 31 | 32 | # Train the model 33 | history_seq = seq_model.fit(X_reg_train, y_reg_train, epochs=20, batch_size=32, validation_split=0.2, verbose=0) 34 | print("Sequential Model Final Validation Loss:", history_seq.history['val_loss'][-1].round(4)) 35 | 36 | # %% [3. Defining Models with tf.keras.Model] 37 | # tf.keras.Model allows custom models via subclassing for complex architectures. 38 | # Example: Classification model for synthetic data. 39 | X_clf, y_clf = make_classification(n_samples=1000, n_features=10, n_classes=3, n_informative=8, random_state=42) 40 | X_clf_train, X_clf_test, y_clf_train, y_clf_test = train_test_split(X_clf, y_clf, test_size=0.2, random_state=42) 41 | X_clf_train = scaler.fit_transform(X_clf_train) 42 | X_clf_test = scaler.transform(X_clf_test) 43 | y_clf_train_cat = tf.keras.utils.to_categorical(y_clf_train) 44 | y_clf_test_cat = tf.keras.utils.to_categorical(y_clf_test) 45 | 46 | class CustomModel(tf.keras.Model): 47 | def __init__(self): 48 | super(CustomModel, self).__init__() 49 | self.dense1 = tf.keras.layers.Dense(128, activation='relu') 50 | self.dense2 = tf.keras.layers.Dense(64, activation='relu') 51 | self.dense3 = tf.keras.layers.Dense(3, activation='softmax') 52 | 53 | def call(self, inputs): 54 | x = self.dense1(inputs) 55 | x = self.dense2(x) 56 | return self.dense3(x) 57 | 58 | custom_model = CustomModel() 59 | custom_model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) 60 | print("\nCustom Model Training:") 61 | custom_model.fit(X_clf_train, y_clf_train_cat, epochs=10, batch_size=32, validation_split=0.2, verbose=0) 62 | loss, acc = custom_model.evaluate(X_clf_test, y_clf_test_cat, verbose=0) 63 | print("Custom Model Test Loss:", loss.round(4), "Test Accuracy:", acc.round(4)) 64 | 65 | # %% [4. Layers: Dense, Convolutional, Pooling, Normalization] 66 | # Example: CNN for synthetic image-like data (simplified). 67 | X_img = np.random.rand(100, 28, 28, 1).astype(np.float32) # 100 samples, 28x28x1 68 | y_img = np.random.randint(0, 2, 100) # Binary classification 69 | X_img_train, X_img_test, y_img_train, y_img_test = train_test_split(X_img, y_img, test_size=0.2, random_state=42) 70 | 71 | cnn_model = tf.keras.Sequential([ 72 | tf.keras.layers.Conv2D(16, (3, 3), activation='relu', input_shape=(28, 28, 1)), 73 | tf.keras.layers.BatchNormalization(), 74 | tf.keras.layers.MaxPooling2D((2, 2)), 75 | tf.keras.layers.Conv2D(32, (3, 3), activation='relu'), 76 | tf.keras.layers.Flatten(), 77 | tf.keras.layers.Dense(64, activation='relu'), 78 | tf.keras.layers.Dense(1, activation='sigmoid') 79 | ]) 80 | cnn_model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) 81 | print("\nCNN Model Summary:") 82 | cnn_model.summary() 83 | cnn_model.fit(X_img_train, y_img_train, epochs=5, batch_size=16, validation_split=0.2, verbose=0) 84 | cnn_loss, cnn_acc = cnn_model.evaluate(X_img_test, y_img_test, verbose=0) 85 | print("CNN Model Test Loss:", cnn_loss.round(4), "Test Accuracy:", cnn_acc.round(4)) 86 | 87 | # %% [5. Activations: ReLU, Sigmoid, Softmax] 88 | # Demonstrate activation functions in a small network. 89 | act_model = tf.keras.Sequential([ 90 | tf.keras.layers.Dense(16, activation='relu', input_shape=(5,)), # ReLU 91 | tf.keras.layers.Dense(8, activation='sigmoid'), # Sigmoid 92 | tf.keras.layers.Dense(3, activation='softmax') # Softmax 93 | ]) 94 | print("\nActivation Functions Model Summary:") 95 | act_model.summary() 96 | 97 | # %% [6. Loss Functions: MSE, Categorical Crossentropy] 98 | # MSE for regression (used in seq_model). 99 | # Categorical Crossentropy for classification (used in custom_model). 100 | print("\nLoss Functions Used:") 101 | print("MSE for Regression (Sequential Model):", history_seq.history['loss'][-1].round(4)) 102 | print("Categorical Crossentropy for Classification (Custom Model):", loss.round(4)) 103 | 104 | # %% [7. Optimizers: SGD, Adam, RMSprop] 105 | # Compare optimizers on the regression task. 106 | optimizers = { 107 | 'SGD': tf.keras.optimizers.SGD(learning_rate=0.01), 108 | 'Adam': tf.keras.optimizers.Adam(learning_rate=0.001), 109 | 'RMSprop': tf.keras.optimizers.RMSprop(learning_rate=0.001) 110 | } 111 | results = {} 112 | for name, opt in optimizers.items(): 113 | model = tf.keras.Sequential([ 114 | tf.keras.layers.Dense(32, activation='relu', input_shape=(5,)), 115 | tf.keras.layers.Dense(1) 116 | ]) 117 | model.compile(optimizer=opt, loss='mse') 118 | history = model.fit(X_reg_train, y_reg_train, epochs=10, batch_size=32, validation_split=0.2, verbose=0) 119 | results[name] = history.history['val_loss'][-1] 120 | print("\nOptimizer Comparison (Validation Loss):") 121 | for name, val_loss in results.items(): 122 | print(f"{name}: {val_loss:.4f}") 123 | 124 | # %% [8. Learning Rate Schedules] 125 | # Use a decaying learning rate schedule for the regression task. 126 | initial_lr = 0.1 127 | lr_schedule = tf.keras.optimizers.schedules.ExponentialDecay( 128 | initial_lr, decay_steps=100, decay_rate=0.9, staircase=True 129 | ) 130 | model_lr = tf.keras.Sequential([ 131 | tf.keras.layers.Dense(32, activation='relu', input_shape=(5,)), 132 | tf.keras.layers.Dense(1) 133 | ]) 134 | model_lr.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=lr_schedule), loss='mse') 135 | history_lr = model_lr.fit(X_reg_train, y_reg_train, epochs=20, batch_size=32, validation_split=0.2, verbose=0) 136 | print("\nLearning Rate Schedule Model Final Validation Loss:", history_lr.history['val_loss'][-1].round(4)) 137 | 138 | # %% [9. Visualizing Training Progress] 139 | # Plot loss curves for Sequential and Learning Rate Schedule models. 140 | plt.figure() 141 | plt.plot(history_seq.history['loss'], label='Sequential (Adam)') 142 | plt.plot(history_lr.history['loss'], label='Learning Rate Schedule') 143 | plt.xlabel('Epoch') 144 | plt.ylabel('Loss') 145 | plt.title('Training Loss Curves') 146 | plt.legend() 147 | plt.savefig('loss_curves.png') 148 | 149 | # %% [10. Interview Scenario: Model Design] 150 | # Design a CNN for a small image classification task. 151 | interview_cnn = tf.keras.Sequential([ 152 | tf.keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)), 153 | tf.keras.layers.MaxPooling2D((2, 2)), 154 | tf.keras.layers.BatchNormalization(), 155 | tf.keras.layers.Flatten(), 156 | tf.keras.layers.Dense(64, activation='relu'), 157 | tf.keras.layers.Dense(10, activation='softmax') 158 | ]) 159 | interview_cnn.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) 160 | print("\nInterview Scenario: CNN Model Summary:") 161 | interview_cnn.summary() 162 | print("Explanation: Conv2D extracts features, MaxPooling reduces dimensions, BatchNorm stabilizes training, Softmax outputs class probabilities.") -------------------------------------------------------------------------------- /Tensorflow Fundamentals/03 Advanced TensorFlow Concepts/02 Advanced Architectures/advanced_architectures.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | from tensorflow.keras.datasets import cifar10 5 | import tensorflow_datasets as tfds 6 | import tensorflow_agents as tfa 7 | from tensorflow_agents.environments import suite_gym, tf_py_environment 8 | from tensorflow_agents.agents.dqn import dqn_agent 9 | from tensorflow_agents.networks import q_network 10 | from tensorflow_agents.policies import random_tf_policy 11 | from tensorflow_agents.replay_buffers import tf_uniform_replay_buffer 12 | from tensorflow_agents.utils import common 13 | 14 | # %% [1. Introduction to Advanced Architectures] 15 | # Advanced architectures include Transformers, Generative Models, Graph Neural Networks, and Reinforcement Learning. 16 | # This file demonstrates a Vision Transformer, VAE, and DQN with TF-Agents. 17 | 18 | print("TensorFlow version:", tf.__version__) 19 | 20 | # %% [2. Preparing Datasets] 21 | # Load CIFAR-10 for Vision Transformer and VAE. 22 | (x_train, y_train), (x_test, y_test) = cifar10.load_data() 23 | x_train = x_train.astype('float32') / 255.0 24 | x_test = x_test.astype('float32') / 255.0 25 | y_train = tf.keras.utils.to_categorical(y_train, 10) 26 | y_test = tf.keras.utils.to_categorical(y_test, 10) 27 | print("\nCIFAR-10 Dataset:") 28 | print("Train Shape:", x_train.shape, "Test Shape:", x_test.shape) 29 | 30 | train_ds = tf.data.Dataset.from_tensor_slices((x_train, y_train)).shuffle(1000).batch(32).prefetch(tf.data.AUTOTUNE) 31 | test_ds = tf.data.Dataset.from_tensor_slices((x_test, y_test)).batch(32).prefetch(tf.data.AUTOTUNE) 32 | 33 | # %% [3. Vision Transformer (ViT)] 34 | # Simplified Vision Transformer for CIFAR-10. 35 | class PatchExtractor(tf.keras.layers.Layer): 36 | def __init__(self, patch_size): 37 | super().__init__() 38 | self.patch_size = patch_size 39 | 40 | def call(self, images): 41 | patches = tf.image.extract_patches( 42 | images=images, sizes=[1, self.patch_size, self.patch_size, 1], 43 | strides=[1, self.patch_size, self.patch_size, 1], rates=[1, 1, 1, 1], padding='VALID') 44 | return tf.reshape(patches, [tf.shape(images)[0], -1, patches.shape[-1]]) 45 | 46 | class ViT(tf.keras.Model): 47 | def __init__(self, num_classes, patch_size, num_patches, d_model, num_heads): 48 | super().__init__() 49 | self.patch_extractor = PatchExtractor(patch_size) 50 | self.pos_embedding = self.add_weight('pos_embedding', shape=(1, num_patches + 1, d_model)) 51 | self.cls_token = self.add_weight('cls_token', shape=(1, 1, d_model)) 52 | self.transformer = tf.keras.layers.MultiHeadAttention(num_heads=num_heads, key_dim=d_model) 53 | self.dense = tf.keras.layers.Dense(num_classes, activation='softmax') 54 | 55 | def call(self, inputs): 56 | patches = self.patch_extractor(inputs) 57 | batch_size = tf.shape(inputs)[0] 58 | cls_tokens = tf.repeat(self.cls_token, batch_size, axis=0) 59 | x = tf.concat([cls_tokens, patches], axis=1) 60 | x += self.pos_embedding 61 | x = self.transformer(x, x) 62 | x = x[:, 0, :] # Take CLS token 63 | return self.dense(x) 64 | 65 | vit_model = ViT(num_classes=10, patch_size=8, num_patches=(32//8)**2, d_model=64, num_heads=4) 66 | vit_model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) 67 | print("\nVision Transformer Training:") 68 | vit_history = vit_model.fit(train_ds, epochs=3, validation_data=test_ds, verbose=1) 69 | print("ViT Test Accuracy:", vit_history.history['val_accuracy'][-1].round(4)) 70 | 71 | # %% [4. Generative Model: Variational Autoencoder (VAE)] 72 | # VAE for generating CIFAR-10-like images. 73 | class VAE(tf.keras.Model): 74 | def __init__(self): 75 | super().__init__() 76 | self.encoder = tf.keras.Sequential([ 77 | tf.keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)), 78 | tf.keras.layers.Flatten(), 79 | tf.keras.layers.Dense(128, activation='relu'), 80 | tf.keras.layers.Dense(16 + 16) # Mean + log variance 81 | ]) 82 | self.decoder = tf.keras.Sequential([ 83 | tf.keras.layers.Dense(128, activation='relu', input_shape=(16,)), 84 | tf.keras.layers.Dense(32 * 32 * 32, activation='relu'), 85 | tf.keras.layers.Reshape((32, 32, 32)), 86 | tf.keras.layers.Conv2DTranspose(3, (3, 3), activation='sigmoid', padding='same') 87 | ]) 88 | 89 | def call(self, inputs): 90 | mean, logvar = tf.split(self.encoder(inputs), num_or_size_splits=2, axis=1) 91 | epsilon = tf.random.normal(tf.shape(mean)) 92 | z = mean + tf.exp(0.5 * logvar) * epsilon 93 | return self.decoder(z) 94 | 95 | vae_model = VAE() 96 | vae_optimizer = tf.keras.optimizers.Adam() 97 | @tf.function 98 | def vae_loss(x, x_recon): 99 | mean, logvar = tf.split(vae_model.encoder(x), num_or_size_splits=2, axis=1) 100 | recon_loss = tf.reduce_mean(tf.keras.losses.binary_crossentropy(x, x_recon)) 101 | kl_loss = -0.5 * tf.reduce_mean(1 + logvar - tf.square(mean) - tf.exp(logvar)) 102 | return recon_loss + kl_loss 103 | 104 | @tf.function 105 | def train_vae_step(x): 106 | with tf.GradientTape() as tape: 107 | x_recon = vae_model(x) 108 | loss = vae_loss(x, x_recon) 109 | gradients = tape.gradient(loss, vae_model.trainable_variables) 110 | vae_optimizer.apply_gradients(zip(gradients, vae_model.trainable_variables)) 111 | return loss 112 | 113 | print("\nVAE Training:") 114 | for epoch in range(3): 115 | for x, _ in train_ds: 116 | loss = train_vae_step(x) 117 | print(f"Epoch {epoch + 1}, Loss: {loss.numpy():.4f}") 118 | 119 | # Generate and save sample images 120 | generated = vae_model(x_test[:5]) 121 | plt.figure() 122 | for i in range(5): 123 | plt.subplot(2, 5, i + 1) 124 | plt.imshow(x_test[i]) 125 | plt.title("Original") 126 | plt.axis('off') 127 | plt.subplot(2, 5, i + 6) 128 | plt.imshow(generated[i]) 129 | plt.title("Generated") 130 | plt.axis('off') 131 | plt.savefig('vae_samples.png') 132 | 133 | # %% [5. Reinforcement Learning with TF-Agents] 134 | # DQN for CartPole environment. 135 | env = suite_gym.load('CartPole-v0') 136 | train_env = tf_py_environment.TFPyEnvironment(env) 137 | eval_env = tf_py_environment.TFPyEnvironment(env) 138 | 139 | q_net = q_network.QNetwork( 140 | train_env.observation_spec(), 141 | train_env.action_spec(), 142 | fc_layer_params=(100,) 143 | ) 144 | agent = dqn_agent.DqnAgent( 145 | train_env.time_step_spec(), 146 | train_env.action_spec(), 147 | q_network=q_net, 148 | optimizer=tf.keras.optimizers.Adam(learning_rate=1e-3), 149 | td_errors_loss_fn=common.element_wise_squared_loss, 150 | train_step_counter=tf.Variable(0) 151 | ) 152 | agent.initialize() 153 | 154 | replay_buffer = tf_uniform_replay_buffer.TFUniformReplayBuffer( 155 | data_spec=agent.collect_data_spec, 156 | batch_size=train_env.batch_size, 157 | max_length=1000 158 | ) 159 | collect_policy = random_tf_policy.RandomTFPolicy(train_env.time_step_spec(), train_env.action_spec()) 160 | def collect_step(env, policy): 161 | time_step = env.current_time_step() 162 | action_step = policy.action(time_step) 163 | next_time_step = env.step(action_step.action) 164 | traj = tfa.trajectories.from_transition(time_step, action_step, next_time_step) 165 | replay_buffer.add_batch(traj) 166 | 167 | print("\nDQN Training on CartPole:") 168 | for _ in range(100): 169 | collect_step(train_env, collect_policy) 170 | dataset = replay_buffer.as_dataset(num_parallel_calls=3, sample_batch_size=64, num_steps=2).prefetch(3) 171 | iterator = iter(dataset) 172 | for _ in range(1000): 173 | trajectories, _ = next(iterator) 174 | agent.train(trajectories) 175 | 176 | # Evaluate DQN 177 | total_reward = 0 178 | for _ in range(5): 179 | time_step = eval_env.reset() 180 | episode_reward = 0 181 | while not time_step.is_last(): 182 | action_step = agent.policy.action(time_step) 183 | time_step = eval_env.step(action_step.action) 184 | episode_reward += time_step.reward 185 | total_reward += episode_reward 186 | print("Average Reward:", (total_reward / 5).numpy()) -------------------------------------------------------------------------------- /Tensorflow Fundamentals/01 Core TensorFlow Foundations/04 Datasets and Data Loading/datasets_and_data_loading.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | try: 5 | import tensorflow_datasets as tfds 6 | except ImportError: 7 | tfds = None 8 | from sklearn.preprocessing import StandardScaler 9 | 10 | # %% [1. Introduction to Datasets and Data Loading] 11 | # Efficient data loading and preprocessing are critical for ML training. 12 | # TensorFlow provides tf.keras.datasets, tfds.load, tf.data.Dataset, and tf.keras.preprocessing. 13 | 14 | print("TensorFlow version:", tf.__version__) 15 | 16 | # %% [2. Built-in Datasets with tf.keras.datasets] 17 | # Load MNIST dataset from tf.keras.datasets. 18 | (x_train_mnist, y_train_mnist), (x_test_mnist, y_test_mnist) = tf.keras.datasets.mnist.load_data() 19 | print("\nMNIST Dataset:") 20 | print("Train Shape:", x_train_mnist.shape, "Test Shape:", x_test_mnist.shape) 21 | print("Label Example:", y_train_mnist[:5]) 22 | 23 | # Normalize pixel values to [0, 1] 24 | x_train_mnist = x_train_mnist.astype('float32') / 255.0 25 | x_test_mnist = x_test_mnist.astype('float32') / 255.0 26 | print("Normalized Train Data (first sample, first row):", x_train_mnist[0, 0, :5]) 27 | 28 | # %% [3. TensorFlow Datasets with tfds.load] 29 | # Load CIFAR-10 dataset using tensorflow-datasets (if installed). 30 | if tfds is not None: 31 | ds_cifar, info = tfds.load('cifar10', with_info=True, as_supervised=True) 32 | ds_cifar_train = ds_cifar['train'] 33 | ds_cifar_test = ds_cifar['test'] 34 | print("\nCIFAR-10 Dataset Info:") 35 | print("Features:", info.features) 36 | print("Number of Training Examples:", info.splits['train'].num_examples) 37 | 38 | # Example: Extract one batch 39 | for image, label in ds_cifar_train.take(1): 40 | print("Sample Image Shape:", image.shape, "Label:", label.numpy()) 41 | else: 42 | print("\ntensorflow-datasets not installed. Install with: pip install tensorflow-datasets") 43 | ds_cifar_train = None 44 | 45 | # %% [4. Data Pipeline with tf.data.Dataset] 46 | # Create a tf.data.Dataset pipeline for MNIST. 47 | mnist_train_ds = tf.data.Dataset.from_tensor_slices((x_train_mnist, y_train_mnist)) 48 | 49 | # Apply transformations: shuffle, batch, and preprocess 50 | def preprocess_mnist(image, label): 51 | image = tf.image.random_brightness(image, max_delta=0.1) # Data augmentation 52 | image = tf.expand_dims(image, axis=-1) # Add channel dimension: (28, 28) -> (28, 28, 1) 53 | label = tf.cast(label, tf.int32) 54 | return image, label 55 | 56 | mnist_train_ds = (mnist_train_ds 57 | .map(preprocess_mnist, num_parallel_calls=tf.data.AUTOTUNE) 58 | .shuffle(buffer_size=1000) 59 | .batch(batch_size=32) 60 | .prefetch(tf.data.AUTOTUNE)) 61 | print("\nMNIST tf.data.Dataset Pipeline Created:") 62 | for image, label in mnist_train_ds.take(1): 63 | print("Batch Shape:", image.shape, "Label Shape:", label.shape) 64 | 65 | # %% [5. Preprocessing with tf.keras.preprocessing] 66 | # Use tf.keras.preprocessing for data augmentation on MNIST. 67 | data_augmentation = tf.keras.Sequential([ 68 | tf.keras.layers.RandomRotation(0.1), 69 | tf.keras.layers.RandomZoom(0.1), 70 | tf.keras.layers.RandomTranslation(0.1, 0.1) 71 | ]) 72 | # Apply augmentation to a sample image 73 | sample_image = x_train_mnist[0:1][..., np.newaxis] # Shape: (1, 28, 28, 1) 74 | augmented_image = data_augmentation(sample_image) 75 | print("\nAugmented Image Shape:", augmented_image.shape) 76 | 77 | # Visualize original vs. augmented image 78 | plt.figure(figsize=(8, 4)) 79 | plt.subplot(1, 2, 1) 80 | plt.imshow(sample_image[0, :, :, 0], cmap='gray') 81 | plt.title('Original Image') 82 | plt.subplot(1, 2, 2) 83 | plt.imshow(augmented_image[0, :, :, 0], cmap='gray') 84 | plt.title('Augmented Image') 85 | plt.savefig('augmentation_comparison.png') 86 | 87 | # %% [6. Handling Large Datasets] 88 | # Simulate a large dataset with synthetic data and create an efficient pipeline. 89 | np.random.seed(42) 90 | large_x = np.random.rand(100000, 10).astype(np.float32) 91 | large_y = np.random.randint(0, 2, 100000).astype(np.int32) 92 | large_ds = tf.data.Dataset.from_tensor_slices((large_x, large_y)) 93 | 94 | # Preprocessing function 95 | def preprocess_large(x, y): 96 | x = tf.cast(x, tf.float32) 97 | x = (x - tf.reduce_mean(x, axis=0)) / tf.math.reduce_std(x, axis=0) # Standardize 98 | y = tf.cast(y, tf.int32) 99 | return x, y 100 | 101 | # Efficient pipeline for large dataset 102 | large_ds = (large_ds 103 | .map(preprocess_large, num_parallel_calls=tf.data.AUTOTUNE) 104 | .shuffle(buffer_size=10000) 105 | .batch(batch_size=64) 106 | .prefetch(tf.data.AUTOTUNE)) 107 | print("\nLarge Dataset Pipeline:") 108 | for x, y in large_ds.take(1): 109 | print("Batch Shape:", x.shape, "Label Shape:", y.shape) 110 | print("Standardized Features (first sample, first 5):", x[0, :5].numpy().round(4)) 111 | 112 | # %% [7. Practical Application: Training a Model with tf.data] 113 | # Train a simple CNN on the MNIST pipeline. 114 | cnn_model = tf.keras.Sequential([ 115 | tf.keras.layers.Conv2D(16, (3, 3), activation='relu', input_shape=(28, 28, 1)), 116 | tf.keras.layers.MaxPooling2D((2, 2)), 117 | tf.keras.layers.Flatten(), 118 | tf.keras.layers.Dense(64, activation='relu'), 119 | tf.keras.layers.Dense(10, activation='softmax') 120 | ]) 121 | cnn_model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) 122 | print("\nCNN Training on MNIST Pipeline:") 123 | history = cnn_model.fit(mnist_train_ds, epochs=5, validation_data=(x_test_mnist[..., np.newaxis], y_test_mnist), verbose=1) 124 | print("Final Validation Accuracy:", history.history['val_accuracy'][-1].round(4)) 125 | 126 | # %% [8. Visualizing Training Progress] 127 | # Plot training and validation accuracy. 128 | plt.figure() 129 | plt.plot(history.history['accuracy'], label='Train Accuracy') 130 | plt.plot(history.history['val_accuracy'], label='Validation Accuracy') 131 | plt.xlabel('Epoch') 132 | plt.ylabel('Accuracy') 133 | plt.title('MNIST CNN Training Progress') 134 | plt.legend() 135 | plt.savefig('mnist_training_progress.png') 136 | 137 | # %% [9. Interview Scenario: Optimizing Data Pipelines] 138 | # Optimize a pipeline for a large image dataset. 139 | def optimized_pipeline(dataset, batch_size=32): 140 | def preprocess(image, label): 141 | image = tf.cast(image, tf.float32) / 255.0 142 | image = tf.image.random_flip_left_right(image) 143 | label = tf.cast(label, tf.int32) 144 | return image, label 145 | return (dataset 146 | .map(preprocess, num_parallel_calls=tf.data.AUTOTUNE) 147 | .cache() # Cache in memory for small datasets 148 | .shuffle(buffer_size=1000) 149 | .batch(batch_size) 150 | .prefetch(tf.data.AUTOTUNE)) 151 | 152 | print("\nInterview Scenario: Optimized Pipeline") 153 | print("Key Optimizations: cache(), prefetch(), parallel map, appropriate shuffle buffer.") 154 | if tfds is not None: 155 | sample_ds = tfds.load('cifar10', split='train', as_supervised=True) 156 | optimized_ds = optimized_pipeline(sample_ds) 157 | for image, label in optimized_ds.take(1): 158 | print("Optimized Batch Shape:", image.shape, "Label Shape:", label.shape) 159 | 160 | # %% [10. Custom Preprocessing Function] 161 | # Create a custom preprocessing function for a regression dataset. 162 | np.random.seed(42) 163 | X_reg = np.random.rand(1000, 5).astype(np.float32) 164 | y_reg = np.sum(X_reg, axis=1) + np.random.normal(0, 0.1, 1000).astype(np.float32) 165 | reg_ds = tf.data.Dataset.from_tensor_slices((X_reg, y_reg)) 166 | 167 | def custom_preprocess(x, y): 168 | x = tf.cast(x, tf.float32) 169 | x = (x - tf.reduce_mean(x)) / tf.math.reduce_std(x) # Normalize 170 | y = tf.cast(y, tf.float32) 171 | return x, y 172 | 173 | reg_ds = (reg_ds 174 | .map(custom_preprocess, num_parallel_calls=tf.data.AUTOTUNE) 175 | .shuffle(buffer_size=100) 176 | .batch(batch_size=16) 177 | .prefetch(tf.data.AUTOTUNE)) 178 | print("\nCustom Preprocessing for Regression Dataset:") 179 | for x, y in reg_ds.take(1): 180 | print("Batch Shape:", x.shape, "Label Shape:", y.shape) 181 | print("Normalized Features (first sample, first 5):", x[0, :5].numpy().round(4)) -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 🔥 TensorFlow Interview Preparation 2 | 3 |
Your comprehensive guide to mastering TensorFlow for AI/ML research and industry applications
13 | 14 | --- 15 | 16 | ## 📖 Introduction 17 | 18 | Welcome to the TensorFlow Mastery Roadmap! 🚀 This repository is your ultimate guide to conquering TensorFlow, a powerful open-source framework for machine learning and AI. Designed for hands-on learning and interview preparation, it covers everything from tensors to advanced model deployment, empowering you to excel in AI/ML projects and technical interviews with confidence. 19 | 20 | ## 🌟 What’s Inside? 21 | 22 | - **Core TensorFlow Foundations**: Master tensors, Keras API, neural networks, and data pipelines. 23 | - **Intermediate Techniques**: Build CNNs, RNNs, and leverage transfer learning. 24 | - **Advanced Concepts**: Explore Transformers, GANs, distributed training, and edge deployment. 25 | - **Specialized Libraries**: Dive into `TensorFlow Datasets`, `TensorFlow Hub`, `Keras`, and `TensorFlow Lite`. 26 | - **Hands-on Projects**: Tackle beginner-to-advanced projects to solidify your skills. 27 | - **Best Practices**: Learn optimization, debugging, and production-ready workflows. 28 | 29 | ## 🔍 Who Is This For? 30 | 31 | - Data Scientists aiming to build scalable ML models. 32 | - Machine Learning Engineers preparing for technical interviews. 33 | - AI Researchers exploring advanced architectures. 34 | - Software Engineers transitioning to deep learning roles. 35 | - Anyone passionate about TensorFlow and AI innovation. 36 | 37 | ## 🗺️ Comprehensive Learning Roadmap 38 | 39 | --- 40 | 41 | ### 📚 Prerequisites 42 | 43 | - **Python Proficiency**: Core Python (data structures, OOP, file handling). 44 | - **Mathematics for ML**: 45 | - Linear Algebra (vectors, matrices, eigenvalues) 46 | - Calculus (gradients, optimization) 47 | - Probability & Statistics (distributions, Bayes’ theorem) 48 | - **Machine Learning Basics**: 49 | - Supervised/Unsupervised Learning 50 | - Regression, Classification, Clustering 51 | - Bias-Variance, Evaluation Metrics 52 | - **NumPy**: Arrays, broadcasting, and mathematical operations. 53 | 54 | --- 55 | 56 | ### 🏗️ Core TensorFlow Foundations 57 | 58 | #### 🧮 Tensors and Operations 59 | - Tensor Creation (`tf.constant`, `tf.zeros`, `tf.random`) 60 | - Attributes (shape, `dtype`, `device`) 61 | - Operations (indexing, reshaping, matrix multiplication, broadcasting) 62 | - CPU/GPU Interoperability 63 | - NumPy Integration 64 | 65 | #### 🔢 Automatic Differentiation 66 | - Computational Graphs 67 | - Gradient Computation (`tf.GradientTape`) 68 | - Gradient Application (`optimizer.apply_gradients`) 69 | - No-Gradient Context (`tf.stop_gradient`) 70 | 71 | #### 🛠️ Neural Networks (`tf.keras`) 72 | - Defining Models (`tf.keras.Sequential`, `tf.keras.Model`) 73 | - Layers: Dense, Convolutional, Pooling, Normalization 74 | - Activations: ReLU, Sigmoid, Softmax 75 | - Loss Functions: MSE, Categorical Crossentropy 76 | - Optimizers: SGD, Adam, RMSprop 77 | - Learning Rate Schedules 78 | 79 | #### 📂 Datasets and Data Loading 80 | - Built-in Datasets (`tf.keras.datasets`) 81 | - TensorFlow Datasets (`tfds.load`) 82 | - Data Pipeline (`tf.data.Dataset`, map, batch, shuffle) 83 | - Preprocessing (`tf.keras.preprocessing`) 84 | - Handling Large Datasets 85 | 86 | #### 🔄 Training Pipeline 87 | - Training/Evaluation Loops 88 | - Model Checkpointing (`model.save`, `model.load`) 89 | - GPU/TPU Training (`tf.device`) 90 | - Monitoring with TensorBoard 91 | 92 | --- 93 | 94 | ### 🧩 Intermediate TensorFlow Concepts 95 | 96 | #### 🏋️ Model Architectures 97 | - Feedforward Neural Networks (FNNs) 98 | - Convolutional Neural Networks (CNNs) 99 | - Recurrent Neural Networks (RNNs, LSTMs, GRUs) 100 | - Transfer Learning (`tf.keras.applications`) 101 | 102 | #### ⚙️ Customization 103 | - Custom Layers and Loss Functions 104 | - Functional and Subclassing APIs 105 | - Debugging Gradient Issues 106 | 107 | #### 📈 Optimization 108 | - Hyperparameter Tuning (learning rate, batch size) 109 | - Regularization (dropout, L2) 110 | - Mixed Precision Training (`tf.keras.mixed_precision`) 111 | - Model Quantization 112 | 113 | --- 114 | 115 | ### 🚀 Advanced TensorFlow Concepts 116 | 117 | #### 🌐 Distributed Training 118 | - Data Parallelism (`tf.distribute.MirroredStrategy`) 119 | - Multi-GPU/TPU Training (`tf.distribute.TPUStrategy`) 120 | - Distributed Datasets 121 | 122 | #### 🧠 Advanced Architectures 123 | - Transformers (BERT, Vision Transformers) 124 | - Generative Models (VAEs, GANs) 125 | - Graph Neural Networks 126 | - Reinforcement Learning (TF-Agents) 127 | 128 | #### 🛠️ Custom Extensions 129 | - Custom Gradient Functions 130 | - TensorFlow Addons 131 | - Custom Optimizers 132 | 133 | #### 📦 Deployment 134 | - Model Export (SavedModel, ONNX) 135 | - Serving (TensorFlow Serving, FastAPI) 136 | - Edge Deployment (TensorFlow Lite, TensorFlow.js) 137 | 138 | --- 139 | 140 | ### 🧬 Specialized TensorFlow Libraries 141 | 142 | - **TensorFlow Datasets**: Curated datasets for ML tasks 143 | - **TensorFlow Hub**: Pretrained models for transfer learning 144 | - **Keras**: High-level API for rapid prototyping 145 | - **TensorFlow Lite**: Lightweight models for mobile/edge devices 146 | - **TensorFlow.js**: ML in the browser 147 | 148 | --- 149 | 150 | ### ⚠️ Best Practices 151 | 152 | - Modular Code Organization 153 | - Version Control with Git 154 | - Unit Testing for Models 155 | - Experiment Tracking (TensorBoard, MLflow) 156 | - Reproducible Research (random seeds, versioning) 157 | 158 | --- 159 | 160 | ## 💡 Why Master TensorFlow? 161 | 162 | TensorFlow is a leading framework for machine learning, and here’s why: 163 | 1. **Scalability**: Seamless transition from research to production. 164 | 2. **Ecosystem**: Rich libraries for datasets, pretrained models, and edge deployment. 165 | 3. **Industry Adoption**: Powers AI at Google, Airbnb, and more. 166 | 4. **Versatility**: Supports mobile, web, and enterprise applications. 167 | 5. **Community**: Active support on X, forums, and GitHub. 168 | 169 | This roadmap is your guide to mastering TensorFlow for AI/ML careers—let’s ignite your machine learning journey! 🔥 170 | 171 | ## 📆 Study Plan 172 | 173 | - **Month 1-2**: Tensors, Keras, neural networks, data pipelines 174 | - **Month 3-4**: CNNs, RNNs, transfer learning, intermediate projects 175 | - **Month 5-6**: Transformers, GANs, distributed training 176 | - **Month 7+**: Deployment, custom extensions, advanced projects 177 | 178 | ## 🛠️ Projects 179 | 180 | - **Beginner**: Linear Regression, MNIST/CIFAR-10 Classification 181 | - **Intermediate**: Object Detection (SSD, Faster R-CNN), Sentiment Analysis 182 | - **Advanced**: BERT Fine-tuning, GANs, Distributed Training 183 | 184 | ## 📚 Resources 185 | 186 | - **Official Docs**: [tensorflow.org](https://tensorflow.org) 187 | - **Tutorials**: TensorFlow Tutorials, Coursera 188 | - **Books**: 189 | - *Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow* by Aurélien Géron 190 | - *TensorFlow for Deep Learning* by Bharath Ramsundar 191 | - **Communities**: TensorFlow Forums, X (#TensorFlow), r/TensorFlow 192 | 193 | ## 🤝 Contributions 194 | 195 | Want to enhance this roadmap? 🌟 196 | 1. Fork the repository. 197 | 2. Create a feature branch (`git checkout -b feature/amazing-addition`). 198 | 3. Commit changes (`git commit -m 'Add awesome content'`). 199 | 4. Push to the branch (`git push origin feature/amazing-addition`). 200 | 5. Open a Pull Request. 201 | 202 | --- 203 | 204 |Happy Learning and Best of Luck in Your AI/ML Journey! ✨
206 |