PyTorchの操作方法はNumpyの操作方法と似ています。
そのためNumpyが使用できれば同じような操作方法でPyTrochも扱えるという学習コストの低さが一つのメリットといえます。
しかし、多少の差異はどうしても存在します。
そこで、Numpyの練習に非常に役立つ「100 numpy exercises 」をPyTorchで書き換えることによって、PyTorchの操作方法を学ぶのと同時にNumpyとの類似点や相違点を学んでいきたいと思います。
PyTorchのコードだけでなくNumpyのコードもあわせて紹介していきます。
別記事に他の問題の解法も書いています。
次のリンクにまとめているので、他の問題もあわせて参考にしていただければと思います。
- 46. Create a structured array with x and y coordinates covering the [0,1]x[0,1] area (★★☆)
- 47. Given two arrays, X and Y, construct the Cauchy matrix C (Cij =1/(xi - yj)) (★★☆)
- 48. Print the minimum and maximum representable value for each numpy scalar type (★★☆)
- 49. How to print all the values of an array? (★★☆)
- 50. How to find the closest value (to a given scalar) in a vector? (★★☆)
46. Create a structured array with x and y coordinates covering the [0,1]x[0,1] area (★★☆)
「[0, 1] × [1, 0] 領域をカバーするx座標とy座標を持つ構造化配列を作成してください」
PyTorch
PyTorchでの方法を見つけることができませんでした。
Numpy
Z = np.zeros((5,5), [('x',float),('y',float)]) Z['x'], Z['y'] = np.meshgrid(np.linspace(0,1,5), np.linspace(0,1,5)) print(Z)
# Output [[(0. , 0. ) (0.25, 0. ) (0.5 , 0. ) (0.75, 0. ) (1. , 0. )] [(0. , 0.25) (0.25, 0.25) (0.5 , 0.25) (0.75, 0.25) (1. , 0.25)] [(0. , 0.5 ) (0.25, 0.5 ) (0.5 , 0.5 ) (0.75, 0.5 ) (1. , 0.5 )] [(0. , 0.75) (0.25, 0.75) (0.5 , 0.75) (0.75, 0.75) (1. , 0.75)] [(0. , 1. ) (0.25, 1. ) (0.5 , 1. ) (0.75, 1. ) (1. , 1. )]]
47. Given two arrays, X and Y, construct the Cauchy matrix C (Cij =1/(xi - yj)) (★★☆)
「XとYの二つの配列が与えられた場合、コーシー行列Cを作成してください (Cij =1/(xi - yj))」
PyTorch
X = torch.arange(8) Y = X + torch.tensor(0.5) C = torch.tensor(1.0) / (X.view(-1, 1) - Y) print(torch.linalg.det(C))
# Output tensor(3638.1643)
Numpy
X = np.arange(8) Y = X + 0.5 C = 1.0 / np.subtract.outer(X, Y) print(np.linalg.det(C))
# Output 3638.1636371179666
48. Print the minimum and maximum representable value for each numpy scalar type (★★☆)
「各Numpyのスカラータイプの表現可能な最小値と最大値を出力してください」
PyTorchの場合は、PyTorchの最小値と最大値を出力します。
PyTorch
for dtype in [torch.int8, torch.int32, torch.int64]: print(torch.iinfo(dtype).min) print(torch.iinfo(dtype).max) for dtype in [torch.float32, torch.float64]: print(torch.finfo(dtype).min) print(torch.finfo(dtype).max) print(torch.finfo(dtype).eps)
# Output -128 127 -2147483648 2147483647 -9223372036854775808 9223372036854775807 -3.4028234663852886e+38 3.4028234663852886e+38 1.1920928955078125e-07 -1.7976931348623157e+308 1.7976931348623157e+308 2.220446049250313e-16
Numpy
for dtype in [np.int8, np.int32, np.int64]: print(np.iinfo(dtype).min) print(np.iinfo(dtype).max) for dtype in [np.float32, np.float64]: print(np.finfo(dtype).min) print(np.finfo(dtype).max) print(np.finfo(dtype).eps)
# Output -128 127 -2147483648 2147483647 -9223372036854775808 9223372036854775807 -3.4028235e+38 3.4028235e+38 1.1920929e-07 -1.7976931348623157e+308 1.7976931348623157e+308 2.220446049250313e-16
49. How to print all the values of an array? (★★☆)
「配列のすべての値を出力してください」
PyTorch
torch.set_printoptions(threshold=float('inf')) Z = torch.zeros((40, 40)) print(Z)
# Output 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., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], # ~~ 以下省略 ~~
Numpy
np.set_printoptions(threshold=float("inf")) Z = np.zeros((40,40)) print(Z)
# Output [[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] # ~~ 以下省略 ~~
50. How to find the closest value (to a given scalar) in a vector? (★★☆)
「ベクトル内で(特定のスカラー値に)最も近い値を出力してください」
PyTorch
Z = torch.arange(100) v = torch.distributions.uniform.Uniform(0, 100).sample([1]) index = (torch.abs(Z - v)).argmin() print(Z[index])
# Output tensor(62)
Numpy
Z = np.arange(100) v = np.random.uniform(0, 100) index = (np.abs(Z - v)).argmin() print(Z[index])
# Output 98
リンク