[PYTHON] Understanding the Tensor (2): Shape

1.First of all

Organize the information you need to understand Tensor as your own notes. At the same time, I will introduce the Tensor notation in Python.

Continuing from the previous article, this time I would like to introduce Tensor's Shape. Understanding the tensor (1): Dimension

2. What is a tensor?

  1. Tensor is a just a container for data.
  2. The data are almost numerical data
  3. Therefore, Tensor is a container for number.

A tensor is a container that holds numbers. It's simple.

3. Tensor keywords

It means Tensor, Dimension, Axis, Ranks.

  1. Tensor is a container of numbers.
  2. Tensor is a generalization of matrices to an arbitrary number of dimensions.
  3. In tensor, dimension is often called axis.
  4. Number of dimension (=Number of axis) is called ranks.

A tensor is a generalized representation of the matrix.

name tensor Notation
Scalar 0 Dimensional Tensor Not Available
Vector 1 Dimensional Tensor (k)
Matrix 2 Dimensional Tensor (j,k)
.. 3 Dimensional tensor (i,j,l)

tensor-keyword.png

4. Tensor Shape

A tensor shape is information that indicates ** how many elements each Dimension (= axis) is composed of **. From experience, it is easy to understand Shape above 2D tensor (Matrix), but it is necessary to understand Shape of 0D tensor (Scalar) and 1D tensor (Vector) after seeing an example.

You can check the shape of Tensor with the shape command of Python.

4.1 Scalar : 0D Tensor

Scalar has empty shape.

Take, for example, the number 12. The number 12 is Scalar because there is only one number. Scalar is a 0D Tensor. That is, the Dimension of the Tensor is zero. There is no Dimension. Since the information on the number of elements of the missing Dimension is Shape, the Shape of Scalar can only be written in ** () **. This is called Scalar has empty shape. In English. It's a bit unreasonable, but please bear with Vector.

Let's output the Scalar Shape with the code below.

shape_scalar.png

4.2 Vector : 1D Tensor

1D tensor has a shape with a single element, such as (4, ).

The following is an example of a Vector.

\begin{bmatrix}
12 & 3 & 6 & 14 
\end{bmatrix}

Vector is a 1D tensor. Since there is only one Dimension, one number represents the Shape. And the number of Elements of this Vector is 4. Therefore, the Shape of this Vector is written as ** (4,) **. This may not come to mind, but you have to remember it.

Let's output the Shape of Vector with the following code.

shape-vector.png   4.3 Matrix : 2D Tensor

2D tensor has a shape such as (3,4). it is familiar with matrix representation.

The following is an example of Matrix.

\begin{bmatrix}
1 & 3 & 5 & 7 \\
2 & 4 & 6 & 8 \\
3 & 6 & 9 & 12 
\end{bmatrix}

The Shape of this Matrix is ** (3,4) **. It is the same as the notation of a matrix in mathematics so far. Actually, Matrix is a 2D Tensor, so it is written with two numbers. Each number will contain the number of Elements for each Dimension.

Let's output the Matrix Shape with the code below.

image.png

4.4 .. :3D Tensor

3D tensor has a shape (*3*,3,4).

The following is an example of a 3D tensor. When it comes to 3D Tensor, it becomes difficult to express it as a mathematical formula, so please use code notation. Imagine that there are three matrices (3,4) in a row. In this case, the Shape of this tensor is (3, 3, 4).

[[[1, 3, 5, 7],[2, 4, 6, 8],[3 ,6, 9,12]], [[1, 3, 5, 7],[2, 4, 6, 8],[3 ,6, 9,12]], [[1, 3, 5, 7],[2, 4, 6, 8],[3 ,6, 9,12]]]

image.png

5. Summary

I organized the meaning of Shape of Tensor and the expression in Python. Next time, I will introduce an example of Tensor.

name tensor Expression Shape
Scalar 0D Tensor Not Available ()
Vector 1D Tensor (k) (k,)
Matrix 2D Tensor (j,k) (j,k)
.. 3D tensor (i,j,l) (i,j,l)

6. Reference materials

  1. Understanding Tensor (1): Dimension

Recommended Posts

Understanding the Tensor (2): Shape
Understanding the Tensor (3): Real World Data
Understand the tensor product (numpy.tensordot)
About understanding the 3-point reader [...]
Distinguishing the agari shape of mahjong
[Python] Understanding the potential_field_planning of Python Robotics
Understanding and implementing the Tonelli-Shanks algorithm (1)
[Understanding in 3 minutes] The beginning of Linux
Arrange the numbers in a spiral shape