Part 1: Introduction to PyTorch and Tensors

Part of the PyTorch 101 Series

Why I Switched to PyTorch

I was building an image recognition system using NumPy. The gradient calculation code alone was 200+ lines. Every time I changed the model architecture, I had to rewrite the backward pass.

Bugs were everywhere. A minus sign in the wrong place could ruin days of training.

Then I rewrote it in PyTorch: 15 lines. Gradients calculated automatically. No bugs in backpropagation.

That's when I understood: PyTorch isn't just a library, it's freedom from gradient hell.

Let me show you what makes PyTorch special.

What is PyTorch?

PyTorch is a tensor computation library with automatic differentiation, optimized for deep learning.

Key features:

  • Tensors: N-dimensional arrays on CPU or GPU

  • Autograd: Automatic differentiation (calculates gradients for you)

  • Neural Networks: Pre-built layers and models (torch.nn)

  • Dynamic graphs: Change architecture during runtime

  • Production-ready: TorchScript, ONNX, mobile deployment

Think of it as: NumPy with GPU acceleration + automatic gradients + neural network building blocks.

Installation

Check installation:

My setup:

Tensors: The Foundation

Tensors are multi-dimensional arrays - the foundation of PyTorch.

Creating Tensors

Output:

Tensor Attributes

Output:

Data Types

I use float32 for most tasks - good balance of precision and performance.

Tensor Operations

Basic Math

Output:

Matrix Operations

Reduction Operations

Indexing and Slicing

Output:

GPU Acceleration

This is where PyTorch shines - seamless GPU computation.

Moving Tensors to GPU

Best practice:

Performance Comparison

My results (5000x5000 matrix multiplication):

GPU is 100x faster for large matrix operations!

From NumPy to PyTorch

When I migrated my image processing pipeline from NumPy to PyTorch, performance improved dramatically.

NumPy vs PyTorch

Real Migration Example

My image preprocessing pipeline before and after:

Before (NumPy):

After (PyTorch):

Performance gain: 15x faster with GPU!

Common Patterns

Patterns I use in every PyTorch project:

1. Device-Agnostic Code

2. Seed for Reproducibility

3. Memory Management

4. Inference Mode

Quick Reference

Create tensors:

Operations:

GPU:

Best Practices

From my experience:

1. Use GPU when available - 10-100x speedup for large operations.

2. Keep tensors on same device - mixing CPU and GPU tensors errors.

3. Use in-place operations sparingly - they can interfere with autograd.

4. Batch operations - process multiple samples together for efficiency.

5. Use appropriate dtype - float32 for most tasks, float16 for memory savings.

6. Set seeds for reproducibility - crucial for debugging.

7. Profile your code - find bottlenecks before optimizing.

What's Next?

You now understand PyTorch tensors - the foundation of deep learning. In Part 2, we'll explore autograd: automatic differentiation that calculates gradients for you.

Next: Part 2 - Autograd and Automatic Differentiation


Previous: Series Overview

This article is part of the PyTorch 101 series. All examples use Python 3 and are based on real projects.

Last updated