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

PyTorch Tensors

This note is the practical, hands-on counterpart to Tensors โ€” every way to actually create, inspect, and convert PyTorch tensors in real code.

Creating Tensors

import torch

torch.tensor([1, 2, 3])              # from a Python list
torch.zeros(3, 4)                       # a (3,4) tensor of all zeros
torch.ones(2, 2)                        # a (2,2) tensor of all ones
torch.randn(3, 3)                       # random values from a standard normal distribution
torch.arange(0, 10, 2)                 # [0, 2, 4, 6, 8]
torch.linspace(0, 1, 5)                # 5 evenly spaced values from 0 to 1

import numpy as np
torch.from_numpy(np.array([1, 2, 3]))   # convert a NumPy array to a tensor (shares memory!)

Inspecting a Tensor

x = torch.randn(3, 4)
print(x.shape)     # torch.Size([3, 4])
print(x.dtype)      # torch.float32 (the default)
print(x.device)     # cpu (or cuda:0 if moved to GPU)
print(x.ndim)       # 2 -- number of dimensions

The NumPy Conversion Gotcha

torch.from_numpy() shares the underlying memory with the original NumPy array โ€” modifying one modifies the other. Use .clone() if you need an independent copy:

arr = np.array([1.0, 2.0, 3.0])
t = torch.from_numpy(arr)
t[0] = 99.0
print(arr)   # [99. 2. 3.] -- the NumPy array changed too! They SHARE memory

t_independent = torch.from_numpy(arr).clone()   # a genuine independent copy

Common Mistakes

  • Assuming torch.from_numpy() creates an independent copy โ€” it shares memory by default; unexpected mutations in one can silently affect the other.
  • Creating tensors with an unintended dtype (e.g. integer division producing an integer tensor when a float was expected) โ€” always check .dtype when results look unexpectedly wrong.

Interview Relevance

Q: "What's a subtle gotcha with converting a NumPy array to a PyTorch tensor via torch.from_numpy()?" The resulting tensor shares the same underlying memory as the original NumPy array โ€” modifying one modifies the other, since no data is actually copied. Using .clone() (or the newer torch.tensor() constructor, which does copy) is necessary when an independent copy is genuinely needed.

Practice Question

Create a tensor of shape (2, 3) filled with the value 7, without listing every element manually.

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

PyTorch Tensors โ€“ FAQs

Quick answers about learning PyTorch Tensors in Deep Learning.

This free note from CodingNow 2.0 explains PyTorch 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 PyTorch Tensors, is 100% free with no signup required.
With focused practice, most students grasp PyTorch 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