Understanding Tensors

Loading concept...

🧱 Understanding Tensors: The LEGO Blocks of Deep Learning


The Big Picture

Imagine you have a magical toy box filled with LEGO blocks. These blocks can be arranged in different ways—a single block, a row, a flat mat, or even a 3D cube.

Tensors are exactly like these LEGO blocks for computers!

They hold numbers in organized ways, and PyTorch uses them to build amazing things like image recognizers, chatbots, and self-driving cars.


🎯 What is a Tensor?

A tensor is just a container for numbers. Think of it like different ways to organize your toys:

Toy Organization Tensor Name Example
🔵 One marble Scalar (0D) 5
🔵🔵🔵 Marbles in a row Vector (1D) [1, 2, 3]
Grid of marbles Matrix (2D) A photo
Stack of grids 3D Tensor A video
graph TD A[🔵 Scalar: One Number] --> B[🔵🔵🔵 Vector: A Row] B --> C[📊 Matrix: A Grid] C --> D[📦 3D Tensor: Stacked Grids]

Simple Truth: A tensor is just numbers organized in boxes within boxes!


🛠️ Creating Tensors from Data

Let’s build some tensors! It’s as easy as putting toys in a box.

From a Single Number (Scalar)

import torch

# One lonely number
x = torch.tensor(42)
print(x)  # tensor(42)

Like holding one marble in your hand.

From a List (Vector)

# A row of numbers
ages = torch.tensor([5, 8, 12])
print(ages)  # tensor([5, 8, 12])

Like placing marbles in a line.

From Nested Lists (Matrix)

# A grid of numbers
grid = torch.tensor([
    [1, 2, 3],
    [4, 5, 6]
])
print(grid)

Output:

tensor([[1, 2, 3],
        [4, 5, 6]])

Like arranging marbles in a checkerboard pattern.

Quick Tensor Builders

PyTorch gives you shortcuts to make tensors fast:

# All zeros - like empty boxes
zeros = torch.zeros(3, 4)

# All ones - like boxes full of 1s
ones = torch.ones(2, 3)

# Random numbers - surprise boxes!
random = torch.rand(2, 2)

🎨 Tensor Data Types

Just like crayons come in different colors, numbers come in different types:

Type What It Holds When to Use
float32 Decimals (3.14) Most AI work
int64 Whole numbers (42) Counting things
bool True/False Yes/No decisions

Setting the Type

# Decimal numbers (default)
prices = torch.tensor(
    [1.99, 2.50, 3.75]
)
print(prices.dtype)
# torch.float32

# Whole numbers only
counts = torch.tensor(
    [1, 2, 3],
    dtype=torch.int64
)
print(counts.dtype)
# torch.int64

Changing Types

# Convert decimals to whole numbers
x = torch.tensor([1.9, 2.7, 3.1])
y = x.to(torch.int64)
print(y)  # tensor([1, 2, 3])

The decimals got chopped off—like rounding down!


📐 Tensor Shapes and Dimensions

Shape tells you how your LEGO blocks are arranged.

What is Shape?

# A row of 4 numbers
row = torch.tensor([1, 2, 3, 4])
print(row.shape)  # torch.Size([4])

# A 2x3 grid
grid = torch.tensor([
    [1, 2, 3],
    [4, 5, 6]
])
print(grid.shape)  # torch.Size([2, 3])

Reading shapes:

  • [4] → 4 items in a row
  • [2, 3] → 2 rows, 3 columns

Dimensions (How Deep is the Box?)

# Scalar = 0 dimensions
scalar = torch.tensor(5)
print(scalar.ndim)  # 0

# Vector = 1 dimension
vector = torch.tensor([1, 2, 3])
print(vector.ndim)  # 1

# Matrix = 2 dimensions
matrix = torch.tensor([[1, 2], [3, 4]])
print(matrix.ndim)  # 2
graph TD S[0D: Scalar] -->|Add dimension| V[1D: Vector] V -->|Add dimension| M[2D: Matrix] M -->|Add dimension| T[3D: Tensor]

Real-World Shapes

Data Type Shape Example
Single pixel [3] RGB: [255, 0, 128]
Grayscale image [28, 28] 28x28 pixels
Color image [3, 224, 224] 3 colors, 224x224
Batch of images [32, 3, 224, 224] 32 images at once

Checking Size

img = torch.rand(3, 224, 224)

print(img.shape)     # torch.Size([3, 224, 224])
print(img.size())    # torch.Size([3, 224, 224])
print(img.ndim)      # 3
print(img.numel())   # 150528 (total numbers)

🎉 Quick Summary

Concept One-Line Explanation
Tensor A box that holds numbers
Creating torch.tensor([1, 2, 3])
Data Type What kind of numbers (decimals vs whole)
Shape How the numbers are arranged
Dimensions How many “layers” deep

🚀 You’re Ready!

You just learned the foundation of all deep learning!

Every neural network, every AI model—they all start with tensors. You now understand:

✅ What tensors are (organized number containers) ✅ How to create them (from lists, zeros, ones, random) ✅ Data types (float, int, bool) ✅ Shapes and dimensions (the arrangement of numbers)

Next step: Start playing with tensors in PyTorch. Try creating your own!

import torch

# Your turn! Make a tensor
my_tensor = torch.tensor([
    [1, 2],
    [3, 4]
])
print("Shape:", my_tensor.shape)
print("Type:", my_tensor.dtype)

You’ve got this! 🎮

Loading story...

No Story Available

This concept doesn't have a story yet.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

Interactive Preview

Interactive - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Interactive Content

This concept doesn't have interactive content yet.

Cheatsheet Preview

Cheatsheet - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Cheatsheet Available

This concept doesn't have a cheatsheet yet.

Quiz Preview

Quiz - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Quiz Available

This concept doesn't have a quiz yet.