Tensor
- Machine Learning course
- Advanced
- Lesson 8/78
- 5 min
A tensor is PyTorch's core data structure, a multi-dimensional array.
The tensor
The tensor is PyTorch's core data structure: an n-dimensional array of numbers of the same type. It is named by its number of dimensions: scalar (0), vector (1), matrix (2), beyond that a tensor. A batch of images is typically 4-dimensional: (example, channel, height, width).
s = torch.tensor(3.0) # 0 dimensions
v = torch.tensor([1.0, 2.0]) # 1 dimension
m = torch.tensor([[1, 2], [3, 4]]) # 2 dimensions
print(s.dim(), v.dim(), m.dim()) # 0 1 2Element-wise operations, broadcasting and indexing work as in NumPy.
The bridge to NumPy
torch.from_numpy and .numpy() move data between the two worlds without copying; because they share the same memory, a change in one shows up in the other:
a = np.array([1, 2, 3])
t = torch.from_numpy(a)
a[0] = 100
print(t.tolist()) # [100, 2, 3] shared memory
k = torch.tensor(a) # a copy: independentIf an independent copy is wanted, torch.tensor(a) or t.clone() is used.
Practice Shared memory
From the array a, create t1 with torch.from_numpy and t2 with torch.tensor. Then set a[1] = 50; print t1.tolist() and t2.tolist() on separate lines.
Starter code
import numpy as np
import torch
a = np.array([1, 2, 3])
# buraya yazSample solution
import numpy as np
import torch
a = np.array([1, 2, 3])
t1 = torch.from_numpy(a)
t2 = torch.tensor(a)
a[1] = 50
print(t1.tolist())
print(t2.tolist())Expected output
[1, 50, 3] [1, 2, 3]
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 How many dimensions does a batch of 32 colour (3-channel) 64×64 images have as a tensor?
- 2
- 5
- 4 Correct answer
- 3
Explanation: The shape is (32, 3, 64, 64): example, channel, height, width.
2 Turn the NumPy array into a tensor without copying.
t = torch.____(a)
t = torch.from_numpy(a)
Explanation: from_numpy shares memory; torch.tensor copies.
3 Match the dimensions.
| Term | Meaning |
|---|---|
scalar |
0 dimensions |
vector |
1 dimension |
matrix |
2 dimensions |
image batch |
4 dimensions |