PyTorchの操作方法はNumpyの操作方法と似ています。
そのためNumpyが使用できれば同じような操作方法でPyTrochも扱えるという学習コストの低さが一つのメリットといえます。
しかし、多少の差異はどうしても存在します。
そこで、Numpyの練習に非常に役立つ「100 numpy exercises 」をPyTorchで書き換えることによって、PyTorchの操作方法を学ぶのと同時にNumpyとの類似点や相違点を学んでいきたいと思います。
PyTorchのコードだけでなくNumpyのコードもあわせて紹介していきます。
別記事に他の問題の解法も書いています。
次のリンクにまとめているので、他の問題もあわせて参考にしていただければと思います。
- 51. Create a structured array representing a position (x,y) and a color (r,g,b) (★★☆)
- 52. Consider a random vector with shape (100,2) representing coordinates, find point by point distances (★★☆)
- 53. How to convert a float (32 bits) array into an integer (32 bits) in place?
- 54. How to read the following file? (★★☆)
- 55. What is the equivalent of enumerate for numpy arrays? (★★☆)
51. Create a structured array representing a position (x,y) and a color (r,g,b) (★★☆)
「位置(x, y)と色(r, g, b)を表す構造化配列を作成してください」
PyTorch
PyTorchでの方法を見つけることができませんでした。
Numpy
Z = np.zeros(10, [('position', [('x', float, 1), ('y', float, 1)]), ('color', [('r', float, 1), ('g', float, 1), ('b', float, 1)])]) 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.))]
52. Consider a random vector with shape (100,2) representing coordinates, find point by point distances (★★☆)
「座標を表す形状(100, 2)のランダムなベクトルを作成し、各点との距離を算出してください」
PyTorch
Z = torch.rand((10, 2)) X, Y = torch.atleast_2d(Z[:, 0], Z[:, 1]) D = torch.sqrt((X - X.T) ** 2 + (Y - Y.T) ** 2) print(D)
# Output tensor([[0.0000, 0.4146, 0.8186, 0.5376, 0.9352, 0.8422, 0.7176, 0.6257, 0.4561, 0.3302], [0.4146, 0.0000, 0.6842, 0.1398, 0.5247, 0.4288, 0.4577, 0.2890, 0.1968, 0.3318], [0.8186, 0.6842, 0.0000, 0.7632, 0.9170, 0.8256, 0.2839, 0.9300, 0.8809, 0.9726], [0.5376, 0.1398, 0.7632, 0.0000, 0.3984, 0.3104, 0.5058, 0.1689, 0.1711, 0.3738], [0.9352, 0.5247, 0.9170, 0.3984, 0.0000, 0.1041, 0.6349, 0.3779, 0.5277, 0.7413], [0.8422, 0.4288, 0.8256, 0.3104, 0.1041, 0.0000, 0.5420, 0.3261, 0.4558, 0.6701], [0.7176, 0.4577, 0.2839, 0.5058, 0.6349, 0.5420, 0.0000, 0.6663, 0.6463, 0.7796], [0.6257, 0.2890, 0.9300, 0.1689, 0.3779, 0.3261, 0.6663, 0.0000, 0.1722, 0.3732], [0.4561, 0.1968, 0.8809, 0.1711, 0.5277, 0.4558, 0.6463, 0.1722, 0.0000, 0.2145], [0.3302, 0.3318, 0.9726, 0.3738, 0.7413, 0.6701, 0.7796, 0.3732, 0.2145, 0.0000]])
Numpy
Z = np.random.random((10, 2)) X, Y = np.atleast_2d(Z[:,0], Z[:,1]) D = np.sqrt((X - X.T) ** 2 + (Y - Y.T) ** 2) print(D)
# Output [[0. 0.157812 0.76482063 0.48144225 0.71621426 0.37904617 0.83818064 0.9367339 0.42888455 0.67589251] [0.157812 0. 0.69751426 0.32498747 0.63316806 0.41181355 0.71705963 0.80295543 0.27583226 0.61693084] [0.76482063 0.69751426 0. 0.71464555 0.09775709 0.48241188 0.32759002 0.46932255 0.57454418 0.09399059] [0.48144225 0.32498747 0.71464555 0. 0.62313257 0.64633863 0.58204586 0.61268686 0.14735392 0.6659362 ] [0.71621426 0.63316806 0.09775709 0.62313257 0. 0.47695454 0.25280928 0.3979098 0.48625724 0.11632671] [0.37904617 0.41181355 0.48241188 0.64633863 0.47695454 0. 0.68891831 0.82342245 0.51658112 0.3891275 ] [0.83818064 0.71705963 0.32759002 0.58204586 0.25280928 0.68891831 0. 0.14518608 0.48476056 0.36890033] [0.9367339 0.80295543 0.46932255 0.61268686 0.3979098 0.82342245 0.14518608 0. 0.54568269 0.51406791] [0.42888455 0.27583226 0.57454418 0.14735392 0.48625724 0.51658112 0.48476056 0.54568269 0. 0.52113869] [0.67589251 0.61693084 0.09399059 0.6659362 0.11632671 0.3891275 0.36890033 0.51406791 0.52113869 0. ]]
53. How to convert a float (32 bits) array into an integer (32 bits) in place?
「浮動小数点(32ビット)配列を整数(32ビット)に変換してください」
PyTorch
Z = (torch.rand(10) * 100).to(torch.float32) Y = Z.to(torch.int32) Y[:] = Z print(Y)
# Output tensor([45, 88, 76, 1, 42, 99, 3, 29, 48, 22], dtype=torch.int32)
Numpy
Z = (np.random.rand(10)*100).astype(np.float32) Y = Z.view(np.int32) Y[:] = Z print(Y)
# Output [45 9 61 2 89 93 37 7 35 10]
54. How to read the following file? (★★☆)
「ファイルを読み込んでください」
PyTorch
読み込み関数はNumpy関数を使用しています。
from io import StringIO s = StringIO('''1, 2, 3, 4, 5 6, , , 7, 8 , , 9,10,11 ''') Z = np.genfromtxt(s, delimiter=",", dtype=np.int) Z = torch.tensor(Z) print(Z)
# Output tensor([[ 1, 2, 3, 4, 5], [ 6, -1, -1, 7, 8], [-1, -1, 9, 10, 11]], dtype=torch.int32)
Numpy
from io import StringIO s = StringIO('''1, 2, 3, 4, 5 6, , , 7, 8 , , 9,10,11 ''') Z = np.genfromtxt(s, delimiter=",", dtype=np.int) print(Z)
# Output [[ 1 2 3 4 5] [ 6 -1 -1 7 8] [-1 -1 9 10 11]]
55. What is the equivalent of enumerate for numpy arrays? (★★☆)
「Numpy配列に対してenumurateに相当するものは何でしょうか」
PyTorchの場合は、PyTorchのenumurateに相当するものを使用します。
PyTorch
PyTorchでenumurateに相当するものを見つけることができませんでしたので、np.ndenumerate関数
とnp.ndindex関数
を使用しています。
Z = torch.arange(9).view(3, 3) for index, value in np.ndenumerate(Z): print(index, value) for index in np.ndindex(Z.size()): print(index, Z[index])
# Output (0, 0) 0 (0, 1) 1 (0, 2) 2 (1, 0) 3 (1, 1) 4 (1, 2) 5 (2, 0) 6 (2, 1) 7 (2, 2) 8 (0, 0) tensor(0) (0, 1) tensor(1) (0, 2) tensor(2) (1, 0) tensor(3) (1, 1) tensor(4) (1, 2) tensor(5) (2, 0) tensor(6) (2, 1) tensor(7) (2, 2) tensor(8)
Numpy
Z = np.arange(9).reshape(3, 3) for index, value in np.ndenumerate(Z): print(index, value) for index in np.ndindex(Z.shape): print(index, Z[index])
# Output (0, 0) 0 (0, 1) 1 (0, 2) 2 (1, 0) 3 (1, 1) 4 (1, 2) 5 (2, 0) 6 (2, 1) 7 (2, 2) 8
リンク