プリンキピア

第22章付録:深層学習のための数学

22.1.2. Dot Products and Angles22.1.2 内積と角度

As we saw in Section 2.3, if we take two column vectors 𝐮 and 𝐯, we can form their dot product by computing:
英語のヒント
2.3節で見たように、二つの列ベクトル𝐮と𝐯をとると、次の計算でそれらの内積を作れる。
𝐮⊤𝐯=∑iui⋅vi.(22.1.3)
Because (22.1.3) is symmetric, we will mirror the notation of classical multiplication and write
英語のヒント
式(22.1.3)は対称なので、通常の掛け算の記法をまねて次のように書く。
𝐮⋅𝐯=𝐮⊤𝐯=𝐯⊤𝐮,(22.1.4)
to highlight the fact that exchanging the order of the vectors will yield the same answer.
英語のヒント
これは、ベクトルの順序を入れ替えても同じ答えになるという事実を強調するためである。
The dot product (22.1.3) also admits a geometric interpretation: it is closely related to the angle between two vectors. Consider the angle shown in Fig. 22.1.4.
英語のヒント
内積の式(22.1.3)には幾何学的な解釈もあり、二つのベクトルの間の角度と密接に関係している。図22.1.4に示した角度を考えよう。
Fig. 22.1.4図22.1.4
Between any two vectors in the plane there is a well defined angle θ. We will see this angle is intimately tied to the dot product.
英語のヒント
平面上の任意の二つのベクトルの間には、きちんと定義された角度θがある。この角度が内積と密接に結びついていることを見ていく。
To start, let’s consider two specific vectors:
英語のヒント
まず、二つの特定のベクトルを考えよう。
𝐯=(r,0)and𝐰=(scos⁡(θ),ssin⁡(θ)).(22.1.5)
The vector 𝐯 is length r and runs parallel to the x-axis, and the vector 𝐰 is of length s and at angle θ with the x-axis. If we compute the dot product of these two vectors, we see that
英語のヒント
ベクトル𝐯は長さrでx軸に平行であり、ベクトル𝐰は長さsでx軸と角度θをなす。これら二つのベクトルの内積を計算すると、次のようになる。
𝐯⋅𝐰=rscos⁡(θ)=∥𝐯∥∥𝐰∥cos⁡(θ).(22.1.6)
With some simple algebraic manipulation, we can rearrange terms to obtain
英語のヒント
簡単な代数的操作で項を並べ替えると、次が得られる。
θ=arccos⁡(𝐯⋅𝐰∥𝐯∥∥𝐰∥).(22.1.7)
In short, for these two specific vectors, the dot product combined with the norms tell us the angle between the two vectors. This same fact is true in general. We will not derive the expression here, however, if we consider writing ∥𝐯−𝐰∥2 in two ways: one with the dot product, and the other geometrically using the law of cosines, we can obtain the full relationship. Indeed, for any two vectors 𝐯 and 𝐰, the angle between the two vectors is
英語のヒント
要するに、これら二つの特定のベクトルについては、内積とノルムを組み合わせると二つのベクトルの間の角度がわかる。同じ事実は一般にも成り立つ。ここではその式を導かないが、∥𝐯−𝐰∥2を、内積を使う書き方と、余弦定理を使って幾何学的に書く書き方の二通りで書くことを考えると、完全な関係が得られる。実際、任意の二つのベクトル𝐯と𝐰について、二つのベクトルの間の角度は次のようになる。
θ=arccos⁡(𝐯⋅𝐰∥𝐯∥∥𝐰∥).(22.1.8)
This is a nice result since nothing in the computation references two-dimensions. Indeed, we can use this in three or three million dimensions without issue.
英語のヒント
計算の中に2次元を前提とするものが何もないので、これはよい結果である。実際、これは3次元でも300万次元でも問題なく使える。
As a simple example, let’s see how to compute the angle between a pair of vectors:
英語のヒント
簡単な例として、ベクトルの組の間の角度を計算する方法を見よう。
%matplotlib inline
import torch
import torchvision
from IPython import display
from torchvision import transforms
from d2l import torch as d2l


def angle(v, w):
    return torch.acos(v.dot(w) / (torch.norm(v) * torch.norm(w)))

