Tensor Creaiton and Initialization
Basic Tensor Operations
import torch
# Create tensor from list
data_tensor = torch.tensor([1, 2, 3], dtype=torch.float32)
# Create tensor with random values (uniform distribution)
rand_tensor = torch.rand(2, 3)
# Create tensor with normal distribution values
normal_tensor = torch.randn(3, 4)
# Create tensor with random integers
int_tensor = torch.randint(0, 10, (5,))
Device Management
# Move tensor to GPU if available
device = 'cuda' if torch.cuda.is_available() else 'cpu'
gpu_tensor = data_tensor.to(device)
Mathematical Operations
Matrix Multiplication
# Batch matrix multiplication
batch1 = torch.randn(10, 3, 4)
batch2 = torch.randn(10, 4, 5)
result = torch.bmm(batch1, batch2)
Element-wise Operations
# Basic arithmetic
a = torch.tensor([1.0, 2.0, 3.0])
b = torch.tensor([4.0, 5.0, 6.0])
sum_tensor = a + b
prod_tensor = a * b
# Activation functions
relu_output = torch.relu(a)
sigmoid_output = torch.sigmoid(b)
Neural Network Components
Layer Definitions
import torch.nn as nn
# Linear layer
linear_layer = nn.Linear(in_features=64, out_features=128)
# Convolutional layer
conv_layer = nn.Conv2d(in_channels=3, out_channels=32, kernel_size=3)
# Recurrent layer
lstm_layer = nn.LSTM(input_size=100, hidden_size=50, num_layers=2)
Loss Fucntions
# Mean Squared Error
mse_loss = nn.MSELoss()
# Cross Entropy Loss
ce_loss = nn.CrossEntropyLoss()
# Binary Cross Entropy
bce_loss = nn.BCELoss()
Model Training Utilities
Optimization
from torch.optim import Adam
model = MyNeuralNetwork()
optimizer = Adam(model.parameters(), lr=0.001)
# Training loop
for epoch in range(epochs):
optimizer.zero_grad()
outputs = model(inputs)
loss = loss_fn(outputs, targets)
loss.backward()
optimizer.step()
Gradient Management
# Disable gradient calculation
with torch.no_grad():
predictions = model(test_inputs)
# Detach tensor from computation graph
intermediate = hidden_states.detach()
Tenser Manipulation
Reshaping Operations
# Change tensor shape
original = torch.randn(4, 5)
reshaped = original.view(2, 10)
# Add/remove dimensions
unsqueezed = original.unsqueeze(0) # adds batch dimension
squeezed = unsqueezed.squeeze(0) # removes batch dimension
Concatenation and Stacking
# Concatenate along existing dimension
concat_result = torch.cat([tensor1, tensor2], dim=0)
# Stack along new dimension
stack_result = torch.stack([tensor1, tensor2], dim=1)
Model Persistence
Saving and Loading
# Save entire model
torch.save(model, 'model.pth')
# Save only parameters
torch.save(model.state_dict(), 'params.pth')
# Load model
loaded_model = torch.load('model.pth')
# Load parameters into existing model
model.load_state_dict(torch.load('params.pth'))
Utility Functions
Memory Management
# Clear GPU cache
torch.cuda.empty_cache()
# Check memory usage
print(torch.cuda.memory_allocated())
print(torch.cuda.max_memory_allocated())
Random Seed Control
# Set random seed for reproducibility
torch.manual_seed(42)
if torch.cuda.is_available():
torch.cuda.manual_seed_all(42)