Your First torch.tensor
Create a tensor and print its shape and dtype.
Your First torch.tensor is a free Deep Learning Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Deep Learning Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Meet the Tensor
A tensor is PyTorch's core data container, a grid of numbers much like a NumPy array but ready to run on a GPU. 🧮
Create One from a List
The simplest way to make a tensor is torch.tensor from a Python list. PyTorch copies your numbers into a fast numerical grid.
x = torch.tensor([1, 2, 3])Print It Out
Printing a tensor shows its values wrapped in tensor(...), so you always know you are looking at PyTorch data, not a plain list.
print(x)Shape Describes Structure
The shape tells you the size along each dimension. A flat list of three values has a shape of three.
print(x.shape)Build a 2D Tensor
Nest lists to make a grid. This matrix has two rows and three columns, so its shape reads as two by three.
m = torch.tensor([[1, 2, 3], [4, 5, 6]])Dtype Is the Number Type
Every tensor has a dtype, the kind of number it stores. Whole numbers become int64, decimals become float32 by default.
print(x.dtype)Floats for Training
Networks learn with decimals, so most model data is float32. Add a decimal point and PyTorch picks the float type for you.
y = torch.tensor([1.0, 2.0, 3.0])Set the Dtype Yourself
You can request a type directly with the dtype argument, which is handy when you need floats from integer input.
z = torch.tensor([1, 2], dtype=torch.float32)Tensors Full of Zeros
Need a blank tensor of a given size? torch.zeros fills the shape you ask for with zeros, perfect as a starting buffer.
torch.zeros(2, 3)Random Tensors
Model weights often start random. torch.rand gives a tensor of the shape you choose, filled with values between zero and one.
torch.rand(2, 3)Count the Dimensions
The number of dimensions is the tensor's rank. Read it with .ndim: a vector is rank one, a matrix is rank two.
print(m.ndim)Quick Check
Reason about the shape of a nested-list tensor.
Recap
You created tensors from lists, read their shape and dtype, and made zeros and random grids. This is the data deep learning runs on. ✅
Frequently asked questions
Is the “Your First torch.tensor” lesson free?
Yes — the full text of “Your First torch.tensor” is free to read here on the web, and the Deep Learning Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Deep Learning Academy course, upgrade to CoddyKit PRO.
What will I learn in “Your First torch.tensor”?
Create a tensor and print its shape and dtype. You practise Deep Learning Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Deep Learning Academy?
No prior experience is required. Deep Learning Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Your First torch.tensor” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Deep Learning Academy lesson?
Yes. Every Deep Learning Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.