help@rskworld.in +91 93305 39277
RSK World
  • Home
  • Development
    • Web Development
    • Mobile Apps
    • Software
    • Games
    • Project
  • Technologies
    • Data Science
    • AI Development
    • Cloud Development
    • Blockchain
    • Cyber Security
    • Dev Tools
    • Testing Tools
  • Blog
  • About
  • Contact

Theme Settings

Color Scheme
Display Options
Font Size
100%
Back to Project
RSK World
tensorflow-deeplearning
/
src
RSK World
tensorflow-deeplearning
Deep learning with TensorFlow and Keras
src
  • utils
  • __init__.py330 B
  • autoencoders.py8 KB
  • cnns.py6.7 KB
  • custom_layers.py8.3 KB
  • data_generator.py14.2 KB
  • data_preprocessing.py9.9 KB
  • gans.py7 KB
  • model_deployment.py8.7 KB
  • model_evaluation.py10.5 KB
  • model_training.py10.1 KB
  • neural_networks.py4.7 KB
  • rnns.py6.8 KB
  • transfer_learning.py5.4 KB
  • transformers.py7.8 KB
  • visualization.py9.6 KB
transformers.pytransfer_learning.py
src/transformers.py
Raw Download
Find: Go to:
"""
Transformer Models with TensorFlow
Author: RSK World
Website: https://rskworld.in
Email: help@rskworld.in
Phone: +91 93305 39277

This module demonstrates Transformer architecture implementation.
"""

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers, Model
import numpy as np
import math

def positional_encoding(position, d_model):
    """
    Create positional encoding for transformer.
    
    Args:
        position: Maximum position
        d_model: Model dimension
    
    Returns:
        Positional encoding matrix
    """
    angle_rads = np.arange(position)[:, np.newaxis] / np.power(10000, (2 * (np.arange(d_model)[np.newaxis, :] // 2)) / np.float32(d_model))
    
    angle_rads[:, 0::2] = np.sin(angle_rads[:, 0::2])
    angle_rads[:, 1::2] = np.cos(angle_rads[:, 1::2])
    
    pos_encoding = angle_rads[np.newaxis, ...]
    return tf.cast(pos_encoding, dtype=tf.float32)

class MultiHeadAttention(layers.Layer):
    """
    Multi-head attention layer.
    Author: RSK World - https://rskworld.in
    """
    
    def __init__(self, d_model, num_heads, **kwargs):
        super(MultiHeadAttention, self).__init__(**kwargs)
        self.num_heads = num_heads
        self.d_model = d_model
        
        assert d_model % self.num_heads == 0
        
        self.depth = d_model // self.num_heads
        
        self.wq = layers.Dense(d_model)
        self.wk = layers.Dense(d_model)
        self.wv = layers.Dense(d_model)
        
        self.dense = layers.Dense(d_model)
    
    def split_heads(self, x, batch_size):
        """Split the last dimension into (num_heads, depth)."""
        x = tf.reshape(x, (batch_size, -1, self.num_heads, self.depth))
        return tf.transpose(x, perm=[0, 2, 1, 3])
    
    def call(self, v, k, q, mask=None):
        batch_size = tf.shape(q)[0]
        
        q = self.wq(q)
        k = self.wk(k)
        v = self.wv(v)
        
        q = self.split_heads(q, batch_size)
        k = self.split_heads(k, batch_size)
        v = self.split_heads(v, batch_size)
        
        scaled_attention, attention_weights = self.scaled_dot_product_attention(
            q, k, v, mask
        )
        
        scaled_attention = tf.transpose(scaled_attention, perm=[0, 2, 1, 3])
        concat_attention = tf.reshape(scaled_attention, (batch_size, -1, self.d_model))
        
        output = self.dense(concat_attention)
        
        return output, attention_weights
    
    def scaled_dot_product_attention(self, q, k, v, mask):
        """Calculate the attention weights."""
        matmul_qk = tf.matmul(q, k, transpose_b=True)
        
        dk = tf.cast(tf.shape(k)[-1], tf.float32)
        scaled_attention_logits = matmul_qk / tf.math.sqrt(dk)
        
        if mask is not None:
            scaled_attention_logits += (mask * -1e9)
        
        attention_weights = tf.nn.softmax(scaled_attention_logits, axis=-1)
        output = tf.matmul(attention_weights, v)
        
        return output, attention_weights

def point_wise_feed_forward_network(d_model, dff):
    """
    Point-wise feed-forward network.
    
    Args:
        d_model: Model dimension
        dff: Feed-forward dimension
    
    Returns:
        Sequential model
    """
    return keras.Sequential([
        layers.Dense(dff, activation='relu'),
        layers.Dense(d_model)
    ])

class EncoderLayer(layers.Layer):
    """
    Transformer encoder layer.
    Author: RSK World - https://rskworld.in
    """
    
    def __init__(self, d_model, num_heads, dff, rate=0.1, **kwargs):
        super(EncoderLayer, self).__init__(**kwargs)
        
        self.mha = MultiHeadAttention(d_model, num_heads)
        self.ffn = point_wise_feed_forward_network(d_model, dff)
        
        self.layernorm1 = layers.LayerNormalization(epsilon=1e-6)
        self.layernorm2 = layers.LayerNormalization(epsilon=1e-6)
        
        self.dropout1 = layers.Dropout(rate)
        self.dropout2 = layers.Dropout(rate)
    
    def call(self, x, training, mask=None):
        attn_output, _ = self.mha(x, x, x, mask)
        attn_output = self.dropout1(attn_output, training=training)
        out1 = self.layernorm1(x + attn_output)
        
        ffn_output = self.ffn(out1)
        ffn_output = self.dropout2(ffn_output, training=training)
        out2 = self.layernorm2(out1 + ffn_output)
        
        return out2

class DecoderLayer(layers.Layer):
    """
    Transformer decoder layer.
    Author: RSK World - https://rskworld.in
    """
    
    def __init__(self, d_model, num_heads, dff, rate=0.1, **kwargs):
        super(DecoderLayer, self).__init__(**kwargs)
        
        self.mha1 = MultiHeadAttention(d_model, num_heads)
        self.mha2 = MultiHeadAttention(d_model, num_heads)
        
        self.ffn = point_wise_feed_forward_network(d_model, dff)
        
        self.layernorm1 = layers.LayerNormalization(epsilon=1e-6)
        self.layernorm2 = layers.LayerNormalization(epsilon=1e-6)
        self.layernorm3 = layers.LayerNormalization(epsilon=1e-6)
        
        self.dropout1 = layers.Dropout(rate)
        self.dropout2 = layers.Dropout(rate)
        self.dropout3 = layers.Dropout(rate)
    
    def call(self, x, enc_output, training, look_ahead_mask=None, padding_mask=None):
        attn1, attn_weights_block1 = self.mha1(x, x, x, look_ahead_mask)
        attn1 = self.dropout1(attn1, training=training)
        out1 = self.layernorm1(attn1 + x)
        
        attn2, attn_weights_block2 = self.mha2(enc_output, enc_output, out1, padding_mask)
        attn2 = self.dropout2(attn2, training=training)
        out2 = self.layernorm2(attn2 + out1)
        
        ffn_output = self.ffn(out2)
        ffn_output = self.dropout3(ffn_output, training=training)
        out3 = self.layernorm3(ffn_output + out2)
        
        return out3, attn_weights_block1, attn_weights_block2

def create_transformer_encoder(num_layers, d_model, num_heads, dff, input_vocab_size, maximum_position_encoding, rate=0.1):
    """
    Create a transformer encoder.
    
    Args:
        num_layers: Number of encoder layers
        d_model: Model dimension
        num_heads: Number of attention heads
        dff: Feed-forward dimension
        input_vocab_size: Vocabulary size
        maximum_position_encoding: Maximum position encoding
        rate: Dropout rate
    
    Returns:
        Encoder model
    """
    inputs = keras.Input(shape=(None,))
    x = layers.Embedding(input_vocab_size, d_model)(inputs)
    x *= tf.math.sqrt(tf.cast(d_model, tf.float32))
    
    pos_encoding = positional_encoding(maximum_position_encoding, d_model)
    x += pos_encoding[:, :tf.shape(x)[1], :]
    x = layers.Dropout(rate)(x)
    
    for i in range(num_layers):
        x = EncoderLayer(d_model, num_heads, dff, rate)(x, training=True)
    
    return Model(inputs, x, name='transformer_encoder')

def example_usage():
    """
    Example usage of transformer functions.
    """
    # Create a simple transformer encoder
    encoder = create_transformer_encoder(
        num_layers=2,
        d_model=128,
        num_heads=8,
        dff=512,
        input_vocab_size=10000,
        maximum_position_encoding=1000,
        rate=0.1
    )
    
    print("Transformer Encoder Model:")
    encoder.summary()
    
    # Test with dummy data
    sample_input = tf.random.uniform((32, 100), minval=0, maxval=10000, dtype=tf.int32)
    sample_output = encoder(sample_input, training=False)
    
    print(f"\nInput shape: {sample_input.shape}")
    print(f"Output shape: {sample_output.shape}")
    
    return encoder

if __name__ == '__main__':
    print("Transformer Models with TensorFlow")
    print("Author: RSK World - https://rskworld.in")
    encoder = example_usage()
241 lines•7.8 KB
python
src/transfer_learning.py
Raw Download
Find: Go to:
"""
Transfer Learning with TensorFlow
Author: RSK World
Website: https://rskworld.in
Email: help@rskworld.in
Phone: +91 93305 39277

This module demonstrates transfer learning using pre-trained models.
"""

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers, applications
import numpy as np
import matplotlib.pyplot as plt

def create_transfer_learning_model(base_model_name='VGG16', num_classes=10, input_shape=(224, 224, 3)):
    """
    Create a transfer learning model using pre-trained base models.
    
    Args:
        base_model_name: Name of pre-trained model ('VGG16', 'ResNet50', 'MobileNet', 'InceptionV3')
        num_classes: Number of output classes
        input_shape: Input image shape
    
    Returns:
        Compiled Keras model
    """
    # Load pre-trained base model
    base_models = {
        'VGG16': applications.VGG16,
        'ResNet50': applications.ResNet50,
        'MobileNet': applications.MobileNet,
        'InceptionV3': applications.InceptionV3,
        'Xception': applications.Xception,
        'DenseNet121': applications.DenseNet121
    }
    
    if base_model_name not in base_models:
        raise ValueError(f"Unknown base model: {base_model_name}")
    
    # Create base model
    base_model = base_models[base_model_name](
        weights='imagenet',
        include_top=False,
        input_shape=input_shape
    )
    
    # Freeze base model layers
    base_model.trainable = False
    
    # Add custom classification head
    model = keras.Sequential([
        base_model,
        layers.GlobalAveragePooling2D(),
        layers.Dense(512, activation='relu'),
        layers.Dropout(0.5),
        layers.Dense(num_classes, activation='softmax')
    ])
    
    model.compile(
        optimizer=keras.optimizers.Adam(learning_rate=0.001),
        loss='sparse_categorical_crossentropy',
        metrics=['accuracy']
    )
    
    return model, base_model

def fine_tune_model(model, base_model, unfreeze_layers=10):
    """
    Fine-tune a transfer learning model by unfreezing some layers.
    
    Args:
        model: Transfer learning model
        base_model: Base pre-trained model
        unfreeze_layers: Number of top layers to unfreeze
    
    Returns:
        Recompiled model ready for fine-tuning
    """
    # Unfreeze top layers
    base_model.trainable = True
    
    # Freeze all layers except the last N layers
    for layer in base_model.layers[:-unfreeze_layers]:
        layer.trainable = False
    
    # Recompile with lower learning rate for fine-tuning
    model.compile(
        optimizer=keras.optimizers.Adam(learning_rate=0.0001),
        loss='sparse_categorical_crossentropy',
        metrics=['accuracy']
    )
    
    return model

def create_feature_extractor(base_model_name='VGG16', input_shape=(224, 224, 3)):
    """
    Create a feature extractor using pre-trained model.
    
    Args:
        base_model_name: Name of pre-trained model
        input_shape: Input image shape
    
    Returns:
        Feature extractor model
    """
    base_models = {
        'VGG16': applications.VGG16,
        'ResNet50': applications.ResNet50,
        'MobileNet': applications.MobileNet,
        'InceptionV3': applications.InceptionV3
    }
    
    base_model = base_models[base_model_name](
        weights='imagenet',
        include_top=False,
        input_shape=input_shape
    )
    
    base_model.trainable = False
    
    # Add global pooling
    inputs = keras.Input(shape=input_shape)
    x = base_model(inputs, training=False)
    x = layers.GlobalAveragePooling2D()(x)
    
    feature_extractor = keras.Model(inputs, x)
    
    return feature_extractor

def extract_features(model, images, batch_size=32):
    """
    Extract features from images using a pre-trained model.
    
    Args:
        model: Feature extractor model
        images: Input images
        batch_size: Batch size for processing
    
    Returns:
        Extracted features
    """
    features = model.predict(images, batch_size=batch_size, verbose=0)
    return features

def example_usage():
    """
    Example usage of transfer learning functions.
    """
    # Create transfer learning model
    model, base_model = create_transfer_learning_model(
        base_model_name='MobileNet',
        num_classes=10,
        input_shape=(224, 224, 3)
    )
    
    print("Transfer Learning Model:")
    model.summary()
    
    # Generate dummy data for demonstration
    X_train = np.random.rand(100, 224, 224, 3).astype('float32')
    y_train = np.random.randint(0, 10, 100)
    
    # Train model (initial training with frozen base)
    print("\nTraining with frozen base model...")
    history = model.fit(
        X_train, y_train,
        batch_size=16,
        epochs=2,
        verbose=1
    )
    
    # Fine-tune model
    print("\nFine-tuning model...")
    model = fine_tune_model(model, base_model, unfreeze_layers=10)
    
    # Continue training with fine-tuning
    history_finetune = model.fit(
        X_train, y_train,
        batch_size=16,
        epochs=2,
        verbose=1
    )
    
    return model, history, history_finetune

if __name__ == '__main__':
    print("Transfer Learning with TensorFlow")
    print("Author: RSK World - https://rskworld.in")
    model, history, history_finetune = example_usage()
192 lines•5.4 KB
python
🚀 Support RSK World

Subscribe to our YouTube channel for latest tutorials & updates!



Click subscribe & support our work ❤️

About RSK World

Founded by Molla Samser, with Designer & Tester Rima Khatun, RSK World is your one-stop destination for free programming resources, source code, and development tools.

Founder: Molla Samser
Designer & Tester: Rima Khatun

Development

  • Game Development
  • Web Development
  • Mobile Development
  • AI Development
  • Development Tools

Legal

  • Terms & Conditions
  • Privacy Policy
  • Disclaimer

Contact Info

Nutanhat, Mongolkote
Purba Burdwan, West Bengal
India, 713147

+91 93305 39277

hello@rskworld.in
support@rskworld.in

© 2026 RSK World. All rights reserved.

Content used for educational purposes only. View Disclaimer