0% found this document useful (0 votes)
7 views40 pages

PyTorch Tensors: Basics & Creation Guide

The document covers the basics of PyTorch, focusing on tensors as the fundamental data structure essential for deep learning. It explains how to create, manipulate, and understand tensors, including their dimensions and types, as well as the advantages of using PyTorch over other libraries like NumPy. Additionally, it provides practical examples and homework exercises to reinforce learning.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views40 pages

PyTorch Tensors: Basics & Creation Guide

The document covers the basics of PyTorch, focusing on tensors as the fundamental data structure essential for deep learning. It explains how to create, manipulate, and understand tensors, including their dimensions and types, as well as the advantages of using PyTorch over other libraries like NumPy. Additionally, it provides practical examples and homework exercises to reinforce learning.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

GenAI Week1 - PyTorch Basics

🔥 Topic: PyTorch Tensors and Why They Matter

Tensor ek basic data structure hai PyTorch ka.


Ye wahi concept hai jese array/matrix hota hai — lekin more powerful.
Deep Learning me har input, output, weight, feature = sab kuch tensor hota hai.
PyTorch me fluent hone ka matlab hai:
Tensor banana
Tensor ko reshape karna
GPU pe move karna
Debug karna jab error aaye

🧠 Yaad rakhne layak:


Tensor = Deep learning ki language
Agar ye aata hai toh model banana, samajhna, aur debug karna easy ho jata hai.

Agar tumne NumPy use kiya hai toh good news — PyTorch ka tensor similar hota hai NumPy
array jaisa.
PyTorch extra features deta hai:
GPU acceleration (Fast training)
Automatic differentiation (for backpropagation)

💡 Bonus:
1/40
Linear Algebra ke basics — jaise matrix multiplication, vector addition — agar aate hain toh
aur easy ho jayega.

⚙️ 1:45 – [Link] What is PyTorch?


✍️ PyTorch ek combo hai:
Scientific computing library (like NumPy)
Deep learning framework (like TensorFlow)

📦 Use cases:
Data cleaning
Loss function define karna
Neural network banana
Simulations aur research

🔥 Kyon PyTorch sikhna zaroori hai?


Academia + Industry, dono me use hota hai
Open-source projects me zyada tar PyTorch code milta hai
Reviewers, interviewers — sab familiar hote hain PyTorch se

🧪 Example:
python

import torch

# ek simple tensor
x = [Link]([1.0, 2.0, 3.0])
print(x)

📊 3:50 – [Link] What are Tensors (Deeply Explained)


🚀 Visualize like this:
Name Dimensions Example PyTorch Shape

Scalar 0D 5 [Link](5)

2/40
Name Dimensions Example PyTorch Shape

Vector 1D [3, 4, 5] [Link]([3, 4,


5])

Matrix 2D [[1, 2], [3, 4]] [Link]([[1,2],


[3,4]])

3D Tensor 3D RGB Image [height, width,


channel]

4D Tensor 4D Video [frame, height,


width, channel]

5D Tensor 5D Batch of videos [batch, frame,


height, width,
channel]

🧠 Real-Life Example:
Grayscale image = 2D tensor (height x width)
RGB image = 3D tensor (height x width x 3)
Video = 4D (frames x height x width x 3)
Batch of videos = 5D

🔍 5:18 – [Link] Roadmap of Tensor Learning


Step What You'll Learn

Creation Tensor kaise banate hain

Introspection Tensor ka shape, type, size check


karna

Reshaping View, reshape, unsqueeze/squeeze


operations

Indexing Specific elements access karna

3/40
Step What You'll Learn

GPU Acceleration .to('cuda') ka use for faster


computation

Ye sab steps real-life model training me directly use hote hain.

✍️ 6:00 – [Link] Mathematical Notation for Tensors


Type Symbol Format Example Notation

Scalar Small case a ∈ ℝ

Vector Bold lowercase **x** ∈ ℝⁿ

Matrix Bold UPPERCASE **X** ∈ ℝ^(m×n)

Tensor With shape **T** ∈


ℝ^(d₁×d₂×...×dₙ)

🧠 Example:
A 3x3 matrix:
X ∈ ℝ^(3×3)
A 28x28 grayscale image:
I ∈ ℝ^(28×28)
A batch of 64 RGB images (3 channels):
B ∈ ℝ^(64×3×28×28)

