LoginSignup
2
4

More than 3 years have passed since last update.

テンソル(Tensor)の理解(2) : Shape

Last updated at Posted at 2020-03-11

1.はじめに

Tensorの理解に必要な情報を、自分用メモとして整理します。
同時に、PythonにおけるTensorの表記法もご紹介します。
*英語の表現が直感的で理解しやすいため、訳さずにそのまま記入致します。

前回の記事に続き、今回はTensorのShapeについてご紹介します。
テンソル(Tensor)の理解 (1) : Dimension

2.テンソルとは?

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

テンソルは、数字を入れるコンテナです。シンプルですね。

3.テンソルのキーワード

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.

テンソルは、マトリックスの一般化された表現。

名称 テンソル 表記
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.テンソルのShape

テンソルのShapeとは、各Dimension(=axis)がいくつの要素(Element)で構成さらたかを表す情報です。
経験上、2Dテンソル(Matrix)以上は、Shapeの理解がしやすいですが、0Dテンソル(Scalar), 1Dテンソル(Vector)のShapeは、例を見てから理解する必要があります。

Pythonのshape命令で、TensorのShapeが確認できます。

4.1 Scalar : 0D Tensor

Scalar has empty shape.

例えば、数字12の例を挙げます。
数字12は、数字が一つあるだけなので、Scalarです。
Scalarは0D Tensorです。すなわちTensorのDimensionがゼロです。Dimensionがありません。
ないDimensionのElement数の情報がShapeですので、ScalarのShapeは、( )にしか表記できません。
これを英語では、Scalar has empty shape.と言います。
ちょっと理不尽な説明ですが、Vectorまで我慢してください。

下記のコードでScalarのShapeを出力してみます。

shape_scalar.png

4.2 Vector : 1D Tensor

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

次に、Vectorの例を挙げます。

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

Vectorは1Dテンソルです。Dimensionが一つしかないので、一つの数字でShapeを表します。
そして、このVectorのElements数は4です。
従って、このVectorのShapeは、(4, )と表記します。これはピンとこないかもしれませんが、覚えるしかありません。

下記のコードでVectorのShapeを出力してみます。

shape-vector.png
 

4.3 Matrix : 2D Tensor

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

次に、Matrixの例を挙げます。

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

このMatrixのShapeは(3,4)です。これまでの数学での行列の表記と同じです。
実際には、Matrixは2D Tensorなので、二つの数字で表記。それぞれの数字には各DimensionのElements数を入れることになります。

下記のコードで、MatrixのShapeを出力してみます。

image.png

4.4 .. :3D Tensor

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

次に、3Dテンソルの例を挙げます。
3D Tensorになると、数式として表現が難しくなりますので、コード表記とさせてください。
(3,4)の行列が三つ並んでいることを頭に描いてみてください。
この場合、このテンソルのShapeは、(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. まとめ

TensorのShapeの意味、Pythonでの表現について整理しました。
次回は、Tensorの実例についてご紹介します。

名称 テンソル 表現 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. 参考資料

  1. テンソル(Tensor)の理解 (1) : Dimension
2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4