第2章準備
2.1.3. Operations2.1.3 演算
Now that we know how to construct tensors and how to read from and write to their elements, we can begin to manipulate them with various mathematical operations. Among the most useful of these are the elementwise operations. These apply a standard scalar operation to each element of a tensor. For functions that take two tensors as inputs, elementwise operations apply some standard binary operator on each pair of corresponding elements. We can create an elementwise function from any function that maps from a scalar to a scalar.
英語のヒント
テンソルの作り方と、その要素の読み書きのしかたがわかったので、さまざまな数学的演算で操作し始められる。そのなかでも特に役立つのが、要素ごとの演算である。これは、テンソルの各要素に標準的なスカラー演算を適用する。二つのテンソルを入力にとる関数では、要素ごとの演算は、対応する要素の各組に何らかの標準的な二項演算子を適用する。スカラーからスカラーへ写す関数なら、どれからでも要素ごとの関数を作れる。
In mathematical notation, we denote such unary scalar operators (taking one input) by the signature . This just means that the function maps from any real number onto some other real number. Most standard operators, including unary ones like , can be applied elementwise.
英語のヒント
数学の記法では、こうした単項のスカラー演算子(入力を一つとる)を、シグネチャで表す。これは単に、関数が任意の実数をほかの何らかの実数に写すという意味である。のような単項演算子を含め、標準的な演算子のほとんどは要素ごとに適用できる。
torch.exp(x)tensor([162754.7969, 162754.7969, 162754.7969, 162754.7969, 162754.7969,
162754.7969, 162754.7969, 162754.7969, 2980.9580, 8103.0840,
22026.4648, 59874.1406])Likewise, we denote binary scalar operators, which map pairs of real numbers to a (single) real number via the signature . Given any two vectors and of the same shape, and a binary operator , we can produce a vector by setting for all , where , and are the elements of vectors , and . Here, we produced the vector-valued by lifting the scalar function to an elementwise vector operation. The common standard arithmetic operators for addition (
+), subtraction (-), multiplication (*), division (/), and exponentiation (**) have all been lifted to elementwise operations for identically-shaped tensors of arbitrary shape.英語のヒント
同じように、実数の組を(一つの)実数に写す二項のスカラー演算子は、シグネチャで表す。同じ形状の任意の二つのベクトルと、そして二項演算子が与えられれば、すべてのについてとすることで、ベクトルを作れる。ここで、は、ベクトル、の番目の要素である。ここでは、スカラー関数を要素ごとのベクトル演算に持ち上げることで、ベクトル値のを作った。加算(
+)、減算(-)、乗算(*)、除算(/)、累乗(**)の一般的な算術演算子はすべて、任意の形状をもつ同じ形状のテンソルどうしの要素ごとの演算に持ち上げられている。x = torch.tensor([1.0, 2, 4, 8])
y = torch.tensor([2, 2, 2, 2])
x + y, x - y, x * y, x / y, x ** y(tensor([ 3., 4., 6., 10.]),
tensor([-1., 0., 2., 6.]),
tensor([ 2., 4., 8., 16.]),
tensor([0.5000, 1.0000, 2.0000, 4.0000]),
tensor([ 1., 4., 16., 64.]))In addition to elementwise computations, we can also perform linear algebraic operations, such as dot products and matrix multiplications. We will elaborate on these in Section 2.3.
英語のヒント
要素ごとの計算のほかに、内積や行列の積といった線形代数の演算も行える。これらについては2.3節で詳しく述べる。
We can also concatenate multiple tensors, stacking them end-to-end to form a larger one. We just need to provide a list of tensors and tell the system along which axis to concatenate. The example below shows what happens when we concatenate two matrices along rows (axis 0) instead of columns (axis 1). We can see that the first output’s axis-0 length () is the sum of the two input tensors’ axis-0 lengths (); while the second output’s axis-1 length () is the sum of the two input tensors’ axis-1 lengths ().
英語のヒント
複数のテンソルを連結し、端と端をつなげて積み重ね、より大きなテンソルにすることもできる。テンソルのリストを与え、どの軸に沿って連結するかをシステムに伝えればよい。下の例は、二つの行列を列(軸1)ではなく行(軸0)に沿って連結したときに何が起きるかを示す。一つ目の出力の軸0の長さ()は、二つの入力テンソルの軸0の長さの和()であり、二つ目の出力の軸1の長さ()は、二つの入力テンソルの軸1の長さの和()であることがわかる。
X = torch.arange(12, dtype=torch.float32).reshape((3,4))
Y = torch.tensor([[2.0, 1, 4, 3], [1, 2, 3, 4], [4, 3, 2, 1]])
torch.cat((X, Y), dim=0), torch.cat((X, Y), dim=1)(tensor([[ 0., 1., 2., 3.],
[ 4., 5., 6., 7.],
[ 8., 9., 10., 11.],
[ 2., 1., 4., 3.],
[ 1., 2., 3., 4.],
[ 4., 3., 2., 1.]]),
tensor([[ 0., 1., 2., 3., 2., 1., 4., 3.],
[ 4., 5., 6., 7., 1., 2., 3., 4.],
[ 8., 9., 10., 11., 4., 3., 2., 1.]]))Sometimes, we want to construct a binary tensor via logical statements. Take
X == Y as an example. For each position i, j, if X[i, j] and Y[i, j] are equal, then the corresponding entry in the result takes value 1, otherwise it takes value 0.英語のヒント
論理的な文で二値のテンソルを作りたいこともある。
X == Yを例にとろう。各位置i, jについて、X[i, j]とY[i, j]が等しければ、結果の対応する成分は値1をとり、そうでなければ値0をとる。X == Ytensor([[False, True, False, True],
[False, False, False, False],
[False, False, False, False]])Summing all the elements in the tensor yields a tensor with only one element.
英語のヒント
テンソルのすべての要素を合計すると、要素が一つだけのテンソルが得られる。
X.sum()tensor(66.)