Debug School

rakesh kumar
rakesh kumar

Posted on

Machine Learning, Deep Learning, TensorFlow, and PyTorch: History, Purpose, Uses, and Examples

A beginner-friendly guide to what these technologies mean, why they appeared, and how they work together.

1. Why did we need machine learning?

Imagine building a vehicle-rental website. Some tasks have clear rules:

# Python — ordinary rule-based programming
price_per_day = 500
days = 3
print(price_per_day * days)
Enter fullscreen mode Exit fullscreen mode

Output

1500
Enter fullscreen mode Exit fullscreen mode

You know the rule, so you write it directly. No machine learning is needed.

Other tasks are harder to describe with fixed rules:

  • Is this uploaded photo a bike or a car?
  • Which vehicles might this customer prefer?
  • How much demand might there be next weekend?
  • Does a transaction resemble previous fraud?

Writing every possible rule by hand becomes difficult. Machine learning provides another approach: use examples to estimate a useful pattern.

Why it came: developers needed ways to solve problems where the rules were difficult to specify but relevant data was available.

Machine learning does not replace normal programming. A production application usually combines explicit business rules with learned predictions.

2. What are AI, machine learning, and deep learning?

Term Meaning in simple words Example
Artificial intelligence, or AI A broad field of systems that perform tasks associated with intelligent behavior Planning a route or recognizing speech
Machine learning, or ML Methods that learn patterns from data instead of relying only on hand-written rules Predicting rental demand from past bookings
Deep learning, or DL A part of ML based on neural networks with multiple processing layers Recognizing vehicle types in photos
Framework or library Software tools used to build and run models TensorFlow, PyTorch, scikit-learn

Important: TensorFlow and PyTorch are tools. They are not separate kinds of intelligence and are not individual machine-learning algorithms.

Diagram: how the ideas relate

flowchart TD
    AI["Artificial intelligence"] --> ML["Machine learning"]
    AI --> RULES["Other approaches: rules and planning"]
    ML --> CLASSIC["Other ML: regression, trees, clustering"]
    ML --> DL["Deep learning: multilayer neural networks"]
    DL --> TF["Can be implemented with TensorFlow"]
    DL --> PT["Can be implemented with PyTorch"]
    CLASSIC --> SK["Often implemented with scikit-learn"]
    classDef blue fill:#dbeafe,stroke:#2563eb,color:#172554;
    classDef purple fill:#f3e8ff,stroke:#9333ea,color:#581c87;
    classDef green fill:#dcfce7,stroke:#16a34a,color:#14532d;
    classDef orange fill:#ffedd5,stroke:#ea580c,color:#7c2d12;
    class AI,RULES blue;
    class ML,CLASSIC purple;
    class DL green;
    class TF,PT,SK orange;

The tool associations show common uses, not exclusive restrictions. TensorFlow and PyTorch can also implement models that are not deep neural networks.

Reference: Google — Introduction to Machine Learning.

3. A short history: when did these ideas appear?

There is no single invention date for the entire field. It developed through many contributions.

Period Milestone Why it mattered
1950s Early learning systems, including Arthur Samuel's checkers research; the term “machine learning” is associated with his 1959 work Demonstrated that performance could improve through experience
1986 Rumelhart, Hinton, and Williams published influential work on learning representations using backpropagation Helped explain how multilayer networks could adjust internal weights
1990s–2000s Statistical learning methods and larger datasets became widely useful Supported practical prediction, classification, and search systems
2012 AlexNet demonstrated strong ImageNet results using a deep convolutional network and GPU computation Helped accelerate interest in deep learning for computer vision
November 9, 2015 Google announced the open-source release of TensorFlow Made its machine-learning tooling available to a wider community
January 2017 PyTorch was publicly released Offered a flexible, Python-oriented approach to deep-learning development

Deep learning did not begin in 2012. Earlier ideas became more practical through better training methods, datasets, hardware, and software.

Sources: Samuel historical context, 1986 backpropagation paper, 2012 AlexNet paper, TensorFlow announcement, and PyTorch first anniversary.

4. Machine learning: what, why, purpose, and uses

What is machine learning?

Machine learning uses data to fit a model. A model is a mathematical function that produces an output from input values.

For example:

  • Inputs: city, vehicle type, rental date, and previous demand.
  • Output: estimated bookings for that day.

Training adjusts the model using historical examples. Prediction, also called inference, applies the fitted model to new inputs.

Why did it appear?

Some useful relationships are too complex to write as complete rules. Learning methods let developers estimate those relationships from observations.

Purpose

The purpose is to make useful predictions, discover structure, or support decisions. It is not to guarantee that every prediction is correct.

Main types

Type How it learns Example
Supervised learning Uses examples with known target answers Learn rental prices from vehicle attributes and observed prices
Unsupervised learning Looks for structure without target labels Group customers by similar booking behavior
Reinforcement learning Learns from actions and rewards in an environment Learn a control policy in a simulator

Self-supervised learning creates learning targets from the data itself. It is important in modern language and vision systems and is not simply another name for reinforcement learning.

When should you use ML?

Consider it when you have useful data, a measurable target, and a problem where fixed rules are insufficient. First compare against a simple baseline.

Advantages

  • Can capture patterns that are difficult to express manually.
  • Can rank or predict across many records.
  • Can be retrained when new data becomes available.
  • Supports tasks such as forecasting, recommendations, and classification.

Limitations

Poor data, biased examples, leakage, and changing conditions can produce unreliable predictions. Collecting data and maintaining a model can cost more than implementing a simple rule.

Runnable example: learn a simple rental relationship

Python dependency: scikit-learn. Run this in a Python environment where it is installed.

from sklearn.linear_model import LinearRegression

# Each row contains one feature: number of rental days.
X = [[1], [2], [3], [4]]
y = [500, 1000, 1500, 2000]

model = LinearRegression()
model.fit(X, y)

prediction = model.predict([[5]])[0]
print(f"Estimated rental: ₹{prediction:.0f}")
Enter fullscreen mode Exit fullscreen mode

Expected output

Estimated rental: ₹2500
Enter fullscreen mode Exit fullscreen mode

Explanation: the fitted line learns the relationship in these examples. This is deliberately simple teaching data, not proof of real-world accuracy.

Why ordinary code is better here: if your business explicitly charges ₹500 per day, multiplication is clearer and exact. The example demonstrates fitting; it does not justify replacing a known pricing rule with ML.

Reference: scikit-learn — Linear models.

5. Deep learning: what, why, purpose, and uses

What is deep learning?

Deep learning uses neural networks with multiple processing layers. Each layer transforms numbers, and training adjusts the network's parameters.

A neural network is not a human brain. It is a mathematical model inspired loosely by some ideas about connected processing units.

Why did it become useful?

Traditional workflows often require people to design input features. For a photo, that could mean manually defining measurements of edges, shapes, or colors.

Deep networks can learn useful intermediate representations from data. They still require appropriate architecture, preprocessing, objectives, and evaluation.

Purpose and common uses

  • Recognizing objects in photos.
  • Converting speech to text.
  • Translating or generating text.
  • Finding visual defects.
  • Learning representations for search and recommendations.

Architecture: a simplified vehicle-image classifier

flowchart TD
    IMG["Vehicle photo"] --> PRE["Resize and normalize"]
    PRE --> EARLY["Early layers: local visual patterns"]
    EARLY --> DEEP["Deeper layers: combined learned features"]
    DEEP --> HEAD["Classification layer"]
    HEAD --> BIKE["Bike score"]
    HEAD --> CAR["Car score"]
    HEAD --> OTHER["Other class score"]
    classDef blue fill:#dbeafe,stroke:#2563eb,color:#172554;
    classDef purple fill:#f3e8ff,stroke:#9333ea,color:#581c87;
    classDef green fill:#dcfce7,stroke:#16a34a,color:#14532d;
    class IMG,PRE blue;
    class EARLY,DEEP,HEAD purple;
    class BIKE,CAR,OTHER green;

This is a conceptual architecture, not a claim that every layer has a fixed human-interpretable meaning. Scores are not automatically calibrated probabilities.

When should you use it?

Consider deep learning for complex images, audio, text, or other tasks where learned representations help. A pretrained model may make it practical with a smaller task-specific dataset.

Advantages

  • Learns useful representations from complex inputs.
  • Supports transfer learning from pretrained models.
  • Can express complicated relationships.
  • Uses accelerator hardware for suitable workloads.

Limitations

Training can require substantial data, memory, compute, and experimentation. Predictions can be hard to explain. More layers do not automatically improve results. A simpler model may be better for a small structured dataset.

6. What is a library? What is a framework?

A library is reusable code you call to perform tasks. A framework supplies broader building blocks and conventions for organizing an application or workflow.

These terms overlap in ML. TensorFlow and PyTorch are often described as frameworks or libraries because they provide both numerical operations and higher-level model-building tools.

Why did ML frameworks appear?

Without them, developers would repeatedly implement matrix operations, gradients, GPU execution, layers, optimizers, and model storage.

