๐Ÿ”ฅLimited Offer: Get 50% OFFon AI & Full Stack Courses๐Ÿ”ฅ
Back to Deep Learning Notes
Topic #13

Tensors

A tensor is simply the generalization of scalars, vectors and matrices to any number of dimensions. In deep learning frameworks, "tensor" is the universal container for data โ€” every input, weight and activation is a tensor of some rank.

Tensor Rank (Number of Dimensions)

RankNameShape ExampleDL Example
0Scalar()A single loss value
1Vector(n,)A feature vector, a bias vector
2Matrix(m, n)A weight matrix, a batch of 1-D signals
33-D Tensor(C, H, W)A single image: channels × height × width
44-D Tensor(N, C, H, W)A batch of \(N\) images
55-D Tensor(N, T, C, H, W)A batch of video clips: batch × time × channels × height × width

Visualizing an Image as a 3-D Tensor

3 channels (R, G, B) × Height × Width shape = (3, 224, 224)

A single RGB image is a rank-3 tensor โ€” one 2-D grid of pixel intensities per color channel, stacked together.

Code โ€” Building Tensors of Different Ranks

import torch

image = torch.randn(3, 224, 224)          # one RGB image: (C, H, W)
batch_of_images = torch.randn(32, 3, 224, 224)  # a batch of 32 images: (N, C, H, W)

print(image.ndim, image.shape)             # 3 torch.Size([3, 224, 224])
print(batch_of_images.ndim, batch_of_images.shape)  # 4 torch.Size([32, 3, 224, 224])
print(image.dtype, image.device)           # torch.float32 cpu

Key Tensor Attributes You'll Use Constantly

AttributeMeaning
.shapeSize along each dimension
.ndimNumber of dimensions (the tensor's rank)
.dtypeData type of the elements (e.g. float32, int64)
.deviceWhere the tensor lives โ€” CPU or GPU (cuda)

Common Mistakes

  • Thinking "tensor" implies something mathematically exotic โ€” in deep learning frameworks it's simply an n-dimensional array; the deeper mathematical-physics notion of a tensor (with transformation rules under change of basis) isn't what PyTorch/TensorFlow code is invoking.
  • Mixing up the (N, C, H, W) channel-first convention (PyTorch default) with the (N, H, W, C) channel-last convention (common in TensorFlow) โ€” feeding a tensor in the wrong layout is a very common, very silent bug.

Interview Relevance

Q: "What's the shape of a batch of 64 grayscale 28ร—28 images?" \((64, 1, 28, 28)\) in PyTorch's channel-first convention โ€” batch size, then 1 channel (grayscale), then height, then width.

Practice Question

What is the shape and rank of a batch of 16 audio clips, each represented as a sequence of 8,000 samples with 1 channel? Write it in (N, C, L) form.

Want to go beyond the notes?

Join CodingNow 2.0's Deep Learning course โ€” live mentorship, real projects, and 100% placement support.

Enroll Now โ€” Free Demo Available

Tensors โ€“ FAQs

Quick answers about learning Tensors in Deep Learning.

This free note from CodingNow 2.0 explains Tensors in Deep Learning โ€” concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Deep Learning topic on CodingNow 2.0, including Tensors, is 100% free with no signup required.
With focused practice, most students grasp Tensors in 1โ€“3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) โ€” expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now