Rank is the number of axes in a tensor. A scalar has rank 0, a vector has rank 1, and a matrix has rank 2. section 2.2.1 section 2.2.5
A scalar in NumPy
Create x as 12 and inspect x$ndim: the result is 0, confirming that a scalar has no axes. section 2.2.1
Do not confuse a vector’s length with tensor rank. A five-entry vector has one axis and rank 1; a rank-5 tensor has five axes, with potentially different sizes. section 2.2.2
Inspect rank alongside the tensor’s shape and data type:
- Use length(dim(x)) for R arrays, or x$ndim and op_ndim(x) for backend tensors. section 2.2.5
- A rank-3 MNIST tensor has shape (60000, 28, 28): 60,000 grayscale images, each represented by a 28 × 28 matrix. section 2.2.5
x <- np_array(x) x array([12., 3., 6., 14., 7.]) x$shape (5,) x$ndim 1
Inspect a five-entry vector’s shape and ndim to see that its length is 5 while its rank is 1. section 2.2.2