✅ Summary in Simple Hinglish:


Concept Samajh

Tensor kya hai? Data container jisme numbers hote


hain

Kitne dimensions hote Scalar (0D), Vector (1D), Matrix


hain? (2D), Tensor (ND)

PyTorch kyon use GPU support, automatic grad,


karein? NumPy jaisa

4/40
Concept Samajh

Tensor ka use kahaan Har jagah — input, output, weight,


hota hai? loss sab me

Sikhe kya? Creation, reshape, move to GPU,


troubleshoot

📌 Homework (Practice with Code):


python

import torch

# 1D Vector
v = [Link]([10, 20, 30])
print([Link]) # [Link]([3])

# 2D Matrix
m = [Link]([[1, 2], [3, 4]])
print([Link]) # [Link]([2, 2])

# 3D Tensor (e.g. RGB image with height=2, width=2, channels=3)


t = [Link](2, 2, 3)
print([Link]) # [Link]([2, 2, 3])

5/40
🔥 Part 2: Tensor Creation in PyTorch
✅ Step 1: Basic Setup
python

import torch
import numpy as np
print(torch.__version__)

Yeh 2 libraries import karna zaroori hai:


torch: PyTorch ke functions ke liye
numpy: conversion ke liye jab numpy arrays se tensor banana ho

🎯 Step 2: Tensor Creation from Python Lists


📌 Types of Tensors with Python Lists:
Python Object Tensor Type PyTorch Code Example

[Link](7) Scalar (0D) Ek single number

[Link]([1,2 Vector (1D) List of numbers


,3])

[Link]([[1, Matrix (2D) Nested list of lists


2],[3,4]])

3D List (List of list of Tensor (3D) Complex data like RGB


list) images/videos

🧪 Example:

6/40
python

# Scalar
s = [Link](7)

# Vector
v = [Link]([1, 2, 3])

# Matrix
m = [Link]([[1, 2], [3, 4]])

# 3D Tensor
t = [Link]([[[1,2],[3,4]], [[5,6],[7,8]]])

🔍 Step 3: Shape and Dimensions


python

print([Link]) # Shape batata hai (2, 2, 2)


print([Link]()) # Dimension count karega (3D)

🧠 Tip:
.shape : Each dimension me kitne elements
.dim() : Kitni dimensions hain

⚙️ Step 4: Data Types (dtype)


📌 Common Data Types:
Data Type Bytes Use for

torch.float32 4 Default for floats

torch.float64 8 High precision floats

torch.int32 4 Integers

torch.int64 8 Long integers

7/40
Data Type Bytes Use for

[Link] 1 True/False values

🧪 Example:
python

a = [Link]([1.0, 2.0], dtype=torch.float32)


b = [Link]([1.0, 2.0], dtype=torch.float64)
c = [Link]([1, 2, 3], dtype=torch.int32)
d = [Link]([True, False, True], dtype=[Link])

print(a.element_size()) # 4 bytes
print(b.element_size()) # 8 bytes
print(c.element_size()) # 4 bytes
print(d.element_size()) # 1 byte

🔄 Step 5: Convert NumPy to Tensor (vice versa bhi hota hai)


python

np_array = [Link]([[1, 2], [3, 4]])


tensor_from_np = torch.from_numpy(np_array)

# View behavior: if you change numpy array, tensor changes too


np_array[0, 0] = 100
print(tensor_from_np)

💡 Important Concept:
torch.from_numpy() gives a view, not a copy.
Agar NumPy array change hua, PyTorch tensor bhi change ho jayega.
Use [Link](np_array.copy()) agar independent tensor chahiye.

🧰 Step 6: Factory Functions

8/40
Function Description Example

[Link]() All zeros tensor [Link](3, 3)

[Link]() All ones tensor [Link](2, 4)

[Link]() Garbage values (fast allocation, [Link](2, 3)


uninitialized)

[Link]() Fill tensor with specific value [Link]((3,2),


42)

📌 Masking Example:
python

mask = [Link](5)
mask[1:3] = 1

data = [Link]([10, 20, 30, 40, 50])


selected = data * mask # Only values at index 1 and 2 remain

