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
pytorch-neuralnetworks
/
examples
RSK World
pytorch-neuralnetworks
Neural networks with PyTorch
examples
  • advanced_features_example.py5.1 KB
  • hyperparameter_tuning_example.py4.6 KB
  • transfer_learning_example.py2.1 KB
transfer_learning_example.py
examples/transfer_learning_example.py
Raw Download
Find: Go to:
"""
Transfer Learning Example - PyTorch Neural Networks
Project: PyTorch Neural Networks
Author: RSK World
Website: https://rskworld.in
Email: help@rskworld.in
Phone: +91 93305 39277
Description: Example demonstrating transfer learning
"""

import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader, TensorDataset
import sys

sys.path.append('..')

from models.transfer_learning import TransferLearningModel
from training.trainer import Trainer
from training.utils import generate_sample_data


def transfer_learning_example():
    """
    Demonstrate transfer learning
    """
    print("=" * 60)
    print("Transfer Learning Example")
    print("Author: RSK World (https://rskworld.in)")
    print("=" * 60)
    
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
    print(f"\nUsing device: {device}\n")
    
    # Note: For real transfer learning, you would use actual image data
    # This is a simplified example with synthetic data
    
    print("Creating transfer learning model (ResNet18)...")
    model = TransferLearningModel(
        model_name='resnet18',
        num_classes=10,
        pretrained=True,
        freeze_backbone=True  # Freeze backbone, only train classifier
    ).to(device)
    
    print("Model created!")
    print(f"Total parameters: {sum(p.numel() for p in model.parameters()):,}")
    print(f"Trainable parameters: {sum(p.numel() for p in model.parameters() if p.requires_grad):,}")
    
    # For demonstration, we'll use synthetic data
    # In practice, use real image datasets
    print("\nNote: Using synthetic data for demonstration")
    print("In practice, use torchvision.datasets for real image data")
    
    # Example: Fine-tuning after initial training
    print("\nAfter initial training, you can unfreeze all layers:")
    print("model.unfreeze_all()  # Unfreeze for fine-tuning")
    
    print("\n" + "=" * 60)
    print("Transfer learning setup complete!")
    print("=" * 60)


if __name__ == '__main__':
    transfer_learning_example()

68 lines•2.1 KB
python

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