.. _sec_ndarray: データ操作 ========== 何らかの実用的な処理を行うには、データを保存し、操作するための方法が必要になる。一般的に、データに対して行うべき重要なことは2つある。(i) データを取得すること、そして (ii) 取得したデータをコンピュータに取り込んだ後に処理することである。データを保存する方法がなければ、データを取得しても意味がない。そこでまずは、\ *テンソル*\ とも呼ばれる :math:`n` 次元配列の操作に慣れていきよう。NumPy のような科学技術計算パッケージをすでに知っているなら、これは簡単だろう。現代のあらゆる深層学習フレームワークにおいて、\ *テンソルクラス*\ (MXNet では ``ndarray``\ 、PyTorch と TensorFlow では ``Tensor``\ )は NumPy の ``ndarray`` に似ているが、いくつかの強力な機能が追加されている。第一に、テンソルクラスは自動微分をサポートしている。第二に、NumPy が CPU 上でしか動作しないのに対し、テンソルは GPU を活用して数値計算を高速化できる。これらの特性により、ニューラルネットワークは実装しやすく、かつ高速に実行できるようになる。 はじめに -------- まず、PyTorch ライブラリをインポートする。パッケージ名は ``torch`` であることに注意しよ。 .. raw:: html
pytorchmxnetjaxtensorflow
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python import torch .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python from mxnet import np, npx npx.set_np() .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python import jax from jax import numpy as jnp .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python import tensorflow as tf .. raw:: html
.. raw:: html
| テンソルは、数値の(場合によっては多次元の)配列を表す。 | 1次元の場合、すなわちデータに1つの軸しか必要ない場合、テンソルは *ベクトル* と呼ばれる。2つの軸を持つテンソルは *行列* と呼ばれる。\ :math:`k > 2` の軸を持つ場合は、特別な名前は使わず、単に :math:`k`-*階テンソル* と呼ぶ。 PyTorch には、値をあらかじめ入れた新しいテンソルを作成するためのさまざまな関数がある。たとえば ``arange(n)`` を呼び出すと、0(含む)から ``n``\ (含まない)までの等間隔の値を持つベクトルを作成できる。デフォルトでは、間隔の大きさは :math:`1` である。特に指定しない限り、新しいテンソルは主記憶に保存され、CPU ベースの計算に割り当てられる。 .. raw:: html
pytorchmxnetjaxtensorflow
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python x = torch.arange(12, dtype=torch.float32) x .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output tensor([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11.]) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python x = np.arange(12) x .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output [07:03:50] ../src/storage/storage.cc:196: Using Pooled (Naive) StorageManager for CPU .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11.]) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python x = jnp.arange(12) x .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output No GPU/TPU found, falling back to CPU. (Set TF_CPP_MIN_LOG_LEVEL=0 and rerun for more info.) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output Array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], dtype=int32) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python x = tf.range(12, dtype=tf.float32) x .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output .. raw:: html
.. raw:: html
これらの各値はテンソルの *要素* と呼ばれる。テンソル ``x`` には 12 個の要素がある。テンソル内の要素数の総数は ``numel`` メソッドで確認できる。 .. raw:: html
pytorchmxnetjaxtensorflow
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python x.numel() .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output 12 .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python x.size .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output 12 .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python x.size .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output 12 .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python tf.size(x) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output .. raw:: html
.. raw:: html
(\ **テンソルの shape\ (各軸に沿った長さ)**\ )は ``shape`` 属性を調べることで取得できる。ここではベクトルを扱っているので、\ ``shape`` には1つの要素しか含まれず、サイズと同じである。 .. raw:: latex \diilbookstyleinputcell .. code:: python x.shape .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output torch.Size([12]) ``reshape`` を呼び出すことで、サイズや値を変えずにテンソルの形状を変更できる。たとえば、形状が (12,) のベクトル ``x`` を、形状が (3, 4) の行列 ``X`` に変換できる。この新しいテンソルはすべての要素を保持したまま、それらを行列として再配置する。ベクトルの要素は1行ずつ並んでいるので、\ ``x[3] == X[0, 3]`` となることに注意しよ。 .. raw:: html
pytorchmxnetjaxtensorflow
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python X = x.reshape(3, 4) X .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output tensor([[ 0., 1., 2., 3.], [ 4., 5., 6., 7.], [ 8., 9., 10., 11.]]) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python X = x.reshape(3, 4) X .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output array([[ 0., 1., 2., 3.], [ 4., 5., 6., 7.], [ 8., 9., 10., 11.]]) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python X = x.reshape(3, 4) X .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output Array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]], dtype=int32) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python X = tf.reshape(x, (3, 4)) X .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output .. raw:: html
.. raw:: html
``reshape`` に対して各 shape 成分をすべて明示するのは冗長であることに注意しよ。テンソルのサイズはすでに分かっているので、他の成分が分かれば shape の1成分を求められる。たとえば、サイズが :math:`n` で目標の shape が (:math:`h`, :math:`w`) のテンソルでは、\ :math:`w = n/h` であることが分かりる。shape の成分を自動的に推定させるには、自動推定されるべき成分に ``-1`` を指定する。ここでは、\ ``x.reshape(3, 4)`` の代わりに ``x.reshape(-1, 4)`` や ``x.reshape(3, -1)`` と書いても同じである。 実務では、すべて 0 や 1 で初期化されたテンソルを扱うことがよくある。すべての要素が 0 に設定されたテンソルを構築する(\ [STRIKEOUT:または 1]\ )には、\ ``zeros`` 関数を使って shape を (2, 3, 4) にする。 .. raw:: html
pytorchmxnetjaxtensorflow
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python torch.zeros((2, 3, 4)) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output tensor([[[0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.]], [[0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.]]]) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python np.zeros((2, 3, 4)) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output array([[[0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.]], [[0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.]]]) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python jnp.zeros((2, 3, 4)) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output Array([[[0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.]], [[0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.]]], dtype=float32) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python tf.zeros((2, 3, 4)) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output .. raw:: html
.. raw:: html
同様に、\ ``ones`` を呼び出すことで、すべて 1 のテンソルを作成できる。 .. raw:: html
pytorchmxnetjaxtensorflow
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python torch.ones((2, 3, 4)) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output tensor([[[1., 1., 1., 1.], [1., 1., 1., 1.], [1., 1., 1., 1.]], [[1., 1., 1., 1.], [1., 1., 1., 1.], [1., 1., 1., 1.]]]) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python np.ones((2, 3, 4)) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output array([[[1., 1., 1., 1.], [1., 1., 1., 1.], [1., 1., 1., 1.]], [[1., 1., 1., 1.], [1., 1., 1., 1.], [1., 1., 1., 1.]]]) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python jnp.ones((2, 3, 4)) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output Array([[[1., 1., 1., 1.], [1., 1., 1., 1.], [1., 1., 1., 1.]], [[1., 1., 1., 1.], [1., 1., 1., 1.], [1., 1., 1., 1.]]], dtype=float32) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python tf.ones((2, 3, 4)) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output .. raw:: html
.. raw:: html
しばしば、与えられた確率分布から各要素をランダムに(かつ独立に)サンプリングする必要がある。たとえば、ニューラルネットワークのパラメータはしばしばランダムに初期化される。次のコード片は、平均 0、標準偏差 1 の標準ガウス(正規)分布から要素を抽出したテンソルを作成する。 .. raw:: html
pytorchmxnetjaxtensorflow
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python torch.randn(3, 4) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output tensor([[ 1.5124, 0.2588, 0.5924, 0.2041], [-0.9516, 1.5309, 0.7024, 0.0892], [ 1.4337, -0.3017, -1.4316, 0.5385]]) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python np.random.normal(0, 1, size=(3, 4)) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output array([[ 2.2122064 , 1.1630787 , 0.7740038 , 0.4838046 ], [ 1.0434403 , 0.29956347, 1.1839255 , 0.15302546], [ 1.8917114 , -1.1688148 , -1.2347414 , 1.5580711 ]]) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python # Any call of a random function in JAX requires a key to be # specified, feeding the same key to a random function will # always result in the same sample being generated jax.random.normal(jax.random.PRNGKey(0), (3, 4)) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output Array([[ 1.1901639 , -1.0996888 , 0.44367844, 0.5984697 ], [-0.39189556, 0.69261974, 0.46018356, -2.068578 ], [-0.21438177, -0.9898306 , -0.6789304 , 0.27362573]], dtype=float32) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python tf.random.normal(shape=[3, 4]) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output .. raw:: html
.. raw:: html
最後に、数値リテラルを含む(場合によっては入れ子になった)Python のリストを与えることで、各要素の正確な値を指定してテンソルを構築できる。ここでは、リストのリストを使って行列を構築している。最外側のリストが軸 0 に対応し、内側のリストが軸 1 に対応する。 .. raw:: html
pytorchmxnetjaxtensorflow
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python torch.tensor([[2, 1, 4, 3], [1, 2, 3, 4], [4, 3, 2, 1]]) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output tensor([[2, 1, 4, 3], [1, 2, 3, 4], [4, 3, 2, 1]]) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python np.array([[2, 1, 4, 3], [1, 2, 3, 4], [4, 3, 2, 1]]) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output array([[2., 1., 4., 3.], [1., 2., 3., 4.], [4., 3., 2., 1.]]) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python jnp.array([[2, 1, 4, 3], [1, 2, 3, 4], [4, 3, 2, 1]]) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output Array([[2, 1, 4, 3], [1, 2, 3, 4], [4, 3, 2, 1]], dtype=int32) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python tf.constant([[2, 1, 4, 3], [1, 2, 3, 4], [4, 3, 2, 1]]) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output .. raw:: html
.. raw:: html
インデックス付けとスライシング ------------------------------ Python のリストと同様に、インデックス付け(0 から始まる)によってテンソルの要素にアクセスできる。リストの末尾からの位置に基づいて要素にアクセスするには、負のインデックスを使う。さらに、スライシング(たとえば ``X[start:stop]``\ )によってインデックスの範囲全体にアクセスできる。このとき返される値には最初のインデックス(\ ``start``\ )は含まれるが、最後のインデックス(\ ``stop``\ )は含まれない。最後に、\ :math:`k`-*階テンソル* に対して1つのインデックス(またはスライス)だけを指定した場合、それは軸 0 に沿って適用される。したがって、次のコードでは、\ ``[-1]`` は最後の行を選択し、\ ``[1:3]`` は2行目と3行目を選択する。 .. raw:: latex \diilbookstyleinputcell .. code:: python X[-1], X[1:3] .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output (tensor([ 8., 9., 10., 11.]), tensor([[ 4., 5., 6., 7.], [ 8., 9., 10., 11.]])) 読み取るだけでなく、(\ **インデックスを指定して行列の要素を 書き込む こともできる。**\ ) .. raw:: html
pytorchmxnetjaxtensorflow
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python X[1, 2] = 17 X .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output tensor([[ 0., 1., 2., 3.], [ 4., 5., 17., 7.], [ 8., 9., 10., 11.]]) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python X[1, 2] = 17 X .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output array([[ 0., 1., 2., 3.], [ 4., 5., 17., 7.], [ 8., 9., 10., 11.]]) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python # JAX arrays are immutable. jax.numpy.ndarray.at index # update operators create a new array with the corresponding # modifications made X_new_1 = X.at[1, 2].set(17) X_new_1 .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output Array([[ 0, 1, 2, 3], [ 4, 5, 17, 7], [ 8, 9, 10, 11]], dtype=int32) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python X_var = tf.Variable(X) X_var[1, 2].assign(9) X_var .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output .. raw:: html
.. raw:: html
複数の要素に同じ値を代入したい場合は、代入操作の左辺でインデックス付けを行う。 たとえば、\ ``[:2, :]`` は1行目と2行目にアクセスし、\ ``:`` は軸 1(列)に沿ったすべての要素を取りる。ここでは行列についてインデックス付けを説明したが、これはベクトルや2次元より高いテンソルにも同様に使える。 .. raw:: html
pytorchmxnetjaxtensorflow
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python X[:2, :] = 12 X .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output tensor([[12., 12., 12., 12.], [12., 12., 12., 12.], [ 8., 9., 10., 11.]]) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python X[:2, :] = 12 X .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output array([[12., 12., 12., 12.], [12., 12., 12., 12.], [ 8., 9., 10., 11.]]) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python X_new_2 = X_new_1.at[:2, :].set(12) X_new_2 .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output Array([[12, 12, 12, 12], [12, 12, 12, 12], [ 8, 9, 10, 11]], dtype=int32) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python X_var = tf.Variable(X) X_var[:2, :].assign(tf.ones(X_var[:2,:].shape, dtype=tf.float32) * 12) X_var .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output .. raw:: html
.. raw:: html
演算 ---- テンソルの構築方法と、その要素の読み書き方法が分かったので、さまざまな数学演算を使ってテンソルを操作できるようになる。中でも最も有用なのは *要素ごとの* 演算である。これは、標準的なスカラー演算をテンソルの各要素に適用する。2つのテンソルを入力に取る関数では、要素ごとの演算は対応する要素の各組に標準的な二項演算子を適用する。スカラーをスカラーへ写す任意の関数から、要素ごとの関数を作ることができる。 数学記法では、このような *単項* スカラー演算子(1つの入力を取る)を、シグネチャ :math:`f: \mathbb{R} \rightarrow \mathbb{R}` で表す。これは、関数が任意の実数を別の実数へ写すことを意味する。\ :math:`e^x` のような単項演算子を含む、ほとんどの標準的な演算子は要素ごとに適用できる。 .. raw:: html
pytorchmxnetjaxtensorflow
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python torch.exp(x) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output tensor([162754.7969, 162754.7969, 162754.7969, 162754.7969, 162754.7969, 162754.7969, 162754.7969, 162754.7969, 2980.9580, 8103.0840, 22026.4648, 59874.1406]) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python np.exp(x) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output array([1.0000000e+00, 2.7182817e+00, 7.3890562e+00, 2.0085537e+01, 5.4598148e+01, 1.4841316e+02, 4.0342880e+02, 1.0966332e+03, 2.9809580e+03, 8.1030840e+03, 2.2026465e+04, 5.9874141e+04]) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python jnp.exp(x) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output Array([1.0000000e+00, 2.7182817e+00, 7.3890562e+00, 2.0085537e+01, 5.4598152e+01, 1.4841316e+02, 4.0342880e+02, 1.0966332e+03, 2.9809580e+03, 8.1030840e+03, 2.2026465e+04, 5.9874141e+04], dtype=float32) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python tf.exp(x) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output .. raw:: html
.. raw:: html
同様に、実数の組を1つの実数へ写す *二項* スカラー演算子は、シグネチャ :math:`f: \mathbb{R}, \mathbb{R} \rightarrow \mathbb{R}` で表す。任意の2つのベクトル :math:`\mathbf{u}` と :math:`\mathbf{v}` が *同じ shape* を持ち、二項演算子 :math:`f` が与えられたとき、各 :math:`i` について :math:`c_i \gets f(u_i, v_i)` と置くことで、ベクトル :math:`\mathbf{c} = F(\mathbf{u},\mathbf{v})` を生成できる。ここで、\ :math:`c_i, u_i, v_i` はそれぞれベクトル :math:`\mathbf{c}, \mathbf{u}, \mathbf{v}` の :math:`i`-*番目* の要素である。ここでは、スカラー関数を要素ごとのベクトル演算へ *持ち上げる* ことで、ベクトル値の :math:`F: \mathbb{R}^d, \mathbb{R}^d \rightarrow \mathbb{R}^d` を得ている。加算(\ ``+``\ )、減算(\ ``-``\ )、乗算(\ ``*``\ )、除算(\ ``/``\ )、べき乗(\ ``**``\ )といった一般的な標準算術演算子は、任意の shape を持つ同じ shape のテンソルに対して、すべて *持ち上げられ*\ 、要素ごとの演算として使えるようになっている。 .. raw:: html
pytorchmxnetjaxtensorflow
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python x = torch.tensor([1.0, 2, 4, 8]) y = torch.tensor([2, 2, 2, 2]) x + y, x - y, x * y, x / y, x ** y .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output (tensor([ 3., 4., 6., 10.]), tensor([-1., 0., 2., 6.]), tensor([ 2., 4., 8., 16.]), tensor([0.5000, 1.0000, 2.0000, 4.0000]), tensor([ 1., 4., 16., 64.])) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python x = np.array([1, 2, 4, 8]) y = np.array([2, 2, 2, 2]) x + y, x - y, x * y, x / y, x ** y .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output (array([ 3., 4., 6., 10.]), array([-1., 0., 2., 6.]), array([ 2., 4., 8., 16.]), array([0.5, 1. , 2. , 4. ]), array([ 1., 4., 16., 64.])) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python x = jnp.array([1.0, 2, 4, 8]) y = jnp.array([2, 2, 2, 2]) x + y, x - y, x * y, x / y, x ** y .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output (Array([ 3., 4., 6., 10.], dtype=float32), Array([-1., 0., 2., 6.], dtype=float32), Array([ 2., 4., 8., 16.], dtype=float32), Array([0.5, 1. , 2. , 4. ], dtype=float32), Array([ 1., 4., 16., 64.], dtype=float32)) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python x = tf.constant([1.0, 2, 4, 8]) y = tf.constant([2.0, 2, 2, 2]) x + y, x - y, x * y, x / y, x ** y .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output (, , , , ) .. raw:: html
.. raw:: html
要素ごとの計算に加えて、内積や行列積のような線形代数演算も実行できる。これらについては :numref:`sec_linear-algebra` で詳しく説明する。 また、複数のテンソルを\ *連結* することもでき、端から端へつなげてより大きなテンソルを作れる。そのためには、テンソルのリストを与え、どの軸に沿って連結するかをシステムに指定するだけである。以下の例では、2つの行列を列(軸 1)ではなく行(軸 0)に沿って連結したときに何が起こるかを示している。最初の出力の軸 0 の長さ(\ :math:`6`\ )は、2つの入力テンソルの軸 0 の長さ(\ :math:`3 + 3`\ )の和であり、2つ目の出力の軸 1 の長さ(\ :math:`8`\ )は、2つの入力テンソルの軸 1 の長さ(\ :math:`4 + 4`\ )の和であることが分かりる。 .. raw:: html
pytorchmxnetjaxtensorflow
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python X = torch.arange(12, dtype=torch.float32).reshape((3,4)) Y = torch.tensor([[2.0, 1, 4, 3], [1, 2, 3, 4], [4, 3, 2, 1]]) torch.cat((X, Y), dim=0), torch.cat((X, Y), dim=1) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output (tensor([[ 0., 1., 2., 3.], [ 4., 5., 6., 7.], [ 8., 9., 10., 11.], [ 2., 1., 4., 3.], [ 1., 2., 3., 4.], [ 4., 3., 2., 1.]]), tensor([[ 0., 1., 2., 3., 2., 1., 4., 3.], [ 4., 5., 6., 7., 1., 2., 3., 4.], [ 8., 9., 10., 11., 4., 3., 2., 1.]])) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python X = np.arange(12).reshape(3, 4) Y = np.array([[2, 1, 4, 3], [1, 2, 3, 4], [4, 3, 2, 1]]) np.concatenate([X, Y], axis=0), np.concatenate([X, Y], axis=1) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output (array([[ 0., 1., 2., 3.], [ 4., 5., 6., 7.], [ 8., 9., 10., 11.], [ 2., 1., 4., 3.], [ 1., 2., 3., 4.], [ 4., 3., 2., 1.]]), array([[ 0., 1., 2., 3., 2., 1., 4., 3.], [ 4., 5., 6., 7., 1., 2., 3., 4.], [ 8., 9., 10., 11., 4., 3., 2., 1.]])) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python X = jnp.arange(12, dtype=jnp.float32).reshape((3, 4)) Y = jnp.array([[2.0, 1, 4, 3], [1, 2, 3, 4], [4, 3, 2, 1]]) jnp.concatenate((X, Y), axis=0), jnp.concatenate((X, Y), axis=1) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output (Array([[ 0., 1., 2., 3.], [ 4., 5., 6., 7.], [ 8., 9., 10., 11.], [ 2., 1., 4., 3.], [ 1., 2., 3., 4.], [ 4., 3., 2., 1.]], dtype=float32), Array([[ 0., 1., 2., 3., 2., 1., 4., 3.], [ 4., 5., 6., 7., 1., 2., 3., 4.], [ 8., 9., 10., 11., 4., 3., 2., 1.]], dtype=float32)) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python X = tf.reshape(tf.range(12, dtype=tf.float32), (3, 4)) Y = tf.constant([[2.0, 1, 4, 3], [1, 2, 3, 4], [4, 3, 2, 1]]) tf.concat([X, Y], axis=0), tf.concat([X, Y], axis=1) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output (, ) .. raw:: html
.. raw:: html
ときには、論理式を通じて二値テンソルを構築したいことがある。\ ``X == Y`` を例にしよう。各位置 :math:`i, j` について、\ ``X[i, j]`` と ``Y[i, j]`` が等しければ、結果の対応する要素は 1 になり、そうでなければ 0 になる。 .. raw:: latex \diilbookstyleinputcell .. code:: python X == Y .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output tensor([[False, True, False, True], [False, False, False, False], [False, False, False, False]]) テンソルのすべての要素を合計すると、要素が1つだけのテンソルが得られる。 .. raw:: html
pytorchmxnetjaxtensorflow
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python X.sum() .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output tensor(66.) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python X.sum() .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output array(66.) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python X.sum() .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output Array(66., dtype=float32) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python tf.reduce_sum(X) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output .. raw:: html
.. raw:: html
.. _subsec_broadcasting: ブロードキャスティング ---------------------- ここまでで、同じ shape を持つ2つのテンソルに対して要素ごとの二項演算を行う方法を学んだ。ある条件下では、shape が異なっていても、ブロードキャスティング機構を呼び出すことで要素ごとの二項演算を実行できる。 ブロードキャスティングは次の2段階の手順に従いる。(i) 長さ 1 の軸に沿って要素をコピーすることで、一方または両方の配列を拡張し、この変換後に2つのテンソルが同じ shape になるようにする。(ii) 得られた配列に対して要素ごとの演算を行う。 .. raw:: html
pytorchmxnetjaxtensorflow
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python a = torch.arange(3).reshape((3, 1)) b = torch.arange(2).reshape((1, 2)) a, b .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output (tensor([[0], [1], [2]]), tensor([[0, 1]])) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python a = np.arange(3).reshape(3, 1) b = np.arange(2).reshape(1, 2) a, b .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output (array([[0.], [1.], [2.]]), array([[0., 1.]])) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python a = jnp.arange(3).reshape((3, 1)) b = jnp.arange(2).reshape((1, 2)) a, b .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output (Array([[0], [1], [2]], dtype=int32), Array([[0, 1]], dtype=int32)) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python a = tf.reshape(tf.range(3), (3, 1)) b = tf.reshape(tf.range(2), (1, 2)) a, b .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output (, ) .. raw:: html
.. raw:: html
``a`` と ``b`` はそれぞれ :math:`3\times1` と :math:`1\times2` の行列なので、shape は一致しない。ブロードキャスティングでは、行列 ``a`` を列方向に、行列 ``b`` を行方向に複製してから要素ごとに加算することで、より大きな :math:`3\times2` 行列を生成する。 .. raw:: latex \diilbookstyleinputcell .. code:: python a + b .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output tensor([[0, 1], [1, 2], [2, 3]]) メモリの節約 ------------ 演算を実行すると、結果を保持するために新しいメモリが割り当てられることがある。 たとえば ``Y = X + Y`` と書くと、\ ``Y`` が以前指していたテンソルへの参照を外し、代わりに ``Y`` を新しく割り当てられたメモリに向ける。この問題は Python の ``id()`` 関数で確認できる。これは参照先オブジェクトのメモリアドレスを正確に返す。\ ``Y = Y + X`` を実行した後、\ ``id(Y)`` が別の場所を指していることに注意しよ。これは、Python がまず ``Y + X`` を評価して結果用の新しいメモリを割り当て、その後 ``Y`` をこの新しいメモリ位置に向けるからである。 .. raw:: latex \diilbookstyleinputcell .. code:: python before = id(Y) Y = Y + X id(Y) == before .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output False これは2つの理由で望ましくない場合がある。第一に、不要なメモリ割り当てを何度も行いたくないからである。機械学習では、しばしば数百メガバイト規模のパラメータを持ち、それらすべてを毎秒何度も更新する。可能な限り、これらの更新は *インプレース* で行いたいものである。第二に、同じパラメータを複数の変数から参照していることがある。インプレース更新をしない場合、メモリリークを起こしたり、古いパラメータを誤って参照したりしないよう、これらすべての参照を注意深く更新しなければならない。 幸いなことに、インプレース演算の実行は簡単である。スライス記法 ``Y[:] = `` を使うことで、演算結果をすでに割り当て済みの配列 ``Y`` に代入できる。この概念を示すために、\ ``zeros_like`` を使って ``Y`` と同じ shape を持つように初期化したテンソル ``Z`` の値を上書きする。 .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python Z = torch.zeros_like(Y) print('id(Z):', id(Z)) Z[:] = X + Y print('id(Z):', id(Z)) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output id(Z): 139859127252688 id(Z): 139859127252688 .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python Z = np.zeros_like(Y) print('id(Z):', id(Z)) Z[:] = X + Y print('id(Z):', id(Z)) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output id(Z): 140413152328000 id(Z): 140413152328000 .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python # JAX arrays do not allow in-place operations .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python Z = tf.Variable(tf.zeros_like(Y)) print('id(Z):', id(Z)) Z.assign(X + Y) print('id(Z):', id(Z)) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output id(Z): 140582589136560 id(Z): 140582589136560 .. raw:: html
.. raw:: html
``X`` の値が後続の計算で再利用されないなら、\ ``X[:] = X + Y`` や ``X += Y`` を使って演算のメモリオーバーヘッドを減らすこともできる。 .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python before = id(X) X += Y id(X) == before .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output True .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python before = id(X) X += Y id(X) == before .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output True .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python A = jax.device_get(X) B = jax.device_put(A) type(A), type(B) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output (numpy.ndarray, jaxlib.xla_extension.ArrayImpl) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python @tf.function def computation(X, Y): Z = tf.zeros_like(Y) # This unused value will be pruned out A = X + Y # Allocations will be reused when no longer needed B = A + Y C = B + Y return C + Y computation(X, Y) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output .. raw:: html
.. raw:: html
他の Python オブジェクトへの変換 -------------------------------- NumPy テンソル(\ ``ndarray``\ )への変換、またはその逆は簡単である。変換結果はメモリを共有しない。この小さな不便さは実は非常に重要である。CPU や GPU 上で演算を行うとき、Python の NumPy パッケージが同じメモリ領域を使って別のことをしたいかどうかを待つために、計算を止めたくはないからである。 NumPy テンソル(\ ``ndarray``\ )への変換、またはその逆は簡単である。torch テンソルと NumPy 配列は基盤となるメモリを共有し、一方をインプレース演算で変更すると、もう一方も変更される。 .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python A = X.numpy() B = torch.from_numpy(A) type(A), type(B) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output (numpy.ndarray, torch.Tensor) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python A = X.asnumpy() B = np.array(A) type(A), type(B) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output (numpy.ndarray, mxnet.numpy.ndarray) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python a = jnp.array([3.5]) a, a.item(), float(a), int(a) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output (Array([3.5], dtype=float32), 3.5, 3.5, 3) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python A = X.numpy() B = tf.constant(A) type(A), type(B) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output (numpy.ndarray, tensorflow.python.framework.ops.EagerTensor) .. raw:: html
.. raw:: html
サイズ 1 のテンソルを Python のスカラーに変換するには、\ ``item`` 関数や Python の組み込み関数を呼び出せる。 .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python a = torch.tensor([3.5]) a, a.item(), float(a), int(a) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output (tensor([3.5000]), 3.5, 3.5, 3) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python a = np.array([3.5]) a, a.item(), float(a), int(a) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output (array([3.5]), 3.5, 3.5, 3) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python a = tf.constant([3.5]).numpy() a, a.item(), float(a), int(a) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output (array([3.5], dtype=float32), 3.5, 3.5, 3) .. raw:: html
.. raw:: html
要約 ---- テンソルクラスは、深層学習ライブラリにおいてデータを保存し操作するための主要なインターフェースである。テンソルは、構築ルーチン、インデックス付けとスライシング、基本的な数学演算、ブロードキャスティング、メモリ効率のよい代入、そして他の Python オブジェクトとの相互変換など、さまざまな機能を提供する。 演習 ---- 1. この節のコードを実行しよ。条件式 ``X == Y`` を ``X < Y`` または ``X > Y`` に変えて、どのようなテンソルが得られるか確認しよ。 2. ブロードキャスティング機構で要素ごとに作用する2つのテンソルを、3次元テンソルなど別の shape に置き換えよ。結果は期待どおりになるか?