第22章付録:深層学習のための数学
22.1.3. Hyperplanes22.1.3 超平面
In addition to working with vectors, another key object that you must understand to go far in linear algebra is the hyperplane, a generalization to higher dimensions of a line (two dimensions) or of a plane (three dimensions). In an -dimensional vector space, a hyperplane has dimensions and divides the space into two half-spaces.
英語のヒント
ベクトルを扱うことに加えて、線形代数で先へ進むために理解しなければならないもう一つの鍵となる対象が超平面である。超平面は、直線(2次元)や平面(3次元)をより高い次元に一般化したものである。次元のベクトル空間では、超平面は次元をもち、空間を二つの半空間に分ける。
Let’s start with an example. Suppose that we have a column vector . We want to know, “what are the points with ?” By recalling the connection between dot products and angles above (22.1.8), we can see that this is equivalent to
英語のヒント
例から始めよう。列ベクトルがあるとする。「を満たす点はどれか」を知りたい。上で見た内積と角度の関係(式(22.1.8))を思い出すと、これは次と同値であることがわかる。
(22.1.10)
Fig. 22.1.5図22.1.5
Recalling trigonometry, we see the formula is the length of the projection of the vector onto the direction of
英語のヒント
三角法を思い出すと、式はベクトルをの方向に射影した長さであることがわかる。
If we consider the geometric meaning of this expression, we see that this is equivalent to saying that the length of the projection of onto the direction of is exactly , as is shown in Fig. 22.1.5. The set of all points where this is true is a line at right angles to the vector . If we wanted, we could find the equation for this line and see that it is or equivalently .
英語のヒント
この式の幾何学的な意味を考えると、これは、図22.1.5に示すように、をの方向に射影した長さがちょうどであると言うことと同値であることがわかる。これが成り立つすべての点の集合は、ベクトルに直角な直線である。その気になれば、この直線の方程式を求めて、それが、あるいは同じことだがであることを確かめられる。
If we now look at what happens when we ask about the set of points with or , we can see that these are cases where the projections are longer or shorter than , respectively. Thus, those two inequalities define either side of the line. In this way, we have found a way to cut our space into two halves, where all the points on one side have dot product below a threshold, and the other side above as we see in Fig. 22.1.6.
英語のヒント
では、やを満たす点の集合について問うと何が起きるかを見ると、これらはそれぞれ射影がより長い場合と短い場合であることがわかる。したがって、この二つの不等式は直線のどちらかの側を定める。このようにして、図22.1.6で見るように、空間を二つの半分に切り分ける方法が見つかった。一方の側のすべての点は内積があるしきい値より小さく、もう一方の側は大きい。
Fig. 22.1.6図22.1.6
If we now consider the inequality version of the expression, we see that our hyperplane (in this case: just a line) separates the space into two halves.
英語のヒント
式の不等式版を考えると、超平面(この場合はただの直線)が空間を二つの半分に分けることがわかる。
The story in higher dimension is much the same. If we now take and ask about the points in three dimensions with , we obtain a plane at right angles to the given vector . The two inequalities again define the two sides of the plane as is shown in Fig. 22.1.7.
英語のヒント
より高い次元でも話はほとんど同じである。今度はをとって、3次元でを満たす点について問うと、与えられたベクトルに直角な平面が得られる。図22.1.7に示すように、二つの不等式はここでも平面の二つの側を定める。
Fig. 22.1.7図22.1.7
Hyperplanes in any dimension separate the space into two halves.
英語のヒント
どんな次元の超平面も、空間を二つの半分に分ける。
While our ability to visualize runs out at this point, nothing stops us from doing this in tens, hundreds, or billions of dimensions. This occurs often when thinking about machine learned models. For instance, we can understand linear classification models like those from Section 4.1, as methods to find hyperplanes that separate the different target classes. In this context, such hyperplanes are often referred to as decision planes. The majority of deep learned classification models end with a linear layer fed into a softmax, so one can interpret the role of the deep neural network to be to find a non-linear embedding such that the target classes can be separated cleanly by hyperplanes.
英語のヒント
視覚化する能力はここで尽きるが、これを数十次元、数百次元、数十億次元で行うのを妨げるものは何もない。機械学習のモデルについて考えるときには、こうしたことがよく起きる。たとえば、4.1節のような線形分類モデルは、異なる目標クラスを分ける超平面を見つける方法として理解できる。この文脈では、こうした超平面はしばしば決定平面と呼ばれる。深層学習の分類モデルの大部分は、ソフトマックスに入力される線形層で終わるので、深層ニューラルネットワークの役割は、目標クラスが超平面できれいに分けられるような非線形の埋め込みを見つけることだと解釈できる。
To give a hand-built example, notice that we can produce a reasonable model to classify tiny images of t-shirts and trousers from the Fashion-MNIST dataset (seen in Section 4.2) by just taking the vector between their means to define the decision plane and eyeball a crude threshold. First we will load the data and compute the averages.
英語のヒント
手作りの例を挙げよう。(4.2節で見た)Fashion-MNISTのデータセットのTシャツとズボンの小さな画像を分類するまずまずのモデルは、両者の平均の間のベクトルを決定平面を定めるのに使い、おおざっぱなしきい値を目分量で決めるだけで作れることに注意しよう。まず、データを読み込んで平均を計算する。
# Load in the dataset
trans = []
trans.append(transforms.ToTensor())
trans = transforms.Compose(trans)
train = torchvision.datasets.FashionMNIST(root="../data", transform=trans,
train=True, download=True)
test = torchvision.datasets.FashionMNIST(root="../data", transform=trans,
train=False, download=True)
X_train_0 = torch.stack(
[x[0] * 256 for x in train if x[1] == 0]).type(torch.float32)
X_train_1 = torch.stack(
[x[0] * 256 for x in train if x[1] == 1]).type(torch.float32)
X_test = torch.stack(
[x[0] * 256 for x in test if x[1] == 0 or x[1] == 1]).type(torch.float32)
y_test = torch.stack([torch.tensor(x[1]) for x in test
if x[1] == 0 or x[1] == 1]).type(torch.float32)
# Compute averages
ave_0 = torch.mean(X_train_0, axis=0)
ave_1 = torch.mean(X_train_1, axis=0)It can be informative to examine these averages in detail, so let’s plot what they look like. In this case, we see that the average indeed resembles a blurry image of a t-shirt.
英語のヒント
これらの平均を詳しく調べると多くのことがわかるので、どのように見えるかをプロットしよう。この場合、平均はたしかにTシャツのぼやけた画像に似ている。
# Plot average t-shirt
d2l.set_figsize()
d2l.plt.imshow(ave_0.reshape(28, 28).tolist(), cmap='Greys')
d2l.plt.show()In the second case, we again see that the average resembles a blurry image of trousers.
英語のヒント
二つ目の場合でも、平均はズボンのぼやけた画像に似ている。
# Plot average trousers
d2l.plt.imshow(ave_1.reshape(28, 28).tolist(), cmap='Greys')
d2l.plt.show()In a fully machine learned solution, we would learn the threshold from the dataset. In this case, I simply eyeballed a threshold that looked good on the training data by hand.
英語のヒント
完全に機械学習による解決策では、しきい値をデータセットから学習するだろう。ここでは単純に、訓練データでよさそうに見えるしきい値を手で目分量で決めた。
# Print test set accuracy with eyeballed threshold
w = (ave_1 - ave_0).T
# '@' is Matrix Multiplication operator in pytorch.
predictions = X_test.reshape(2000, -1) @ (w.flatten()) > -1500000
# Accuracy
torch.mean((predictions.type(y_test.dtype) == y_test).float(), dtype=torch.float64)tensor(0.7870, dtype=torch.float64)