🔢 Step 7: Creating Sequential Tensors


🧪 Examples:
python

# Like range()
[Link](0, 10) # 0 to 9
[Link](0, 10, 2) # 0, 2, 4, 6, 8

# Even spacing (linspace)


[Link](0, 1, 5) # [0.00, 0.25, 0.5, 0.75, 1.0]

# Log spacing
[Link](1, 4, 5) # [10^1, ..., 10^4]

🧠 Tip:
9/40
linspace includes both start and end
arange excludes end value (like Python range)

🔁 Step 8: Identity and Diagonal Matrices


🧪 Examples:
python

# Identity matrix (3x3)


[Link](3)

# Identity-like (2x4)
[Link](2, 4)

# Diagonal from vector


v = [Link]([1, 2, 3])
diag_matrix = [Link](v)

# Extract diagonal from matrix


m = [Link]([[1,2,3],[4,5,6],[7,8,9]])
diag_elements = [Link](m)

📌 Quick Revision Table


Task Function

Zeros Tensor [Link](shape)

Ones Tensor [Link](shape)

Uninitialized Tensor [Link](shape)

Fixed-value Tensor [Link](shape,


val)

Sequential Range [Link]()

Even Spacing [Link]()

10/40
Task Function

Log Spacing [Link]()

Identity Matrix [Link](n)

Diagonal from Vector [Link](vec)

Diagonal from Matrix [Link](matrix)

From NumPy torch.from_numpy()

🧠 Real-Life Analogy
💡 zeros, ones, full: Blank notebooks with predefined content
📏 arange, linspace: Number lines (rulers) with equal spacing
🧬 diag, eye: DNA patterns jahan diagonal me values chahiye
🔄 from_numpy: Convert borrowed data into PyTorch-friendly format

🏁 Homework Practice
1. Create a 5×5 tensor filled with 9.
2. Create a 4D tensor with shape (2,3,4,5) with random values.
3. Create identity matrix of size 6×6 and extract diagonal.
4. Convert a numpy array to torch tensor, modify numpy array and observe tensor.

11/40
🧮 PART 1: Random Tensor Generation
📌 Use Cases:
Neural Network ka weight initialization
Data augmentation (noise add karna)
Monte Carlo simulation
Synthetic data banana for testing
Variability / randomness model karna (e.g., dropout)

🛠️ 1. Random Uniform Distribution


python

rand_tensor = [Link](3, 4) # 3 rows × 4 columns

⚙️ Description:
Ye values 0 to 1 ke beech me random hoti hain.
Float32 by default
Uniform distribution ka matlab: sab value aane ka equal chance

python

print(rand_tensor.shape) # (3, 4)
print(rand_tensor.dtype) # torch.float32
print(rand_tensor.min()) # Min value
print(rand_tensor.max()) # Max value
print(rand_tensor.mean()) # Mean (expected ~0.5)
print(rand_tensor.std()) # Standard deviation (~0.288)

📏 Theory: Uniform [0, 1]


Mean = 0.5
Standard deviation (σ) = √(1/12) ≈ 0.2887

12/40
📸 2. Image-Like Random Tensor
python

img_tensor = [Link](224, 224, 3)

⚙️ Explanation:
RGB image: 224x224 pixels, 3 channels
Legacy of ImageNet: most pretrained CNNs expect 224×224 inputs

📐 Memory Check:
python

print(img_tensor.element_size()) # 4 bytes per float32


print(img_tensor.nelement()) # 150,528 elements
print(img_tensor.element_size() * img_tensor.nelement()) # ~0.57 MB

📌 Real-Life Analogy:
Jese tum ek TV screen ke pixels ko random colors se fill kar rahe ho testing ke liye.

🛠️ 3. Random Tensor for Augmentation


python

noise = torch.rand_like(img_tensor)
noisy_image = img_tensor + 0.1 * noise # Add little noise

🔢 PART 2: Tensor with Sequential Values


📌 1. [Link]
python

13/40
[Link](0, 10) # 0 to 9
[Link](0, 12, 2) # Step size = 2

📌 2. [Link]
python

[Link](0, 1, 5) # 5 values from 0 to 1 (inclusive)

📌 3. [Link]
python

