PyTorch Basics
PyTorch fundamentals and tensor operations.
Installation & Import
Install PyTorch
pip install torch torchvision
Import
import torch
import torch.nn as nn
print(torch.__version__)
Check CUDA availability
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
Tensors
Create tensor
x = torch.tensor([1, 2, 3])
Create tensor with shape
matrix = torch.tensor([[1, 2], [3, 4]])
Zeros and ones
zeros = torch.zeros(3, 3)
ones = torch.ones(2, 2)
Random values
random = torch.randn(3, 3)
Tensor to GPU
x = x.to(device)
Tensor Operations
Arithmetic operations
a = torch.tensor([1, 2, 3])
b = torch.tensor([4, 5, 6])
add = a + b
sub = a - b
mul = a * b
div = a / b
Matrix multiplication
matmul = torch.matmul(matrix1, matrix2)
matmul = matrix1 @ matrix2
Reshape tensor
reshaped = x.view(2, 2)
reshaped = x.reshape(2, 2)
Autograd
Enable gradient tracking
x = torch.tensor(2.0, requires_grad=True)
y = x ** 2 + 3 * x
Compute gradients
y.backward()
print(x.grad)
Disable gradient
with torch.no_grad():
y = x * 2
Zero gradients
x.grad.zero_()