PyTorchの操作方法はNumpyの操作方法と似ています。
そのためNumpyが使用できれば同じような操作方法でPyTrochも扱えるという学習コストの低さが一つのメリットといえます。
しかし、多少の差異はどうしても存在します。
そこで、Numpyの練習に非常に役立つ「100 numpy exercises 」をPyTorchで書き換えることによって、PyTorchの操作方法を学ぶのと同時にNumpyとの類似点や相違点を学んでいきたいと思います。
PyTorchのコードだけでなくNumpyのコードもあわせて紹介していきます。
別記事に他の問題の解法も書いています。
次のリンクにまとめているので、他の問題もあわせて参考にしていただければと思います。
- 11. Create a 3x3 identity matrix (★☆☆)
- 12. Create a 3x3x3 array with random values (★☆☆)
- 13. Create a 10x10 array with random values and find the minimum and maximum values (★☆☆)
- 14. Create a random vector of size 30 and find the mean value (★☆☆)
- 15. Create a 2d array with 1 on the border and 0 inside (★☆☆)
11. Create a 3x3 identity matrix (★☆☆)
「3×3の単位行列を作成してください」
PyTorch
Z = torch.eye(3) print(Z)
# Output tensor([[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]])
Numpy
Z = np.eye(3) print(Z)
# Output [[1. 0. 0.] [0. 1. 0.] [0. 0. 1.]]
12. Create a 3x3x3 array with random values (★☆☆)
「要素がランダムの3×3×3の配列を作成してください」
PyTorch
Z = torch.rand((3, 3, 3)) print(Z)
# Output tensor([[[0.7065, 0.5323, 0.6609], [0.5617, 0.6697, 0.3816], [0.6289, 0.6906, 0.5097]], [[0.8313, 0.9725, 0.4622], [0.7089, 0.7056, 0.1623], [0.1794, 0.6519, 0.8961]], [[0.9205, 0.7204, 0.2701], [0.5994, 0.4827, 0.3271], [0.3080, 0.0663, 0.9939]]])
Numpy
Z = np.random.random((3, 3, 3)) print(Z)
# Output [[[0.17646691 0.93256101 0.29264166] [0.8821208 0.5881321 0.32091339] [0.17575104 0.46484676 0.80618491]] [[0.48507302 0.68746481 0.72514204] [0.08474374 0.33241432 0.16624967] [0.46326199 0.31954387 0.02366121]] [[0.95404531 0.21276611 0.97869308] [0.997538 0.93182844 0.71233085] [0.79715607 0.08070649 0.3871963 ]]]
13. Create a 10x10 array with random values and find the minimum and maximum values (★☆☆)
「ランダムな10x10配列を作成して、最小値と最大値を算出してください」
PyTorch
Z = torch.rand(10, 10) Zmin, Zmax = Z.min(), Z.max() print(Zmin, Zmax)
# Output tensor(0.0035) tensor(0.9904)
Numpy
Z = np.random.random((10, 10)) Zmin, Zmax = Z.min(), Z.max() print(Zmin, Zmax)
# Output 0.0003285892530539014 0.9815009241681587
14. Create a random vector of size 30 and find the mean value (★☆☆)
「サイズが30のランダムなベクトルを作成し、平均値を算出してください」
PyTorch
Z = torch.rand(30) m = Z.mean() print(m)
# Output tensor(0.4685)
Numpy
Z = np.random.random(30) m = Z.mean() print(m)
# Output 0.4884329026946153
15. Create a 2d array with 1 on the border and 0 inside (★☆☆)
「境界が1で内側が0の2次元の配列を作成してください」
PyTorch
Z = torch.ones(10, 10) Z[1:-1, 1:-1] = 0 print(Z)
# Output tensor([[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.], [1., 0., 0., 0., 0., 0., 0., 0., 0., 1.], [1., 0., 0., 0., 0., 0., 0., 0., 0., 1.], [1., 0., 0., 0., 0., 0., 0., 0., 0., 1.], [1., 0., 0., 0., 0., 0., 0., 0., 0., 1.], [1., 0., 0., 0., 0., 0., 0., 0., 0., 1.], [1., 0., 0., 0., 0., 0., 0., 0., 0., 1.], [1., 0., 0., 0., 0., 0., 0., 0., 0., 1.], [1., 0., 0., 0., 0., 0., 0., 0., 0., 1.], [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]])
Numpy
Z = np.ones((10, 10)) Z[1:-1,1:-1] = 0 print(Z)
# Output [[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.] [1. 0. 0. 0. 0. 0. 0. 0. 0. 1.] [1. 0. 0. 0. 0. 0. 0. 0. 0. 1.] [1. 0. 0. 0. 0. 0. 0. 0. 0. 1.] [1. 0. 0. 0. 0. 0. 0. 0. 0. 1.] [1. 0. 0. 0. 0. 0. 0. 0. 0. 1.] [1. 0. 0. 0. 0. 0. 0. 0. 0. 1.] [1. 0. 0. 0. 0. 0. 0. 0. 0. 1.] [1. 0. 0. 0. 0. 0. 0. 0. 0. 1.] [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]]
リンク