Part 1 - What is PyTorch?
PyTorch (pytorch.org) ek open-source, Python-based deep learning library hai. Research community mein yeh behad popular hai.
Papers With Code ke mutabik, 2019 se PyTorch research ke liye sabse zyada istemal hone wali library ban gayi hai. Kaggle ke 2022 survey mein bhi lagbhag 40% data scientists ne PyTorch ka istemal kiya, aur yeh number har saal badh raha hai.
Iski popularity ka raaz iska user-friendly interface aur efficiency hai. Yeh aasan hone ke saath-saath advanced users ko customization ke liye poori flexibility bhi deta hai.
Aage aane wale subsections mein, hum PyTorch ke main features ko define karenge.
1.1 PyTorch ke teen core components
PyTorch ko samajhne ka sabse achha tareeka hai iske teen core components par focus karna:
- Tensor Library (GPU ke saath): Yeh NumPy jaisi ek array-oriented library hai, lekin isme GPU par calculations ko fast karne ki extra power hai. Isse aap aasani se CPU aur GPU ke beech switch kar sakte hain.
- Automatic Differentiation Engine: Is feature ko 'autograd' bhi kehte hain. Yeh tensor operations ke liye gradients ko automatically calculate karta hai, jisse model training (backpropagation) bahut aasan ho jaati hai.
- Deep Learning Library: PyTorch aapko deep learning models design aur train karne ke liye zaroori building blocks (jaise pre-trained models, loss functions, optimizers) provide karta hai.
Deep learning term ko define karne aur PyTorch ko install karne ke baad, is tutorial ka baki hissa PyTorch ke in teen core components ko aur detail mein cover karega, hands-on code examples ke saath.
1.2 Defining deep learning
Aapne AI, Machine Learning, aur Deep Learning jaise terms sune honge. Chaliye inke beech ke farak ko samajhte hain.
- Artificial Intelligence (AI): Yeh ek broad field hai jiska maqsad aisi machines banana hai jo insaani intelligence wale kaam kar sakein, jaise sochna, samajhna aur faisle lena.
- Machine Learning (ML): Yeh AI ka ek subfield hai. Isme algorithms data se seekhte hain aur predictions karte hain, bina explicitly program kiye. Recommendation systems aur spam filters iske examples hain.
- Deep Learning (DL): Yeh ML ki ek special branch hai jo 'deep neural networks' ka istemal karti hai. Ye networks insaani dimaag ke neurons se inspired hain. "Deep" ka matlab hai in networks mein multiple layers ka hona, jo unhe images, audio, aur text jaise complex data ko samajhne mein madad karta hai. PyTorch ek deep learning library hai.
1.3 Installing PyTorch
PyTorch ko kisi bhi other Python library ya package ki tarah install kiya ja sakta hai. Lekin, kyunki PyTorch ek comprehensive library hai jismein CPU- aur GPU-compatible codes hain, installation ke liye additional explanation ki zaroorat pad sakti hai.
Python Version: Hamesha Python ka ek stable version (latest se ek ya do version purana) istemal karein. Jaise, agar Python 3.13 latest hai, to 3.11 ya 3.12 use karna behtar hai.
Agar aapke paas NVIDIA GPU hai, to GPU-enabled version install karna best hai. Installation ke liye yeh simple command hai:
pip install torch
Yeh command automatically detect kar lega ki aapke system mein GPU hai ya nahi aur uske hisaab se sahi version install kar dega.
PyTorch ka version check karne ke liye, aap PyTorch mein following code execute kar sakte hain:
import torch
print(torch.__version__)
Output kuch aisa dikhega:
2.4.1
PyTorch install karne ke baad, aap check kar sakte hain ki aapka installation aapke built-in NVIDIA GPU ko recognize karta hai ya nahi, Python mein following code run karke:
import torch
print(torch.cuda.is_available())
Agar yeh `True` return karta hai, to aapka GPU setup taiyar hai. Agar `False` aata hai, to chinta na karein, aap CPU par bhi training kar sakte hain, bas woh thoda slow hoga.
Agar aapke paas local GPU nahi hai, to aap Google Colab jaise cloud services ka istemal kar sakte hain, jo free mein limited time ke liye GPU access dete hain.