[Link](1, 4, 5) # 10^1 to 10^4 => 10, 100, 1000, 10000

🧠 Summary Table
Function Description

arange Like range() , but


returns a tensor

linspace Even spacing between


values

logspace Logarithmic spacing


between 10^a to 10^b

🧰 PART 3: Create Tensor Based on Another Tensor


python

a = [Link](2, 3)
b = torch.zeros_like(a)
c = torch.ones_like(a)

Ye functions help karte hain jab shape same chahiye but value alag.

14/40
🔄 PART 4: Data Types and Requires Grad
python

t1 = [Link]([3.0, 6.0, 9.0], dtype=torch.float32, requires_grad=False)


t2 = [Link]([3, 6, 9], dtype=torch.int64)

💡 Use Case:
requires_grad=True => for automatic gradient calculation in backprop

➕ PART 5: Basic Arithmetic on Tensors


python

x = [Link]([1, 2, 3])
print(x + 10) # Adds 10 to each element => [11, 12, 13]
print(x - 5) # Subtracts 5 => [-4, -3, -2]
print(x * 2) # Multiply => [2, 4, 6]
print(x / 2) # Divide => [0.5, 1.0, 1.5]

🔁 Reassignment
python

x += 10
print(x) # Now x is modified

⚙️ PART 6: Element-Wise vs Matrix Multiplication


⚡ Element-wise
python

x = [Link]([11, 12, 13])


print(x * x) # => [121, 144, 169]

🧮 PART 7: Matrix Multiplication


15/40
📚 Theory:
If A = [m × n], B = [n × p], then:
Output C = [m × p]
Inner dimensions must match!

🧪 Code:
python

A = [Link]([[1, 2], [3, 4]])


B = [Link]([[5, 6], [7, 8]])

# Method 1
C = [Link](A, B)

# Method 2 (only for 2D)


C=A@B

# Method 3 (manual)
C = [Link](A, B)

print(C)

📌 Dot Product (special case):


python

x = [Link]([11, 12, 13])


dot = [Link](x, x) # => 11^2 + 12^2 + 13^2 = 434

🤯 Real Meaning:
Dot product = Similarity
High value = similar direction
Neural networks use this in linear layers, attention, gradient calc

🧠 Mind Map Summary


yaml

16/40
Tensors
├── Random: rand, rand_like
│ ├── Uniform [0,1], float32
│ ├── Use: noise, augmentation, init
├── Sequential: arange, linspace, logspace
│ ├── Use: indexing, counters
├── Arithmetic
│ ├── Add, Sub, Mul, Div
│ ├── Reassignment
├── Matrix Ops
│ ├── Element-wise (*)
│ ├── Matrix Multiplication (@ or matmul)
│ └── Dot Product (dot)

🧪 Homework Practice
1. Create a random tensor of shape (5, 5) and calculate min, max, mean, std.
2. Generate linspace values between 2 and 10 with step 2.
3. Make two 2×3 and 3×2 tensors, multiply them and verify shape.
4. Use dot() on any two vectors and explain result in terms of similarity.

17/40
🧱 1. Reshape a Tensor
python

x = [Link]([1, 2, 3, 4, 5, 6, 7])
x_reshaped = [Link](1, 7)

📌 Points:
.reshape(a, b) : New shape
Doesn't change data, just arrangement
Very useful in matrix multiplication where dimensions must match

👓 2. .view() vs .reshape()

python

z = [Link](1, 7)

🔍 Difference:
reshape() may return a copy (not guaranteed)

view() is a new view of same memory

Changing z will also change x

python

z[0][0] = 99
print(x) # x[0] is also 99 now

🧱 3. Stacking Tensors
python

x = [Link]([1, 2, 3, 4])
stacked_0 = [Link]([x, x, x, x], dim=0)
stacked_1 = [Link]([x, x, x, x], dim=1)

18/40
💡 dim=0 → stack row-wise

💡 dim=1 → stack column-wise

plaintext

dim=0 → 4x4
dim=1 → 4x4 (but columns come from different tensors)

✂️ 4. Squeeze — Remove Useless Dimension


python

x = [Link]([[1, 2, 3, 4]]) # shape (1, 4)


x_squeezed = [Link]()

💡 Use:
Remove any dimension with size 1
Useful when shapes are like (1, N) or (N, 1) but you want just (N,)

➕ 5. Unsqueeze — Add New Dimension


python

x = [Link]([1, 2, 3, 4])
x_unsq = [Link](0) # shape becomes (1, 4)

📌 Use:
Add dimension where needed for broadcasting or matrix mult

python

[Link](1).shape # (4, 1)

🔄 6. Permute — Reorder Dimensions


python

19/40
img = [Link](224, 224, 3) # HWC
img_p = [Link](2, 0, 1) # CHW

📌 Why:
PyTorch expects input as (Channel, Height, Width)
Image libraries like OpenCV/PIL give (Height, Width, Channel)

🔍 7. Indexing Tensors
python

x = [Link](1, 10).reshape(1, 3, 3)

📐 Shape:
1 matrix of 3x3

Examples:

python

x[0] # 3x3 matrix


x[0, 0] # first row
x[0, :, 1] # second column (of all rows)
x[0, 1, 2] # single element (row 1, col 2)

🧠 Real-World Analogy:
Tensor = Excel sheet (multi-dimensional)
: means "select all"
x[0, :, 2] means "get all rows in sheet 0, column 2"

🧠 Visual Table: All in One

20/40
Operation Function Result Shape

Reshape [Link](a, b) (a, b)

View [Link](a, b) (a, b)

Stack [Link]([x]* (n, [Link][0])


n)

Squeeze [Link]() removes dim=1

Unsqueeze [Link](dim) adds dim

Permute [Link](...) reorders dims

Indexing x[a, b, c] specific value/s

🎯 Real Use-Cases Recap


Feature Use Case Example

reshape() Fix shape mismatch before matmul

view() Efficient reshaping without copying

stack() Combine batches of data

squeeze() Remove dummy dimensions from


model output

unsqueeze() Add batch/channel dimension


before CNNs

permute() Change image from HWC to CHW

indexing Extract specific slice or pixel (for


masks, selections etc)

🧪 Homework Practice
1. Create a tensor of shape (1, 3, 3) and try:

21/40
.squeeze()
.unsqueeze(0)

.permute(2, 0, 1) (after reshaping)


2. Stack 5 copies of a 1D tensor both row-wise and column-wise
3. Permute a 4D tensor (batch, height, width, channels) to (batch, channels,
height, width)
4. Index the center value of a 3x3 matrix inside a (1, 3, 3) tensor

Next:
🎯 GPU Acceleration → , check availability, copy data
.to("cuda")
🎯 Broadcasting → Tensor arithmetic with mismatched shapes
Bhai chahiye toh in sabka revision PDF + worksheet bhi bana deta hoon. Batao.

Bhai, ab hum aa gaye hain ek practical aur super useful topic pe —

🔁 Interoperability: PyTorch Tensors ↔ NumPy Arrays


Isme hum deeply samjhenge ki:
Kaise NumPy array ko PyTorch tensor me convert karte hain
Kaise PyTorch tensor ko wapas NumPy array me banate hain
Kab aur kyon yeh conversion zaroori hoti hai

🎯 Kyun Zaroori Hai?


Machine learning pipeline me kai baar aisa hota hai ki:
Data pre-processing NumPy ya Pandas me hota hai
Modeling PyTorch me hoti hai
Tab tumhe data ko convert karna padta hai:
NumPy → PyTorch
PyTorch → NumPy

🔄 NumPy to PyTorch
🧪 Example:
22/40
python

import numpy as np
import torch

np_array = [Link]([1, 2, 3, 4, 5, 6, 7])


tensor_from_np = torch.from_numpy(np_array)

print(tensor_from_np)
print(tensor_from_np.shape) # [Link]([7])
print(tensor_from_np.dtype) # torch.float64 / torch.int64 (depends on np_array)

⚠️ Important:
torch.from_numpy() returns a view, not a copy
Matlab agar np_array me change karoge, tensor_from_np bhi change ho jayega

🔁 PyTorch to NumPy
🧪 Example:
python

tensor = [Link]([10, 20, 30, 40])


np_from_tensor = [Link]()

print(np_from_tensor)
print(np_from_tensor.shape) # (4,)
print(np_from_tensor.dtype) # int64