Frameworks reduce that repeated engineering work. They do not choose correct data or guarantee a useful model.

Tool Main role Simple example
NumPy Numerical arrays and operations Calculate an average or multiply matrices
pandas Tabular data manipulation Clean a bookings table
Matplotlib Plotting Display prediction errors
scikit-learn Common ML algorithms and evaluation tools Train a regression model or decision tree
TensorFlow Numerical computation and ML workflows Build and train neural networks
PyTorch Tensor computation and automatic differentiation Implement and train custom neural networks
Keras Higher-level deep-learning API Build models with layers and a convenient training interface

Keras 3 supports multiple backends, including TensorFlow and PyTorch. It is not limited to TensorFlow. See Keras — About.

7. TensorFlow: history, purpose, uses, and advantages

What is TensorFlow?

TensorFlow is an open-source numerical-computing and machine-learning framework. Google announced its open-source release on November 9, 2015.

A tensor is a numerical array that may have zero, one, two, or more dimensions. “Flow” refers to computation involving those values.

Why did it come?

Teams needed reusable infrastructure for numerical models, training, and execution across different computing environments. TensorFlow made that infrastructure available beyond individual hand-written implementations.

Purpose and uses

Use it to define models, calculate gradients, train parameters, and run predictions. Its ecosystem includes tools for data processing, model development, and serving.

Advantages

  • High-level model development through Keras.
  • Automatic differentiation.
  • Accelerator support for suitable operations and supported environments.
  • A broad ecosystem for training and deployment.

Example: build a small deep network

This example uses two hidden layers. It performs a forward pass with deliberately assigned weights so the output is easy to explain. It does not train a model or demonstrate prediction quality.

Python dependency: tensorflow.

import numpy as np
import tensorflow as tf

model = tf.keras.Sequential([
    tf.keras.Input(shape=(1,)),
    tf.keras.layers.Dense(2, activation="relu"),
    tf.keras.layers.Dense(2, activation="relu"),
    tf.keras.layers.Dense(1)
])

# Teaching weights: copy the positive input along one path,
# then multiply it by 500 at the output.
model.set_weights([
    np.array([[1.0, 0.0]], dtype=np.float32),
    np.zeros(2, dtype=np.float32),
    np.eye(2, dtype=np.float32),
    np.zeros(2, dtype=np.float32),
    np.array([[500.0], [0.0]], dtype=np.float32),
    np.zeros(1, dtype=np.float32)
])

result = model(np.array([[5.0]], dtype=np.float32), training=False)
print(f"Network output: {float(result.numpy()[0, 0]):.0f}")
Enter fullscreen mode Exit fullscreen mode

Expected output

Network output: 2500
Enter fullscreen mode Exit fullscreen mode

Explanation: the input 5 passes through two hidden layers and is multiplied by 500. Because the weights are manually assigned, this is a demonstration of architecture and inference—not machine learning from examples.

In real training, you define a loss, choose an optimizer, and fit the weights on training data. Evaluate separately on unseen data. See the official TensorFlow beginner quickstart.

8. PyTorch: history, purpose, uses, and advantages

What is PyTorch?

PyTorch is an open-source tensor and machine-learning framework, originally developed at Facebook's AI research group and publicly released in January 2017.

Why did it come?

Researchers and engineers wanted flexible model development closely integrated with Python. PyTorch supports a direct programming style and automatic differentiation through executed operations.

Purpose and uses

Use it for numerical computation, custom neural networks, training loops, image models, language models, and other ML workloads.

Advantages

  • Direct Python-oriented model development.
  • Flexible custom training loops.
  • Automatic differentiation through autograd.
  • Accelerator support and a broad ecosystem.

Both TensorFlow and PyTorch can support research and production. Neither is universally faster, easier, or more accurate.

Example: the same architecture in PyTorch

Python dependency: torch.

import torch
from torch import nn

model = nn.Sequential(
    nn.Linear(1, 2),
    nn.ReLU(),
    nn.Linear(2, 2),
    nn.ReLU(),
    nn.Linear(2, 1)
)

# Assign the same conceptual transformation as the TensorFlow example.
# PyTorch Linear weights use the shape [outputs, inputs].
with torch.no_grad():
    model[0].weight.copy_(torch.tensor([[1.0], [0.0]]))
    model[0].bias.zero_()
    model[2].weight.copy_(torch.eye(2))
    model[2].bias.zero_()
    model[4].weight.copy_(torch.tensor([[500.0, 0.0]]))
    model[4].bias.zero_()

