Coding Now – Best AI & Full Stack Courses in Delhi NCR | 100% Placement
Limited Offer: Get 50% OFF on AI & Full Stack Courses
📞 Call Now: +918448811320
Back to Deep Learning Notes
Topic #535

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.

Related DL Notes

Want to go beyond the notes?

Join Coding Hubs School of AI's Deep Learning course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available
💬 Talk to Advisor
1
WhatsApp

Latest from Our Blog

Insights on AI, Data Science, Full Stack & Career

View All Articles →