⚠️ Important:
.numpy() bhi ek view deta hai
Agar tensor change hua, toh np_from_tensor bhi change hoga (and vice versa)

🧠 Real-Life Analogy
Socho NumPy ek Hindi speaker hai aur PyTorch ek English speaker
Tum dono ko ek translator chahiye jo ek hi sentence ko Hindi aur English dono me samjha

23/40
sake — bina dobara likhe.
Yeh translator = view!

🔬 Deep Tip: Type Casting and Shapes


python

np_array = [Link]([1.0, 2.0, 3.0], dtype=np.float32)


tensor = torch.from_numpy(np_array)

print([Link]) # torch.float32

python

tensor = [Link]([1, 2, 3])


np_array = [Link]()
print(np_array.dtype) # int64 (same as tensor)

🛑 Common Pitfalls
Mistake What Happens?

Change NumPy array PyTorch tensor bhi change ho


after conversion jayega

Use tensor with ❌ Error: Tensors with gradients


gradients → can't use .numpy()
.numpy()

Mismatch in dtype Model training may crash or slow


(float64 vs float32) down

✅ Recommended Way (Safe Copy)


If you want independent copy, not view:

python

24/40
# NumPy → PyTorch (copy)
tensor = [Link](np_array.copy())

# PyTorch → NumPy (copy)


np_array = [Link]().detach().numpy()

📌 Summary Table
Operation Function

NumPy → PyTorch torch.from_numpy()

PyTorch → NumPy [Link]()

Safe Copy [Link]([Link]


(NumPy→Torch) ())

Safe Copy [Link]().detac


(Torch→NumPy) h().numpy()

🎯 Use-Cases
Use Case PyTorch or NumPy?

CSV/Data NumPy / Pandas


Preprocessing

Feature Extraction from NumPy


raw signals

Model Training PyTorch

Visualization NumPy preferred


(Matplotlib, etc.)

🧪 Homework Practice

25/40
1. Convert NumPy array of shape (3, 3) to PyTorch tensor, modify one value in NumPy, and
observe change in tensor
2. Convert PyTorch tensor to NumPy array, modify tensor, and see if NumPy changes too
3. Create a safe, independent copy while converting and verify they are separate

📌 Reproducibility — Matlab Kya?


Definition:
Machine Learning me reproducibility ka matlab hai:

"Agar kal ya koi dusra banda same code, same data, same settings use kare… to same results
aaye."

🧪 PyTorch Me Problem Kya Hai?


By default agar tum aise code likho:

python

import torch
A = [Link](2, 2)

26/40
print(A)

To har baar naya output aayega — kyuki yeh random tensor hai.
Lekin agar tum research paper likh rahe ho, ya production model deploy kar rahe ho, ya
debug kar rahe ho —
Random output se kaam nahi chalega.

⚠️ Reproducibility Kyun Important Hai?


Reason Kya Fayda Milega?

🔧 Debugging Bar bar same run se bug trace kar


sakte ho

🧪 Model Comparison Fair comparison (same data splits)

📚 Scientific Research Duniya verify kar paye tumhara


result

🔁 Team Koi aur same model train kar sake


Collaboration

🏭 Production Retrain with same config during


Systems data drift

✅ Solution: Seed Set Karo


🔢 torch.manual_seed(seed)

Ye set karta hai CPU ke random numbers ke liye fixed sequence.

python

import torch

torch.manual_seed(42)
A = [Link](2, 2)
print(A)

Har baar same result milega.

27/40
⚡ For GPU: [Link].manual_seed(seed)

Agar CUDA GPU use kar rahe ho, to alag seed set karna padta hai:

python

if [Link].is_available():
[Link].manual_seed(42)

Full Code:

python

import torch

seed = 42
torch.manual_seed(seed)

if [Link].is_available():
[Link].manual_seed(seed)

# Now generate
print([Link](2, 2)) # CPU
print([Link](2, 2).cuda()) # GPU (if available)

🧠 Real Life Analogy:


Socho ek random number generator ek shuffled deck of cards hai.
Har baar naye sequence milte hain.
Lekin agar tum deck ko same tareeke se shuffle karo (same seed), to same order milega.
Seed = Shuffle Style Identifier

