PyTorchの操作方法はNumpyの操作方法と似ています。
そのためNumpyが使用できれば同じような操作方法でPyTrochも扱えるという学習コストの低さが一つのメリットといえます。
しかし、多少の差異はどうしても存在します。
そこで、Numpyの練習に非常に役立つ「100 numpy exercises 」をPyTorchで書き換えることによって、PyTorchの操作方法を学ぶのと同時にNumpyとの類似点や相違点を学んでいきたいと思います。
PyTorchのコードだけでなくNumpyのコードもあわせて紹介していきます。
別記事に他の問題の解法も書いています。
次のリンクにまとめているので、他の問題もあわせて参考にしていただければと思います。
- 36. Extract the integer part of a random array of positive numbers using 4 different methods (★★☆)
- 37. Create a 5x5 matrix with row values ranging from 0 to 4 (★★☆)
- 38. Consider a generator function that generates 10 integers and use it to build an array (★☆☆)
- 39. Create a vector of size 10 with values ranging from 0 to 1, both excluded (★★☆)
- 40. Create a random vector of size 10 and sort it (★★☆)
36. Extract the integer part of a random array of positive numbers using 4 different methods (★★☆)
「4つの異なる方法でランダムな正の配列の整数部分を取得してください」
PyTorch
Z = torch.distributions.uniform.Uniform(0, 10).sample([10]) print(Z - Z % 1) print(Z // 1) print(torch.floor(Z)) print(Z.to(torch.int32)) print(torch.trunc(Z))
# Output tensor([0., 4., 9., 3., 1., 0., 7., 9., 1., 1.]) tensor([0., 4., 9., 3., 1., 0., 7., 9., 1., 1.]) tensor([0., 4., 9., 3., 1., 0., 7., 9., 1., 1.]) tensor([0, 4, 9, 3, 1, 0, 7, 9, 1, 1], dtype=torch.int32) tensor([0., 4., 9., 3., 1., 0., 7., 9., 1., 1.])
Numpy
Z = np.random.uniform(0,10,10) print(Z - Z%1) print(Z // 1) print(np.floor(Z)) print(Z.astype(int)) print(np.trunc(Z))
# Output [6. 7. 8. 8. 4. 4. 3. 4. 3. 0.] [6. 7. 8. 8. 4. 4. 3. 4. 3. 0.] [6. 7. 8. 8. 4. 4. 3. 4. 3. 0.] [6 7 8 8 4 4 3 4 3 0] [6. 7. 8. 8. 4. 4. 3. 4. 3. 0.]
37. Create a 5x5 matrix with row values ranging from 0 to 4 (★★☆)
「要素が0から4の値をもつ5×5の行列を作成してください」
PyTorch
Z = torch.zeros((5, 5)) Z += torch.arange(5) print(Z) Z = torch.tile(torch.arange(0, 5), (5, 1)) print(Z)
# Output tensor([[0., 1., 2., 3., 4.], [0., 1., 2., 3., 4.], [0., 1., 2., 3., 4.], [0., 1., 2., 3., 4.], [0., 1., 2., 3., 4.]]) tensor([[0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4]])
Numpy
Z = np.zeros((5,5)) Z += np.arange(5) print(Z) Z = np.tile(np.arange(0, 5), (5,1)) print(Z)
# Output [[0. 1. 2. 3. 4.] [0. 1. 2. 3. 4.] [0. 1. 2. 3. 4.] [0. 1. 2. 3. 4.] [0. 1. 2. 3. 4.]] [[0 1 2 3 4] [0 1 2 3 4] [0 1 2 3 4] [0 1 2 3 4] [0 1 2 3 4]]
38. Consider a generator function that generates 10 integers and use it to build an array (★☆☆)
「10個の整数を生成するジェネレータを考え、それを使用して配列を作成してください」
PyTorch
np.fromiter関数
に相当する関数を見つけることができませんでした。
Numpy
def generate(): for x in range(10): yield x Z = np.fromiter(generate(), dtype=float, count=-1) print(Z)
# Output [0. 1. 2. 3. 4. 5. 6. 7. 8. 9.]
39. Create a vector of size 10 with values ranging from 0 to 1, both excluded (★★☆)
「0から1の範囲を持つ10個のベクトルを作成し、両端を除外してください」
PyTorch
Z = torch.linspace(0, 1, 12)[1:11] print(Z)
# Output tensor([0.0909, 0.1818, 0.2727, 0.3636, 0.4545, 0.5455, 0.6364, 0.7273, 0.8182, 0.9091])
Numpy
Z = np.linspace(0, 1, 11, endpoint=False)[1:] print(Z)
# Output [0.09090909 0.18181818 0.27272727 0.36363636 0.45454545 0.54545455 0.63636364 0.72727273 0.81818182 0.90909091]
40. Create a random vector of size 10 and sort it (★★☆)
「サイズが10のランダムなベクトルを作成し、並び替えてください」
PyTorch
Z = torch.rand(10) Z.sort() print(Z)
# Output tensor([0.9165, 0.6950, 0.4529, 0.1114, 0.9503, 0.5083, 0.0592, 0.6146, 0.9830, 0.0664])
Numpy
Z = np.random.random(10) Z.sort() print(Z)
# Output [0.01628261 0.29794 0.48000757 0.58999975 0.59318463 0.59787013 0.65219298 0.72759454 0.88994109 0.8909954 ]
リンク