プリンキピア

第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 m×n matrix 𝐀 and an n-dimensional vector 𝐱. To start off, we visualize our matrix in terms of its row vectors
英語のヒント
内積の計算のしかたがわかったので、m×nの行列𝐀とn次元のベクトル𝐱の積を理解し始められる。まず、行列をその行ベクトルで見てみよう。
𝐀=[𝐚1⊤𝐚2⊤⋮𝐚m⊤],(2.3.5)
where each 𝐚i⊤∈ℝn is a row vector representing the ith row of the matrix 𝐀.
英語のヒント
ここで各𝐚i⊤∈ℝnは、行列𝐀のith行目を表す行ベクトルである。
The matrix–vector product 𝐀𝐱 is simply a column vector of length m, whose ith element is the dot product 𝐚i⊤𝐱:
英語のヒント
行列とベクトルの積𝐀𝐱は、単に長さmの列ベクトルで、そのith番目の要素は内積𝐚i⊤𝐱である。
𝐀𝐱=[𝐚1⊤𝐚2⊤⋮𝐚m⊤]𝐱=[𝐚1⊤𝐱𝐚2⊤𝐱⋮𝐚m⊤𝐱].(2.3.6)
We can think of multiplication with a matrix 𝐀∈ℝm×n as a transformation that projects vectors from ℝn to ℝm. 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.
英語のヒント
行列𝐀∈ℝm×nを掛けることは、ベクトルをℝnからℝmへ射影する変換と考えられる。こうした変換は驚くほど役に立つ。たとえば、回転は、ある種の正方行列を掛けることとして表せる。行列とベクトルの積は、ニューラルネットワークで、前の層の出力が与えられたときに各層の出力を計算する際の鍵となる計算も記述する。
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.]))