model.eval()
with torch.no_grad():
    result = model(torch.tensor([[5.0]]))

print(f"Network output: {result.item():.0f}")
Enter fullscreen mode Exit fullscreen mode

Expected output

Network output: 2500
Enter fullscreen mode Exit fullscreen mode

Explanation: this is the same small network calculation in another framework. The architecture has two hidden layers, but its weights were not learned.

model.eval() changes the behavior of layers such as dropout and batch normalization. It does not itself turn off gradient recording. The no_grad() context handles that in this example.

References: PyTorch tensors, PyTorch autograd, and PyTorch learning workflow.

9. What does training actually do?

The previous network examples use fixed weights. This small PyTorch example shows a parameter being learned instead.

Python — PyTorch, CPU example

import torch

# A toy relationship: target = 2 * input.
x = torch.tensor([1.0, 2.0, 3.0, 4.0])
y = torch.tensor([2.0, 4.0, 6.0, 8.0])

weight = torch.tensor(0.0, requires_grad=True)
optimizer = torch.optim.SGD([weight], lr=0.05)

for _ in range(100):
    prediction = weight * x
    loss = ((prediction - y) ** 2).mean()

    optimizer.zero_grad()
    loss.backward()
    optimizer.step()

with torch.no_grad():
    print(f"Learned weight: {weight.item():.2f}")
    print(f"Prediction for 5: {(weight * 5).item():.2f}")
Enter fullscreen mode Exit fullscreen mode

Expected output

Learned weight: 2.00
Prediction for 5: 10.00
Enter fullscreen mode Exit fullscreen mode

The prediction begins wrong. The loss measures its error. Backpropagation calculates how the parameter affects that error. The optimizer updates it. Repetition moves the weight toward 2.

This is a one-parameter ML example, not deep learning. It isolates the training mechanism without hiding it behind a large network. It also does not contain a real-world test set.

Diagram: the training feedback loop

flowchart TD
    DATA["Training examples and target answers"] --> MODEL["Model predicts using current weights"]
    MODEL --> LOSS["Loss compares prediction with target"]
    LOSS --> GRAD["Backpropagation computes gradients"]
    GRAD --> UPDATE["Optimizer updates weights"]
    UPDATE --> MODEL
    MODEL --> CHECK["Separate validation measures progress"]
    CHECK --> SELECT["Select model and test on held-out data"]
    classDef blue fill:#dbeafe,stroke:#2563eb,color:#172554;
    classDef purple fill:#f3e8ff,stroke:#9333ea,color:#581c87;
    classDef orange fill:#ffedd5,stroke:#ea580c,color:#7c2d12;
    classDef green fill:#dcfce7,stroke:#16a34a,color:#14532d;
    class DATA,MODEL blue;
    class LOSS,GRAD purple;
    class UPDATE orange;
    class CHECK,SELECT green;

Do not fit model parameters on the test set. Use validation for development choices and reserve the test set for final evaluation. For forecasting, split by time rather than randomly mixing future information into past training examples.

10. TensorFlow versus PyTorch

Question TensorFlow PyTorch
What is it? ML and numerical-computing framework ML and tensor-computing framework
Public release 2015 2017
High-level model building Commonly uses Keras Commonly uses torch.nn; Keras also supports a PyTorch backend
Automatic gradients Supported Supported through autograd
GPU support Available with suitable hardware/software Available with suitable hardware/software
Custom training Supported Supported
Research and production Both possible Both possible
Always faster? No No
Best choice Depends on model, team, infrastructure, and deployment requirements Depends on model, team, infrastructure, and deployment requirements

The old claim “TensorFlow is always static, PyTorch is always dynamic” is misleading. Modern workflows support eager execution and compiled or graph-based options.

Choose based on required libraries, existing model code, deployment targets, maintainability, and measurements—not a blanket ranking. Use the official installation instructions for compatible Python, operating-system, and accelerator versions.

11. Production architecture: vehicle-demand forecasting

Suppose a rental platform wants to estimate next week's demand by city and vehicle category.

Offline training and online prediction

flowchart TD
    subgraph TRAIN["Offline model development"]
        HISTORY["Historical bookings and relevant inputs"] --> CLEAN["Clean data and create time-based splits"]
        CLEAN --> FIT["Train baseline and candidate models"]
        FIT --> EVAL["Evaluate accuracy and business usefulness"]
        EVAL --> REG["Versioned model and preprocessing"]
    end
    subgraph SERVE["Application prediction"]
        UI["Web or mobile dashboard"] --> API["Authenticated backend API"]
        API --> INPUT["Validate input and prepare features"]
        INPUT --> PREDICT["Load approved model and predict"]
        PREDICT --> RESULT["Return estimate with model version"]
    end
    REG --> PREDICT
    RESULT --> MONITOR["Monitor errors, latency, and drift"]
    MONITOR -. "Inform a reviewed retraining cycle" .-> CLEAN
    classDef blue fill:#dbeafe,stroke:#2563eb,color:#172554;
    classDef purple fill:#f3e8ff,stroke:#9333ea,color:#581c87;
    classDef green fill:#dcfce7,stroke:#16a34a,color:#14532d;
    classDef orange fill:#ffedd5,stroke:#ea580c,color:#7c2d12;
    class HISTORY,CLEAN,UI,API blue;
    class FIT,EVAL,INPUT,PREDICT purple;
    class REG,RESULT green;
    class MONITOR orange;

Training is normally separated from ordinary user requests. A deployed model uses the same feature definitions and preprocessing used during development.

The API returns an estimate, not a guaranteed future outcome. Monitor real errors as actual bookings become available. Retraining should be evaluated before replacing the approved model.

What remains ordinary backend logic?

  • Who is allowed to access a city's or partner's data.
  • Whether a vehicle is actually available.
  • The final payable amount and valid discounts.
  • Database transactions for reservations.
  • Payment authorization and accounting.

A model prediction does not replace those controls.

12. When should you use each approach?

Situation Sensible starting point Why
Known rental formula Ordinary code Clear, exact, and easy to test
Small table of booking features Simple statistical or classical ML baseline Easier to establish useful performance
Vehicle-photo classification Consider pretrained deep vision models Learned visual representations can help
Text similarity or semantic search Consider pretrained text embeddings Avoid training a language model from scratch
Very little useful data Rules or data collection first A complex model cannot compensate for missing evidence
Strict latency or cost constraints Small model, caching, or simpler rules Operational cost may dominate tiny accuracy gains

These are starting points, not universal winners. Compare on your own data and constraints.

13. Common beginner mistakes

  1. Calling every automated system machine learning.
  2. Assuming deep learning is always better than simpler ML.
  3. Using training accuracy as evidence of real-world quality.
  4. Allowing future information or duplicate examples to leak into evaluation.
  5. Training from scratch when a suitable pretrained model is available.
  6. Assuming a GPU helps every workload; tiny jobs may be faster or simpler on CPU.
  7. Comparing frameworks using different models, batch sizes, or hardware.
  8. Ignoring privacy, consent, bias, operating cost, and maintenance.
  9. Loading untrusted model artifacts without checking the format and provenance.
  10. Automatically deploying retrained models without evaluation.

14. A simple learning path

  1. Learn Python functions, lists, dictionaries, and basic classes.
  2. Learn NumPy arrays and basic data preparation.
  3. Learn train/validation/test separation and appropriate metrics.
  4. Build a simple scikit-learn model and compare it with a baseline.
  5. Learn tensors, weights, losses, gradients, and optimizers.
  6. Implement a small neural network in TensorFlow or PyTorch.
  7. Try transfer learning on a realistic task.
  8. Serve predictions behind an API and monitor quality and cost.

Learn enough statistics and linear algebra to understand your models. You do not need to master every mathematical topic before writing your first example.

15. Final summary

Topic Why it appeared Purpose Main advantage Main limitation
Machine learning Fixed rules were difficult for some pattern-based problems Learn useful relationships from examples Data-driven prediction Depends on data and evaluation
Deep learning Complex inputs benefit from learned representations Fit multilayer neural networks Strong representation learning for suitable tasks Compute, data, and complexity costs
TensorFlow Developers needed reusable ML computation and workflows Build, train, and execute models High-level APIs and broad tooling Still requires correct modeling and operations
PyTorch Developers wanted flexible Python-oriented model development Tensor computation, gradients, and model training Direct custom programming Still requires correct modeling and operations
Supporting libraries Repeated data and numerical work needed reusable tools Prepare, analyze, and visualize data Less repeated engineering work Tools cannot fix poor assumptions

Use ordinary code when the rule is known. Use ML when learning from data solves a measurable problem. Use deep learning when its added complexity is justified. Pick the framework that fits your requirements, then verify the result.

References and code notes

Code examples use synthetic data and are educational, not production-ready models. Expected outputs are explained from the code; framework examples were not executed in this environment. Formatting and floating-point details can vary. Mermaid diagrams require a Markdown viewer that supports Mermaid; ordinary viewers may display their source instead.

Top comments (0)