PyTorch

Python · Advanced Python & AI

Theory

PyTorch is an open-source deep learning framework created at Meta and, since 2022, governed by the vendor-neutral PyTorch Foundation (part of the Linux Foundation); it is very popular in research. Its core object is the tensor, which behaves much like a NumPy array but can run on a GPU and track gradients. Models are built from modules in torch.nn: in the example, nn.Sequential chains nn.Linear(10, 16) (10 inputs to 16 neurons), an nn.ReLU activation and nn.Linear(16, 1) for a single output.

PyTorch is 'define-by-run': the computation graph is built as the code executes, so ordinary Python if-statements and loops work inside models and debugging feels like normal Python. Training is an explicit loop you write: run the forward pass, compute the loss (nn.MSELoss), call loss.backward() so autograd computes gradients, call optimizer.step() to update weights, and optimizer.zero_grad() to clear gradients before the next batch.

Why we use it

It gives full, transparent control over how models are built and trained while staying Pythonic. That flexibility makes it easy to experiment with new architectures, which is why much modern AI research uses it.

When to use it

Use PyTorch for deep learning when you want flexibility, custom architectures or access to research models. For a quick standard network Keras can be shorter; for ordinary tabular problems scikit-learn is usually enough.

How it works

  1. Get a batch of input tensors and targets
  2. Forward pass: predictions = model(x)
  3. Compute loss = loss_fn(predictions, y)
  4. loss.backward() computes gradients
  5. optimizer.step() updates weights; optimizer.zero_grad() clears gradients
  6. More batches or epochs? Yes: Repeat the loop. No: Stop training.
  7. Evaluate with torch.no_grad()

Syntax

import torch
import torch.nn as nn
model = nn.Sequential(nn.Linear(10, 1))

Example

import torch.nn as nn
model = nn.Sequential(
    nn.Linear(10, 16), nn.ReLU(), nn.Linear(16, 1)
)

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic