第2章準備
2.3.5. Basic Properties of Tensor Arithmetic2.3.5 テンソル演算の基本的な性質
Scalars, vectors, matrices, and higher-order tensors all have some handy properties. For example, elementwise operations produce outputs that have the same shape as their operands.
英語のヒント
スカラー、ベクトル、行列、高い階のテンソルは、どれも便利な性質をもつ。たとえば、要素ごとの演算は、その被演算子と同じ形状の出力を出す。
A = torch.arange(6, dtype=torch.float32).reshape(2, 3)
B = A.clone() # Assign a copy of A to B by allocating new memory
A, A + B(tensor([[0., 1., 2.],
[3., 4., 5.]]),
tensor([[ 0., 2., 4.],
[ 6., 8., 10.]]))The elementwise product of two matrices is called their Hadamard product (denoted ). We can spell out the entries of the Hadamard product of two matrices :
英語のヒント
二つの行列の要素ごとの積を、アダマール積(で表す)という。二つの行列のアダマール積の成分を書き出すと次のようになる。
(2.3.4)
A * Btensor([[ 0., 1., 4.],
[ 9., 16., 25.]])Adding or multiplying a scalar and a tensor produces a result with the same shape as the original tensor. Here, each element of the tensor is added to (or multiplied by) the scalar.
英語のヒント
スカラーとテンソルを足したり掛けたりすると、元のテンソルと同じ形状の結果が得られる。ここでは、テンソルの各要素にスカラーが足される(または掛けられる)。
a = 2
X = torch.arange(24).reshape(2, 3, 4)
a + X, (a * X).shape(tensor([[[ 2, 3, 4, 5],
[ 6, 7, 8, 9],
[10, 11, 12, 13]],
[[14, 15, 16, 17],
[18, 19, 20, 21],
[22, 23, 24, 25]]]),
torch.Size([2, 3, 4]))