🔐 Ek Important Note:
Reproducibility perfect nahi hoti jab:
Multi-GPU kaam karta hai (parallelism ka randomness hota hai)
CuDNN backend ka non-deterministic operations use ho jaata hai

28/40
Agar 100% determinism chahiye:

python

torch.use_deterministic_algorithms(True)

⚠️ Yeh slow kar sakta hai training ko, but reproducibility milega.

🧪 Tumhare Notebook Wale Example Summary:


python

import torch

# CPU reproducibility
torch.manual_seed(42)

# GPU reproducibility
if [Link].is_available():
[Link].manual_seed(42)

# Generate tensor
A = [Link](2, 2)
print(A)

Output har baar same aayega agar seed fix hai.

📦 Summary Cheatsheet:
Task Command

CPU Random Fix torch.manual_seed(seed)

GPU Random Fix [Link].manual_seed(seed


)

Fully deterministic torch.use_deterministic_alg


algorithms orithms(True)

29/40
Task Command

Why needed? Debugging, Comparisons,


Deployment, Research

🧪 Try This as Practice:


python

for i in range(3):
torch.manual_seed(42)
print([Link](0, 10, (5,)))

Har loop me same 5 integers print honge!

🔥 Why Use GPU in PyTorch?


CPU is good, but GPU is a beast when it comes to matrix calculations like:

Training deep learning models


Large matrix multiplications
Backpropagation, etc.

⚙️ GPUs are:
Parallel processing machines (can do 1000s of tasks at once)
Super optimized for vectorized and matrix ops

30/40
🖥️ Step 1: Check If GPU is Available
python

import torch

print([Link].is_available())

Output:
✅ → GPU is available
True
❌ → GPU not available (you’re stuck with CPU)
False

💡 Ye check karta hai ki tumhare system me CUDA-compatible GPU hai ya nahi.

🚀 Step 2: Check Which GPU You Have


On Linux or Google Colab, you can run shell commands using !

python

!nvidia-smi

This gives:
GPU Name (like A100 , T4 , RTX 3080 )
GPU Memory
Driver version
Usage stats

📌 Note: On Colab, drivers and CUDA are already installed. On your own system, you must
install CUDA Toolkit .

🔍 Step 3: Explore GPU Details


python

print([Link].device_count()) # No. of GPUs


print([Link].current_device()) # Current device ID
print([Link].get_device_name(0)) # GPU name

31/40
print([Link].memory_allocated(0)) # Used memory
print([Link].memory_reserved(0)) # Reserved memory

📊 If you want total memory:


python

total = [Link].get_device_properties(0).total_memory
print(total)

💡 Convert bytes to GB:


python

print(total / (1024 ** 3), "GB")

🧪 Step 4: Use .to() to Send Tensors to GPU

✅ Create CPU Tensor:


python

tensor_cpu = [Link]([1.0, 2.0, 3.0])

✅ Move to GPU:
python

tensor_gpu = tensor_cpu.to('cuda')

You can also use:

python

tensor_gpu = tensor_cpu.cuda()

📌 cuda() is just a shortcut to .to('cuda')

❗ Numpy Conversion Error


If you try:

32/40
python

tensor_gpu.numpy()

You'll get:

vbnet

RuntimeError: Can't convert CUDA tensor to numpy. Use [Link]() to copy it to host memory first.

🧠 Why?
Because NumPy only works with CPU memory.

✅ Fix: Move It Back to CPU First


python

tensor_back_to_cpu = tensor_gpu.cpu()
array = tensor_back_to_cpu.numpy()
print(array)

🎯 This is useful when you:


Do training on GPU
Then move results back to CPU
Convert to NumPy for visualization or saving to CSV

🔁 Summary Table
Task Code

Check GPU [Link].is_availa


ble()

Move to GPU [Link]('cuda') or


[Link]()

Move to CPU [Link]('cpu') or


[Link]()

33/40
Task Code

Convert to NumPy [Link]().numpy(


)

⚠️ Real-World Tip: Use device Variable


So you don’t hardcode "cuda" or "cpu" :

python

device = 'cuda' if [Link].is_available() else 'cpu'


tensor = [Link](device)

That way your code will work on:


GPU machines (training will be fast)
CPU-only machines (like laptops)

💡 Extra: PyTorch, CUDA, cuDNN Version Check


