Posts

Showing posts with the label Common PyTorch Commands and Cheatsheet

Appendix B: Common PyTorch Commands and Cheatsheet

Image
Below is the complete Appendix B: Common PyTorch Commands and Cheatsheet , structured clearly and comprehensively for the book. Appendix B: Common PyTorch Commands and Cheatsheet This appendix summarizes essential PyTorch commands used for tensors, neural networks, autograd, data loading, optimization, GPU usage, and model management. It serves as a quick reference for learners, practitioners, and researchers. 1. Tensors: Creation, Inspection, and Operations 1.1 Creating Tensors import torch # Basic creation x = torch.tensor([1, 2, 3]) x_float = torch.tensor([1.0, 2.0], dtype=torch.float32) # From Python lists a = torch.tensor([[1, 2], [3, 4]]) # Random tensors torch.rand(3, 3) # Uniform random torch.randn(3, 3) # Normal random torch.randint(0, 10, (3,)) # Random integers # Zeros, ones, identity torch.zeros(2, 3) torch.ones(3, 3) torch.eye(4) # Identity matrix # Range torch.arange(0, 10, 2) torch.linspace(0, 1, 5) 1.2 Tensor Pro...