pytorch入門(一):numpyとpytorch
numpyとpytorch1、作成 2、形状 3、行列乗算 4、転化 5、ベクトル演算 6、昇次元 7、次元ダウン 8、乱数シード 9、正規分布乱数 10、ランダム整数 11、関数 2 12、合計 13、点乗 torchとnumpyには多くの類似点があり、gpuバージョンのnumpyと見なすことができる.
コード#コード#
1、作成
arange
結果
2、形状
形を変える
結果
3、行列乗算
コード#コード#
結果
4、転化
テンソル=>配列、配列=>テンソル
結果
5、ベクトル演算
ベクトル加算、減算数を乗算します.
結果
6、昇次元
次元の追加
結果
7、次元を下げる
コード#コード#
結果
8、乱数シード
同じ乱数を生成
結果
9、正規分布乱数
共通分布乱数
結果
10、ランダム整数
コード#コード#
結果
11、関数
ベクトルバージョンの数学関数
結果
12、和を求める
異なる方向の和を求める
結果
13、点乗
対応する要素の乗算
結果
コード#コード#
import numpy as np
import torch
1、作成
arange
print(np.arange(6))
print(torch.arange(6))
結果
[0 1 2 3 4 5]
tensor([0, 1, 2, 3, 4, 5])
2、形状
形を変える
a = np.arange(6).reshape(2, 3)
print(a.shape)
b = torch.arange(6).reshape(2, 3)
print(b.shape)
結果
(2, 3)
torch.Size([2, 3])
3、行列乗算
コード#コード#
print(a.dot(a.T))
print(np.matmul(a, a.T))
print(torch.mm(b, b.T))
結果
[[ 5 14]
[14 50]]
[[ 5 14]
[14 50]]
tensor([[ 5, 14],
[14, 50]])
4、転化
テンソル=>配列、配列=>テンソル
print(torch.from_numpy(a))
print(b.numpy())
結果
tensor([[0, 1, 2],
[3, 4, 5]], dtype=torch.int32)
[[0 1 2]
[3 4 5]]
5、ベクトル演算
ベクトル加算、減算数を乗算します.
a = np.arange(6).reshape(2, 3)
print(a + 10)
b = torch.arange(6).reshape(2, 3)
print(b + 10)
結果
[[10 11 12]
[13 14 15]]
tensor([[10, 11, 12],
[13, 14, 15]])
6、昇次元
次元の追加
print(a[None].shape)
print(a[:, None].shape)
print(a[:, :, None].shape)
for i in range(3):
print(b.unsqueeze(i).shape)
結果
(1, 2, 3)
(2, 1, 3)
(2, 3, 1)
torch.Size([1, 2, 3])
torch.Size([2, 1, 3])
torch.Size([2, 3, 1])
7、次元を下げる
コード#コード#
a = np.arange(6).reshape(2, 3)
b = torch.arange(6).reshape(2, 3)
print(a[None].shape)
print(a[None].squeeze().shape)
print(a[:, None].shape)
print(a[:, None].squeeze(1).shape)
print(a[:, :, None].shape)
print(a[:, :, None].squeeze(2).shape)
for i in range(3):
print(b.unsqueeze(i).shape)
print(b.unsqueeze(i).squeeze(i).shape)
結果
(1, 2, 3)
(2, 3)
(2, 1, 3)
(2, 3)
(2, 3, 1)
(2, 3)
torch.Size([1, 2, 3])
torch.Size([2, 3])
torch.Size([2, 1, 3])
torch.Size([2, 3])
torch.Size([2, 3, 1])
torch.Size([2, 3])
8、乱数シード
同じ乱数を生成
np.random.seed(1)
print(np.random.rand(1))
np.random.seed(1)
print(np.random.rand(1))
torch.manual_seed(1)
print(torch.rand(1))
torch.manual_seed(1)
print(torch.rand(1))
結果
[0.417022]
[0.417022]
tensor([0.7576])
tensor([0.7576])
9、正規分布乱数
共通分布乱数
print(np.random.randn(1))
print(torch.randn(1))
print(np.random.normal(0,1,[2]))
print(torch.normal(0,1,[2]))
結果
[-1.65451545]
tensor([0.1584])
[-2.3634686 1.13534535]
tensor([-0.6617, -0.0426])
10、ランダム整数
コード#コード#
print(np.random.randint(5,size=[2,3]))
print(torch.randint(5,size=[2,3]))
結果
[[2 4 3]
[4 2 4]]
tensor([[3, 3, 0],
[2, 1, 2]])
11、関数
ベクトルバージョンの数学関数
print(np.sin([1,2,3]))
print(torch.sin(torch.Tensor([1,2,3])))
結果
[0.84147098 0.90929743 0.14112001]
tensor([0.8415, 0.9093, 0.1411])
12、和を求める
異なる方向の和を求める
a = np.arange(6).reshape(2,3)
print(a.sum(0))
print(a.sum(1))
print(a.sum())
b = torch.arange(6).reshape(2,3)
print(b.sum(0))
print(b.sum(1))
print(b.sum())
結果
[3 5 7]
[ 3 12]
15
tensor([3, 5, 7])
tensor([ 3, 12])
tensor(15)
13、点乗
対応する要素の乗算
a = np.arange(6).reshape(2,3)
print(a*a)
print(np.multiply(a,a))
b = torch.arange(6).reshape(2,3)
print(b*b)
print(torch.mul(b,b))
結果
[0.417022]
[0.417022]
tensor([0.7576])
tensor([0.7576])