Tensors
What is a tensor? A tensor is a generalization of vectors and matrices and is easily understood as a multidimensional array. A tensor can be a number, a vector, a matrix, or any n-dimensional array.
Examples
- 0-d tensor: scalar
- 1-d tensor: vector
- 2-d tensor: matrix
- 3-d tensor: cube
- n-d tensor: n-d array
Rank
The rank of a tensor is the number of dimensions present within the tensor. For example, the tensor [1, 2, 3] is a rank 1 tensor, while the tensor [[1, 2, 3], [4, 5, 6], [7, 8, 9]] is a rank 2 tensor.
Shape
The shape of a tensor is the number of elements in each dimension. For example, the tensor [[1, 2, 3], [4, 5, 6], [7, 8, 9]] has a shape of (3, 3).
PyTorch tensors
PyTorch tensors are similar to NumPy arrays, but can also be used on a GPU to accelerate computing.
Creating tensors
Directly from data
|
|
gives
|
|
Random
|
|
gives
|
|
Operations
|
|
gives
|
|