Close Menu
    Trending
    • Mommies, Nannies, Au Pairs, and Me: The End Of Being A SAHD
    • Building Essential Leadership Skills in Franchising
    • History of Artificial Intelligence: Key Milestones That Shaped the Future | by amol pawar | softAai Blogs | Jun, 2025
    • FedEx Deploys Hellebrekers Robotic Sorting Arm in Germany
    • Call Klarna’s AI Hotline and Talk to an AI Clone of Its CEO
    • A First-Principles Guide to Multilingual Sentence Embeddings | by Tharunika L | Jun, 2025
    • Google, Spotify Down in a Massive Outage Affecting Thousands
    • Prediksi Kualitas Anggur dengan Random Forest — Panduan Lengkap dengan Python | by Gilang Andhika | Jun, 2025
    Finance StarGate
    • Home
    • Artificial Intelligence
    • AI Technology
    • Data Science
    • Machine Learning
    • Finance
    • Passive Income
    Finance StarGate
    Home»Machine Learning»TensorFlow vs. PyTorch — Speed, Efficiency & Real-World Performance Compared | by vikram mohanagandhi | Feb, 2025
    Machine Learning

    TensorFlow vs. PyTorch — Speed, Efficiency & Real-World Performance Compared | by vikram mohanagandhi | Feb, 2025

    FinanceStarGateBy FinanceStarGateFebruary 25, 2025No Comments4 Mins Read
    Share Facebook Twitter Pinterest LinkedIn Tumblr Reddit Telegram Email
    Share
    Facebook Twitter LinkedIn Pinterest Email


    Within the deep studying panorama, TensorFlow and PyTorch are the 2 dominant frameworks. Whereas TensorFlow is thought for its production-ready capabilities and scalability, PyTorch has gained reputation for its ease of use and dynamic computation graph. However which one performs higher in real-world situations?

    On this article, we’ll examine TensorFlow and PyTorch based mostly on pace, effectivity, and efficiency utilizing GPU-based benchmarks. We’ll run the identical deep studying fashions on each frameworks and measure coaching time, inference pace, reminiscence utilization, and scalability.

    First, guarantee you’ve gotten Python 3.9+ put in (really useful by way of Conda or Homebrew).

    ✅ Set up TensorFlow for Apple Silicon:

    pip set up tensorflow-macos tensorflow-metal

    Confirm TensorFlow is utilizing the Apple GPU:

    import tensorflow as tf
    print(tf.config.list_physical_devices('GPU'))

    ✅ Set up PyTorch with MPS (Steel Efficiency Shaders) Backend:

    pip set up torch torchvision torchaudio

    Confirm PyTorch is utilizing MPS GPU backend:

    import torch
    print(torch.backends.mps.is_available()) # Ought to return True

    We are going to examine TensorFlow vs. PyTorch on the next:
    1. Matrix Multiplication (Low-level compute take a look at)
    2. CNN Coaching Velocity (Deep studying workload with ResNet-18)
    3. Inference Velocity (Time taken for ahead move on a single picture)

    a. Matrix Multiplication (Uncooked Compute Take a look at)

    This exams how properly every framework performs matrix operations utilizing GPU.

    import torch
    import tensorflow as tf
    import time
    from colorama import Fore, Again, Model
    from termcolor import coloured, cprint

    # Outline matrix dimension
    N = 5000

    # TensorFlow take a look at
    tf_matrix1 = tf.random.regular((N, N))
    tf_matrix2 = tf.random.regular((N, N))

    start_time = time.time()
    tf_result = tf.matmul(tf_matrix1, tf_matrix2)
    tf_time = time.time() - start_time

    # PyTorch take a look at
    torch_matrix1 = torch.randn((N, N), system="mps")
    torch_matrix2 = torch.randn((N, N), system="mps")

    start_time = time.time()
    torch_result = torch.mm(torch_matrix1, torch_matrix2)
    torch_time = time.time() - start_time

    cprint(tf.config.list_physical_devices('GPU'),'inexperienced','on_black')
    cprint(torch.backends.mps.is_available(),'inexperienced','on_black')

    print(Fore.LIGHTGREEN_EX +f"TensorFlow Matrix Multiplication Time: {tf_time:.4f} seconds")
    print(Fore.GREEN + f"PyTorch Matrix Multiplication Time: {torch_time:.4f} seconds")

    Consequence:

    b. CNN Coaching Benchmark (ResNet-18 on CIFAR-10)

    i ) Pytorch

    import torch
    import torch.nn as nn
    import torch.optim as optim
    import torchvision.transforms as transforms
    import torchvision.datasets as datasets
    import time
    from colorama import Fore, Again, Model
    from termcolor import coloured, cprint
    import os

    os.environ['CUDA_VISIBLE_DEVICES'] = ''

    # Confirm that no GPUs are seen to PyTorch
    print(torch.cuda.is_available())

    # Load CIFAR-10 dataset
    rework = transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize((0.5,), (0.5,))
    ])

    train_dataset = datasets.CIFAR10(root='./knowledge', prepare=True, obtain=True, rework=rework)
    train_loader = torch.utils.knowledge.DataLoader(train_dataset, batch_size=64, shuffle=True)

    # Outline a easy ResNet mannequin
    mannequin = torch.hub.load('pytorch/imaginative and prescient:v0.10.0', 'resnet18', pretrained=False)
    mannequin = mannequin.to("mps")

    criterion = nn.CrossEntropyLoss()
    optimizer = optim.Adam(mannequin.parameters(), lr=0.001)

    # Coaching loop for 1 epoch
    start_time = time.time()
    for photos, labels in train_loader:
    photos, labels = photos.to("mps"), labels.to("mps")

    optimizer.zero_grad()
    outputs = mannequin(photos)
    loss = criterion(outputs, labels)
    loss.backward()
    optimizer.step()
    train_time = time.time() - start_time

    cprint(f"PyTorch Coaching Time (ResNet-18, 1 Epoch): {train_time:.2f} seconds",'inexperienced','on_black')

    Consequence

    ii ) Tensorflow

    import tensorflow as tf
    import time
    from colorama import Fore, Again, Model
    from termcolor import coloured, cprint

    # Allow Apple Steel GPU acceleration
    tf.config.set_visible_devices([], 'GPU')

    # Load CIFAR-10 dataset
    (x_train, y_train), _ = tf.keras.datasets.cifar10.load_data()
    x_train, y_train = x_train / 255.0, y_train # Normalize pixel values

    # Convert labels to categorical format for TensorFlow
    y_train = tf.keras.utils.to_categorical(y_train, num_classes=10)

    # Load ResNet-50 mannequin (with out pre-trained weights)
    mannequin = tf.keras.purposes.ResNet50(
    input_shape=(32, 32, 3),
    weights=None, # No pre-trained weights
    lessons=10
    )

    # Compile mannequin
    mannequin.compile(
    optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),
    loss="categorical_crossentropy",
    metrics=["accuracy"]
    )

    # Prepare mannequin utilizing Apple Steel GPU
    start_time = time.time()
    mannequin.match(x_train, y_train, batch_size=64, epochs=1, verbose=1)
    train_time = time.time() - start_time

    cprint(f"TensorFlow Coaching Time (ResNet-50, 1 Epoch): {train_time:.2f} seconds")

    Consequence

    c. Inference Velocity Benchmark

    i ) Pytorch

    import torch
    import time
    from PIL import Picture
    from torchvision import transforms

    # Load a pattern picture
    picture = Picture.open("sample_image.jpg")
    rework = transforms.Compose([
    transforms.Resize((224, 224)),
    transforms.ToTensor(),
    ]
    picture = rework(picture).unsqueeze(0).to("mps")

    # Load ResNet mannequin
    mannequin = torch.hub.load('pytorch/imaginative and prescient:v0.10.0', 'resnet18', pretrained=True)
    mannequin = mannequin.to("mps")
    mannequin.eval()

    # Run inference
    start_time = time.time()
    output = mannequin(picture)
    inference_time = time.time() - start_time

    print(f"PyTorch Inference Time: {inference_time:.4f} seconds")

    Consequence

    ii ) Tensorflow

    import numpy as np
    import tensorflow as tf
    import time
    from termcolor import coloured, cprint

    # Create a random take a look at picture matching ResNet-50 enter dimensions
    picture = np.random.rand(1, 224, 224, 3).astype(np.float32) # Simulated enter

    # Load pre-trained ResNet-50
    mannequin = tf.keras.purposes.ResNet50(weights="imagenet")

    # Run inference on GPU
    start_time = time.time()
    output = mannequin.predict(picture)
    inference_time = time.time() - start_time

    cprint(f"TensorFlow Inference Time: {inference_time:.4f} seconds",)

    Consequence

    Takeaways:

    ✅ TensorFlow and PyTorch each profit from Apple Steel acceleration.
    ✅ PyTorch should still be barely sooner for coaching as a consequence of higher Steel optimization.
    ✅ For inference, each frameworks are quick on Apple Silicon.

    ✅ For Apple Silicon (M4, M3, M2, M1) — Good for light-weight deep studying duties however not supreme for large-scale coaching.
    🚀 For critical deep studying tasks, contemplate NVIDIA GPU on Google Colab, AWS, or a devoted workstation.



    Source link

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleHow Much MrBeast Paid to Create Amazon’s ‘Beast Games’
    Next Article Enhancing RAG: Beyond Vanilla Approaches
    FinanceStarGate

    Related Posts

    Machine Learning

    History of Artificial Intelligence: Key Milestones That Shaped the Future | by amol pawar | softAai Blogs | Jun, 2025

    June 13, 2025
    Machine Learning

    A First-Principles Guide to Multilingual Sentence Embeddings | by Tharunika L | Jun, 2025

    June 13, 2025
    Machine Learning

    Prediksi Kualitas Anggur dengan Random Forest — Panduan Lengkap dengan Python | by Gilang Andhika | Jun, 2025

    June 13, 2025
    Add A Comment

    Comments are closed.

    Top Posts

    How AI Is Revolutionizing Compliance Strategies

    February 11, 2025

    5 Key Takeaways From the 2025 IFA Convention

    February 23, 2025

    LLMs + Pandas: How I Use Generative AI to Generate Pandas DataFrame Summaries

    June 3, 2025

    Free Webinar | April 16: How to Cultivate, Grow and Monetize Your Social Audience

    April 2, 2025

    Circuit Tracing: A Step Closer to Understanding Large Language Models

    April 9, 2025
    Categories
    • AI Technology
    • Artificial Intelligence
    • Data Science
    • Finance
    • Machine Learning
    • Passive Income
    Most Popular

    Why Gen Z’s Demand for Ownership is Reshaping Social Media

    April 29, 2025

    Why Manual Data Entry Is Killing Estate Planning Productivity

    April 7, 2025

    FTC Sues Click Profit, Alleges Passive Income Amazon AI Scam

    March 18, 2025
    Our Picks

    Book Flights for Less with Matt’s Flights

    February 9, 2025

    Cut Software Costs Without Losing Essential Tools: MS Office Is on Sale for Life

    February 22, 2025

    Deploying a Production-Ready AI Model — Titanic Survival Predictor | by Akhilesh Veerapareddy | Feb, 2025

    February 24, 2025
    Categories
    • AI Technology
    • Artificial Intelligence
    • Data Science
    • Finance
    • Machine Learning
    • Passive Income
    • Privacy Policy
    • Disclaimer
    • Terms and Conditions
    • About us
    • Contact us
    Copyright © 2025 Financestargate.com All Rights Reserved.

    Type above and press Enter to search. Press Esc to cancel.