第2章準備
2.1.1. Getting Started2.1.1 はじめに
To start, we import the PyTorch library. Note that the package name is
torch.英語のヒント
まず、PyTorchのライブラリをインポートする。パッケージ名は
torchであることに注意してほしい。import torchA tensor represents a (possibly multidimensional) array of numerical values. In the one-dimensional case, i.e., when only one axis is needed for the data, a tensor is called a vector. With two axes, a tensor is called a matrix. With axes, we drop the specialized names and just refer to the object as a -order tensor.
英語のヒント
テンソルは、数値の(場合によっては多次元の)配列を表す。1次元の場合、つまりデータに軸が一つだけ必要なとき、テンソルはベクトルと呼ばれる。軸が二つなら、テンソルは行列と呼ばれる。軸が個なら、特別な名前はやめて、単に階のテンソルと呼ぶ。
PyTorch provides a variety of functions for creating new tensors prepopulated with values. For example, by invoking
arange(n), we can create a vector of evenly spaced values, starting at 0 (included) and ending at n (not included). By default, the interval size is . Unless otherwise specified, new tensors are stored in main memory and designated for CPU-based computation.英語のヒント
PyTorchには、値をあらかじめ詰めた新しいテンソルを作る関数がいろいろある。たとえば
arange(n)を呼び出すと、0(含む)から始まりn(含まない)で終わる、等間隔の値のベクトルを作れる。既定では、間隔の大きさはである。特に指定しないかぎり、新しいテンソルはメインメモリに保存され、CPUでの計算に割り当てられる。x = torch.arange(12, dtype=torch.float32)
xtensor([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11.])Each of these values is called an element of the tensor. The tensor
x contains 12 elements. We can inspect the total number of elements in a tensor via its numel method.英語のヒント
これらの値の一つひとつを、テンソルの要素という。テンソル
xは12個の要素をもつ。テンソルの要素の総数は、そのnumelメソッドで調べられる。x.numel()12We can access a tensor’s shape (the length along each axis) by inspecting its
shape attribute. Because we are dealing with a vector here, the shape contains just a single element and is identical to the size.英語のヒント
テンソルの形状(各軸に沿った長さ)は、その
shape属性を調べれば得られる。ここで扱っているのはベクトルなので、shapeは要素を一つだけ含み、大きさと同じである。x.shapetorch.Size([12])We can change the shape of a tensor without altering its size or values, by invoking
reshape. For example, we can transform our vector x whose shape is (12,) to a matrix X with shape (3, 4). This new tensor retains all elements but reconfigures them into a matrix. Notice that the elements of our vector are laid out one row at a time and thus x[3] == X[0, 3].英語のヒント
reshapeを呼び出せば、テンソルの大きさや値を変えずに形状を変えられる。たとえば、形状が(12,)のベクトルxを、形状が(3, 4)の行列Xに変換できる。この新しいテンソルはすべての要素を保ったまま、それを行列に組み直す。ベクトルの要素は1行ずつ並べられるので、x[3] == X[0, 3]となることに注意してほしい。X = x.reshape(3, 4)
Xtensor([[ 0., 1., 2., 3.],
[ 4., 5., 6., 7.],
[ 8., 9., 10., 11.]])Note that specifying every shape component to
reshape is redundant. Because we already know our tensor’s size, we can work out one component of the shape given the rest. For example, given a tensor of size and target shape (, ), we know that . To automatically infer one component of the shape, we can place a -1 for the shape component that should be inferred automatically. In our case, instead of calling x.reshape(3, 4), we could have equivalently called x.reshape(-1, 4) or x.reshape(3, -1).英語のヒント
reshapeに形状の成分をすべて指定するのは冗長であることに注意してほしい。テンソルの大きさはすでにわかっているので、残りがわかれば形状の成分の一つは割り出せる。たとえば、大きさのテンソルと目標の形状(, )が与えられれば、であることがわかる。形状の成分の一つを自動で推定させるには、自動で推定させたい成分に-1を置けばよい。この例では、x.reshape(3, 4)を呼び出す代わりに、同じことをx.reshape(-1, 4)やx.reshape(3, -1)で呼び出すこともできた。Practitioners often need to work with tensors initialized to contain all 0s or 1s. We can construct a tensor with all elements set to 0 and a shape of (2, 3, 4) via the
zeros function.英語のヒント
実践では、すべて0や1を含むように初期化したテンソルを扱う必要がよくある。
zeros関数を使えば、すべての要素が0で形状が(2, 3, 4)のテンソルを作れる。torch.zeros((2, 3, 4))tensor([[[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]],
[[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]]])Similarly, we can create a tensor with all 1s by invoking
ones.英語のヒント
同じように、
onesを呼び出せば、すべて1のテンソルを作れる。torch.ones((2, 3, 4))tensor([[[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]],
[[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]]])We often wish to sample each element randomly (and independently) from a given probability distribution. For example, the parameters of neural networks are often initialized randomly. The following snippet creates a tensor with elements drawn from a standard Gaussian (normal) distribution with mean 0 and standard deviation 1.
英語のヒント
各要素を、ある確率分布から無作為に(そして独立に)サンプリングしたいこともよくある。たとえば、ニューラルネットワークのパラメータは、多くの場合無作為に初期化される。次のコードの断片は、平均0、標準偏差1の標準ガウス(正規)分布から要素を取り出したテンソルを作る。
torch.randn(3, 4)tensor([[ 0.1351, -0.9099, -0.2028, 2.1937],
[-0.3200, -0.7545, 0.8086, -1.8730],
[ 0.3929, 0.4931, 0.9114, -0.7072]])Finally, we can construct tensors by supplying the exact values for each element by supplying (possibly nested) Python list(s) containing numerical literals. Here, we construct a matrix with a list of lists, where the outermost list corresponds to axis 0, and the inner list corresponds to axis 1.
英語のヒント
最後に、数値リテラルを含む(場合によっては入れ子の)Pythonのリストを与えて、各要素の正確な値を指定することでテンソルを作ることもできる。ここでは、リストのリストで行列を作る。いちばん外側のリストが軸0に、内側のリストが軸1に対応する。
torch.tensor([[2, 1, 4, 3], [1, 2, 3, 4], [4, 3, 2, 1]])tensor([[2, 1, 4, 3],
[1, 2, 3, 4],
[4, 3, 2, 1]])