python

print(torch.__version__) # PyTorch version


print([Link]) # CUDA version
print([Link]()) # cuDNN version

👉 Useful when you download pre-trained models or use third-party libraries that need specific
versions.

⚡ GPU Benefits Recap:


Benefit Description

🚀 Speed 10x–100x faster training

🧠 Memory Large models with 40GB+


VRAM

34/40
Benefit Description

🏭 Production Real-time inference, big


batch training

🧠 Real-Life Analogy
Socho GPU ek express train hai jo 1000 passengers (calculations) ek saath le ja sakta hai
CPU ek auto rickshaw hai — reliable but slow.

📌 Final Practice Task for You:


python

# Create tensor on CPU


t = [Link]([[1, 2], [3, 4]], dtype=torch.float32)

# Move to GPU
t_gpu = [Link]('cuda')

# Do some operation
t_result = t_gpu * 2

# Move back to CPU


t_cpu = t_result.to('cpu')

# Convert to NumPy
print(t_cpu.numpy())

35/40
🔴 Part 1: Shape Mismatch Error
💡 Most common error in PyTorch matrix operations.
🔸 Problem:
python

A = [Link]([[1, 2], [3, 4], [5, 6]], dtype=torch.float32)


B = [Link]([[7, 8], [9, 10], [11, 12]], dtype=torch.float32)
C = [Link](A, B) # ❌ Runtime Error

❌ Error:
Because A is shape (3x2) and B is shape (3x2)
Matrix multiplication rule:

A (m x n) @ B (n x p) → Output: (m x p)
But here, inner dims don’t match ( 2 != 3 )

✅ Fix: Use Transpose


python

C = [Link](A, B.T) # Now B.T is (2x3)

A → (3×2)
B.T → (2×3)
✔️ Now matmul is valid
✔️ Output shape = (3×3)

🔁 Shortcut:
python

36/40
C = [Link](A, B.T)

[Link] is shorthand for matrix multiply (only for 2D tensors)

🔴 Part 2: Data Type Mismatch Error


🔸 Problem:
python

t1 = [Link](10, 100, 10, dtype=torch.float16) # Float16


t2 = [Link](10, 100, 10) # Default Float32
dot = t1 @ t2 # ❌ Error

❌ Error:
Both tensors have different data types:

t1 → float16
t2 → float32

✅ Fix: Type Cast


python

t2 = [Link](torch.float16) # Now same dtype


dot = t1 @ t2 # ✅ Works

You can use .type() to convert:

python

t = [Link](torch.float32)

Or shortcut:

python

t = [Link]()
t = [Link]() # Float16
t = t.int8()

37/40
🔴 Part 3: CPU vs GPU Mismatch Error
🔸 Problem:
python

t1 = [Link](10, 100, 10).to("cpu")


t2 = [Link](10, 100, 10).to("cuda")
dot = t1 @ t2 # ❌ Runtime error

❌ Error:
pgsql

Expected all tensors to be on the same device

✅ Fix: Move both to same device


python

t1 = [Link]("cuda") # Now both on GPU


dot = t1 @ t2 # ✅ Works

🧠 Tip: You can also use .cpu() and .cuda() :

python

t1 = [Link]()
t2 = [Link]()

🔁 Summary of Common PyTorch Errors


❌ Error Type 🧠 Reason ✅ Fix
Shape mismatch Wrong inner dimensions in Use .shape , transpose
matrix multiplication .T , .view()

38/40
❌ Error Type 🧠 Reason ✅ Fix
Data type mismatch Float16 vs Float32 or int8, etc. Use
.type(torch.float32
) or .float()

Device mismatch One tensor on CPU, one on Use .to("cuda") or


GPU .to("cpu")

🧩 Best Practices for Production PyTorch Code


✅ Make code device-agnostic:
python

device = [Link]("cuda" if [Link].is_available() else "cpu")


tensor = [Link](device)

✅ Monitor memory:
Use [Link].memory_allocated() to avoid OOM (Out Of Memory)

✅ Set random seed:


python

torch.manual_seed(42)
if [Link].is_available():
[Link].manual_seed(42)

✅ Use error handling:


Wrap critical operations with try–except blocks

39/40
40/40

You might also like