第2章準備
2.3.9. Matrix–Vector Products2.3.9 行列とベクトルの積
Now that we know how to calculate dot products, we can begin to understand the product between an matrix and an -dimensional vector . To start off, we visualize our matrix in terms of its row vectors
英語のヒント
内積の計算のしかたがわかったので、の行列と次元のベクトルの積を理解し始められる。まず、行列をその行ベクトルで見てみよう。
(2.3.5)
where each is a row vector representing the row of the matrix .
英語のヒント
ここで各は、行列の行目を表す行ベクトルである。
The matrix–vector product is simply a column vector of length , whose element is the dot product :
英語のヒント
行列とベクトルの積は、単に長さの列ベクトルで、その番目の要素は内積である。
(2.3.6)
We can think of multiplication with a matrix as a transformation that projects vectors from to . These transformations are remarkably useful. For example, we can represent rotations as multiplications by certain square matrices. Matrix–vector products also describe the key calculation involved in computing the outputs of each layer in a neural network given the outputs from the previous layer.
英語のヒント
行列を掛けることは、ベクトルをからへ射影する変換と考えられる。こうした変換は驚くほど役に立つ。たとえば、回転は、ある種の正方行列を掛けることとして表せる。行列とベクトルの積は、ニューラルネットワークで、前の層の出力が与えられたときに各層の出力を計算する際の鍵となる計算も記述する。
To express a matrix–vector product in code, we use the
mv function. Note that the column dimension of A (its length along axis 1) must be the same as the dimension of x (its length). Python has a convenience operator @ that can execute both matrix–vector and matrix–matrix products (depending on its arguments). Thus we can write A@x.英語のヒント
コードで行列とベクトルの積を表すには、
mv関数を使う。Aの列の次元(軸1に沿った長さ)は、xの次元(その長さ)と同じでなければならないことに注意してほしい。Pythonには、(引数に応じて)行列とベクトルの積も行列どうしの積も実行できる便利な演算子@がある。したがってA@xと書ける。A.shape, x.shape, torch.mv(A, x), A@x(torch.Size([2, 3]), torch.Size([3]), tensor([ 5., 14.]), tensor([ 5., 14.]))