Tensor
Introduction¶
- Tensors are the basic building block of all the machine learning and deep learning.
- Their job is to represent data in a numerical way
- For example, you could represent an #image as a tensor with shape
[3, 224, 224]which would mean[colour_channels, height, width], as in the image has3colour channels (red, green, blue), a height of224pixels and a width of224pixels.
Language¶
tensor-speak
Creating tensors¶
First a scalar¶
- A single number
- A scalar is a single number, and in tensor-speak it's a zero dimension tensor. (Ref: 02_Math)
- Usually used as variable
a
Initialize a scalar¶
tensor(7)- That means, although
scalaris a single number, it's of typetorch.Tensor.
Checking dimensions of a tensor¶
0from torch.Tensor to a Python integer¶
7
Second a vector¶
- A number with direction (e.g. wind speed with direction) but can also have many other numbers
- A vector is a single dimension tensor but can contain many numbers. (Ref: 02_Math)
- As in, you could have a vector
[3, 2]to describe[bedrooms, bathrooms]in your house. Or you could have[3, 2, 2]to describe[bedrooms, bathrooms, car_parks]in your house. - The important trend here is that a vector is flexible in what it can represent (the same with tensors).
- Usually used as variable
y
Initialize a vector¶
tensor([7, 7])Checking dimensions of a tensor¶
1Number of dimensions a tensor in PyTorch = Number of square brackets on the outside ([) and you only need to count one side.
Checking shape¶
- The shape tells you how the elements inside them are arranged. torch.Size([2])
Third the Matrix¶
- A 2-dimensional array of numbers
- Usually used as variable
Q
Initialize a matrix¶
tensor([[ 7, 8], [ 9, 10]])Dimension & Shape¶
print("Dimentions: ", MATRIX.ndim) # two dimensions
print("Shape: ", MATRIX.shape) #two elements deep and two elements wide
Lets Tensor now¶
- An n-dimensional array of numbers
- Usually used as variable
X
# Initialize a tensor
TENSOR = torch.tensor([[[1, 2, 3], [3, 6, 9],[2, 4, 5]]])
print(TENSOR)
# Check number of dimensions for TENSOR
print("Dimentions: ", TENSOR.ndim)
# Check number of size for TENSOR
print("Shape: ", TENSOR.shape) # The dimensions go outer to inner.
- when building machine learning models with PyTorch, it's rare you'll create tensors by hand (like what we've being doing).
- Instead, a machine learning model often starts out with large random tensors of numbers and adjusts these random numbers as it works through data to better represent it.
-
In essence:
Start with random numbers -> look at data -> update random numbers -> look at data -> update random numbers... -
As a data scientist, you can define how the machine learning model starts (initialization), looks at data (representation) and updates (optimization) its random numbers.
Create a tensor of random numbers w/ torch.rand()¶
# Create a random tensor of size (3, 4)
random_tensor = torch.rand(size=(3, 4))
random_tensor, random_tensor.dtype
- The flexibility of
torch.rand()is that we can adjust thesizeto be whatever we want. - For example, say you wanted a random tensor in the common image shape of
[224, 224, 3]([height, width, color_channels]).