Welcome to the PyTorch Learning Repository! This repository serves as a comprehensive guide to PyTorch concepts, operations, and techniques, helping you master this powerful deep learning framework.
PyTorch is an open-source machine learning framework that provides dynamic computation graphs, making it intuitive and flexible for building and training deep learning models. Its seamless integration with Python makes it a favorite among researchers and developers.
- Getting Started
- Tensor Basics
- Tensor Manipulations
- Matrix Operations
- Tensor Indexing
- Broadcasting and Expanding
- Tensor Reduction
- Linear Algebra
- Comparison Operations
- Additional Resources
-
Install PyTorch: Follow the official installation guide.
# Example for Linux with CUDA pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118 -
Import PyTorch:
import torch
-
Creating Tensors:
x = torch.tensor([1, 2, 3]) # 1D tensor y = torch.zeros(3, 3) # 2D tensor z = torch.rand(3, 4) # Random tensor
-
Inspecting Tensors:
print(x.shape) # Tensor shape print(x.dtype) # Data type print(x.ndimension()) # Number of dimensions
-
Reshape: Change tensor shape.
x = torch.arange(12).reshape(3, 4) # (3, 4)
-
Squeeze/Unsqueeze: Remove or add dimensions of size 1.
x = x.unsqueeze(0) # Add a dimension x = x.squeeze() # Remove dimensions of size 1
-
View: Similar to
reshapebut requires contiguous memory.x = x.view(3, 4)
-
Matrix Multiplication:
torch.mm(A, B) # 2D matrix multiplication torch.matmul(A, B) # Generalized matrix multiplication
-
Batch Matrix Multiplication:
torch.bmm(A, B) # For 3D tensors
-
Basic Indexing:
x = torch.tensor([[1, 2], [3, 4]]) print(x[0, 1]) # Access element
-
Advanced Indexing:
indices = torch.tensor([0, 1]) x_selected = torch.index_select(x, 0, indices)
-
Expand: Expands dimensions without copying data.
x = torch.tensor([1, 2, 3]) x_expanded = x.unsqueeze(0).expand(2, 3)
-
Repeat: Repeats data along dimensions.
x_repeated = x.repeat(2, 1)
-
Sum:
torch.sum(x, dim=0)
-
Mean:
torch.mean(x.float(), dim=1)
-
Product:
torch.prod(x, dim=1)
-
Matrix Inverse:
torch.inverse(A)
-
SVD:
torch.svd(A)
-
Eigenvalues:
torch.eig(A, eigenvectors=True)
-
Element-wise Comparison:
torch.eq(A, B) # Equal torch.gt(A, 2) # Greater than
-
Conditional Selection:
torch.where(A > 2, A, B)
Feel free to submit pull requests, report issues, or suggest improvements to this repository. Let’s make learning PyTorch easier for everyone!
This repository is licensed under the MIT License. Feel free to use and modify it for educational purposes.