angle(torch.tensor([0, 1, 2], dtype=torch.float32), torch.tensor([2.0, 3, 4]))
tensor(0.4190)
We will not use it right now, but it is useful to know that we will refer to vectors for which the angle is π/2 (or equivalently 90∘) as being orthogonal. By examining the equation above, we see that this happens when θ=π/2, which is the same thing as cos⁡(θ)=0. The only way this can happen is if the dot product itself is zero, and two vectors are orthogonal if and only if 𝐯⋅𝐰=0. This will prove to be a helpful formula when understanding objects geometrically.
英語のヒント
今すぐには使わないが、角度がπ/2(あるいは同じことだが90∘)であるベクトルを直交していると呼ぶことは知っておくと役に立つ。上の式を調べると、これはθ=π/2のときに起き、それはcos⁡(θ)=0と同じことである。これが起きうる唯一の場合は内積そのものが0の場合であり、二つのベクトルが直交するのは𝐯⋅𝐰=0のとき、かつそのときに限る。これは、対象を幾何学的に理解するときに役立つ式となる。
It is reasonable to ask: why is computing the angle useful? The answer comes in the kind of invariance we expect data to have. Consider an image, and a duplicate image, where every pixel value is the same but 10% the brightness. The values of the individual pixels are in general far from the original values. Thus, if one computed the distance between the original image and the darker one, the distance can be large. However, for most ML applications, the content is the same—it is still an image of a cat as far as a cat/dog classifier is concerned. However, if we consider the angle, it is not hard to see that for any vector 𝐯, the angle between 𝐯 and 0.1⋅𝐯 is zero. This corresponds to the fact that scaling vectors keeps the same direction and just changes the length. The angle considers the darker image identical.
英語のヒント
次のように問うのはもっともである。なぜ角度を計算することが役に立つのか。その答えは、データに期待する種類の不変性にある。ある画像と、その複製の画像を考えよう。複製では、すべての画素の値が同じだが明るさが10%になっている。個々の画素の値は、一般にもとの値からかけ離れている。したがって、もとの画像と暗いほうの画像の間の距離を計算すると、その距離は大きくなりうる。しかし、たいていのMLの応用では、内容は同じである。猫と犬の分類器にとっては、それはやはり猫の画像である。しかし、角度を考えれば、任意のベクトル𝐯について、𝐯と0.1⋅𝐯の間の角度が0であることは難しくなくわかる。これは、ベクトルを定数倍しても方向は変わらず、長さだけが変わるという事実に対応する。角度は、暗いほうの画像を同一とみなす。
Examples like this are everywhere. In text, we might want the topic being discussed to not change if we write twice as long of document that says the same thing. For some encoding (such as counting the number of occurrences of words in some vocabulary), this corresponds to a doubling of the vector encoding the document, so again we can use the angle.
英語のヒント
このような例はいたるところにある。テキストでは、同じことを述べる2倍の長さの文書を書いても、論じられている話題は変わらないでほしいと思うかもしれない。ある種の符号化(ある語彙の中の単語の出現回数を数えるなど)では、これは文書を符号化するベクトルを2倍にすることに対応するので、ここでも角度を使える。

22.1.2.1. Cosine Similarity22.1.2.1 コサイン類似度

In ML contexts where the angle is employed to measure the closeness of two vectors, practitioners adopt the term cosine similarity to refer to the portion
英語のヒント
二つのベクトルの近さを測るのに角度を使うMLの文脈では、実践者は次の部分を指すのにコサイン類似度という語を使う。
cos⁡(θ)=𝐯⋅𝐰∥𝐯∥∥𝐰∥.(22.1.9)
The cosine takes a maximum value of 1 when the two vectors point in the same direction, a minimum value of −1 when they point in opposite directions, and a value of 0 when the two vectors are orthogonal. Note that if the components of high-dimensional vectors are sampled randomly with mean 0, their cosine will nearly always be close to 0.
英語のヒント
コサインは、二つのベクトルが同じ方向を向くときに最大値1をとり、反対の方向を向くときに最小値−1をとり、二つのベクトルが直交するときに値0をとる。高次元のベクトルの成分が平均0でランダムに抽出されると、そのコサインはほとんどいつも0に近くなることに注意しよう。