Tensor properties
- Machine Learning course
- Advanced
- Lesson 10/78
- 5 min
A tensor has shape, dtype, and device properties.
shape, dtype, device
Every tensor has three basic properties:
x = torch.zeros(2, 3)
print(x.shape) # torch.Size([2, 3])
print(x.dtype) # torch.float32
print(x.device) # cpu
print(x.numel()) # 6 elementsIn deep learning the default type is float32; a tensor created from a list of integers is int64. Class labels should be int64 (torch.long), inputs float32.
Type conversion
To change the type, .to(dtype) or its shortcuts (.float(), .long()) are used. Operating on different types sometimes gives an error, sometimes a silent type promotion:
a = torch.tensor([1, 2, 3])
b = a.float() # float32
c = a.to(torch.float64)
print((a / 2).dtype) # torch.float32: division produces decimals
print((a // 2).tolist()) # [0, 1, 1]Decimal arrays coming from NumPy are float64; before giving them to a model they must be converted to float32 with .float().
Practice Types
For a = torch.tensor([1, 2, 3]), print a.dtype, (a / 2).dtype and a.float().dtype on separate lines. Then print the shape and numel() of torch.zeros(3, 4, 5) on one line.
Starter code
import torch
# buraya yazSample solution
import torch
a = torch.tensor([1, 2, 3])
print(a.dtype)
print((a / 2).dtype)
print(a.float().dtype)
z = torch.zeros(3, 4, 5)
print(z.shape, z.numel())Expected output
torch.int64 torch.float32 torch.float32 torch.Size([3, 4, 5]) 60
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What is <code>torch.tensor([1, 2, 3]).dtype</code>?
- torch.int64 Correct answer
- torch.float32
- torch.int32
- torch.float64
Explanation: Integer lists produce an int64 tensor.
2 Convert the tensor to float32.
x = a.____()
x = a.float()
Explanation: The .float() shortcut produces float32.
3 Match the properties.
| Term | Meaning |
|---|---|
shape |
Dimensions |
dtype |
Element type |
device |
cpu or cuda |
numel() |
Element count |