第22章付録:深層学習のための数学
22.1.9. Tensors and Common Linear Algebra Operations22.1.9 テンソルとよくある線形代数の演算
In Section 2.3 the concept of tensors was introduced. In this section, we will dive more deeply into tensor contractions (the tensor equivalent of matrix multiplication), and see how it can provide a unified view on a number of matrix and vector operations.
英語のヒント
2.3節でテンソルの概念を紹介した。この節では、テンソルの縮約(行列の掛け算のテンソル版)にさらに深く入り込み、それがいくつもの行列やベクトルの演算をどう統一的に見る方法を与えるかを見る。
With matrices and vectors we knew how to multiply them to transform data. We need to have a similar definition for tensors if they are to be useful to us. Think about matrix multiplication:
英語のヒント
行列とベクトルについては、データを変換するためにそれらをどう掛けるかを知っていた。テンソルが役に立つためには、テンソルについても同じような定義が必要である。行列の掛け算を考えよう。
(22.1.30)
or equivalently
英語のヒント
あるいは同じことだが、
(22.1.31)
This pattern is one we can repeat for tensors. For tensors, there is no one case of what to sum over that can be universally chosen, so we need specify exactly which indices we want to sum over. For instance we could consider
英語のヒント
このパターンは、テンソルについても繰り返せる。テンソルでは、何について和をとるかを普遍的に選べる一つの場合というものはないので、どの添字について和をとりたいかを正確に指定する必要がある。たとえば次を考えられる。
(22.1.32)
Such a transformation is called a tensor contraction. It can represent a far more flexible family of transformations that matrix multiplication alone.
英語のヒント
このような変換はテンソルの縮約と呼ばれる。これは、行列の掛け算だけよりもずっと柔軟な変換の族を表せる。
As a often-used notational simplification, we can notice that the sum is over exactly those indices that occur more than once in the expression, thus people often work with Einstein notation, where the summation is implicitly taken over all repeated indices. This gives the compact expression:
英語のヒント
よく使われる記法の簡略化として、和は式の中に2回以上現れる添字についてちょうどとられていることに気づける。そこで人々はよくアインシュタインの縮約記法を使う。この記法では、繰り返されるすべての添字について暗黙のうちに和がとられる。これにより、次の簡潔な式が得られる。
(22.1.33)
22.1.9.1. Common Examples from Linear Algebra22.1.9.1 線形代数からのよくある例
Let’s see how many of the linear algebraic definitions we have seen before can be expressed in this compressed tensor notation:
英語のヒント
これまでに見た線形代数の定義のうち、どれだけがこの圧縮されたテンソルの記法で表せるかを見よう。
In this way, we can replace a myriad of specialized notations with short tensor expressions.
英語のヒント
このようにして、無数の専用の記法を短いテンソルの式で置き換えられる。
22.1.9.2. Expressing in Code22.1.9.2 コードで表す
Tensors may flexibly be operated on in code as well. As seen in Section 2.3, we can create tensors as is shown below.
英語のヒント
テンソルはコードでも柔軟に操作できる。2.3節で見たように、テンソルは下に示すように作れる。
# Define tensors
B = torch.tensor([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
A = torch.tensor([[1, 2], [3, 4]])
v = torch.tensor([1, 2])
# Print out the shapes
A.shape, B.shape, v.shape(torch.Size([2, 2]), torch.Size([2, 2, 3]), torch.Size([2]))Einstein summation has been implemented directly. The indices that occurs in the Einstein summation can be passed as a string, followed by the tensors that are being acted upon. For instance, to implement matrix multiplication, we can consider the Einstein summation seen above () and strip out the indices themselves to get the implementation:
英語のヒント
アインシュタインの縮約記法は直接実装されている。アインシュタインの縮約記法に現れる添字を文字列として渡し、そのあとに操作の対象となるテンソルを続ける。たとえば、行列の掛け算を実装するには、上で見たアインシュタインの縮約記法()を考え、添字そのものを取り出せば実装が得られる。
# Reimplement matrix multiplication
torch.einsum("ij, j -> i", A, v), A@v(tensor([ 5, 11]), tensor([ 5, 11]))This is a highly flexible notation. For instance if we want to compute what would be traditionally written as
英語のヒント
これはとても柔軟な記法である。たとえば、従来は次のように書かれるものを計算したいなら、
(22.1.34)
it can be implemented via Einstein summation as:
英語のヒント
アインシュタインの縮約記法で次のように実装できる。
torch.einsum("ijk, il, j -> kl", B, A, v)tensor([[ 90, 126],
[102, 144],
[114, 162]])This notation is readable and efficient for humans, however bulky if for whatever reason we need to generate a tensor contraction programmatically. For this reason,
einsum provides an alternative notation by providing integer indices for each tensor. For example, the same tensor contraction can also be written as:英語のヒント
この記法は人間にとって読みやすく効率がよいが、何らかの理由でテンソルの縮約をプログラムで生成する必要がある場合にはかさばる。このため、
einsumは、各テンソルに整数の添字を与える別の記法を用意している。たとえば、同じテンソルの縮約は次のようにも書ける。# PyTorch does not support this type of notation.Either notation allows for concise and efficient representation of tensor contractions in code.
英語のヒント
どちらの記法でも、コードでテンソルの縮約を簡潔かつ効率よく表せる。