Pytorch入門(1)
pytorchとは
- pythonで動作する機械学習用ライブラリ
- Facebook製
torch tensor
1. pytorchの基本機能
2. numpyに似たインタフェース
3. 簡易にgpu演算が可能
import torch
x1 = torch.tensor([1,2])
print(x1)
=> tensor([1, 2])
torch tensor
torch empty
x2 = torch.empty(5, 3)
print(x2)
=> tensor([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]])
1. 未初期化の配列を作成する
2. 割り当てられたメモリにあった値が初期値
torch random
x3 = torch.rand(5, 3)
print(x3)
=> tensor([[0.8747, 0.1603, 0.2513],
[0.6047, 0.7739, 0.9702],
[0.1574, 0.1463, 0.1762],
[0.8923, 0.5875, 0.5725],
[0.3153, 0.1467, 0.9051]])
1. 乱数の配列も可能
torch size
x4 = torch.empty(3, 2)
print( x4.size() )
=> torch.Size([3, 2])
1. 配列のサイズを取得(np.shape的な)
torch operations
x = torch.tensor([1,2,3])
y = torch.tensor([10,20,30])
print(x + y)
=> tensor([11, 22, 33])
配列同士の加算
print(torch.add(x, y))
=> tensor([11, 22, 33])
上記と動作は同じ
torch operations
x = torch.tensor([1,2,3])
y = torch.tensor([10,20,30])
y.add_(x)
print(y)
=> tensor([11, 22, 33])
破壊的メソッド(本例ではyの値を変更する)
メソッド名末尾がアンダーバーの場合は破壊的
torch index
x = torch.tensor([[1,2,3], [4,5,6]])
print(x[:,1])
print(x[:, :2])
=> tensor([2, 5])
=> tensor([[1, 2],
[4, 5]])
numpyライクなインデックス操作
torch view
x = torch.tensor([[1,2,3,4], [5,6,7,8]])
print(x.view(8))
print(x.view(-1, 2))
=> tensor([1, 2, 3, 4, 5, 6, 7, 8])
=> tensor([[1, 2],
[3, 4],
[5, 6],
[7, 8]])
viewで行列をresize, reshapeする
torch item
x = torch.tensor([[1,2,3,4], [5,6,7,8]])
print( x[0, 0].item() )
print( type(x[0, 0].item()) )
=> 1
=> <class ‘int’>
1つの値が欲しい場合はitemで取得
取得した値はpython number型
torch numpy bridge
x1 = torch.tensor([1,2,3,4])
x2 = x1.numpy()
x3 = torch.from_numpy(x2)
print(x)
print(_x)
print(__x)
=> tensor([1, 2, 3, 4])
=> [1 2 3 4]
=> tensor([1, 2, 3, 4])
tensorとnumpyは相互に変換が可能
torch cuda
device = "cuda:1" if torch.cuda.is_available() else "cpu"
x = torch.tensor([1,2,3]).to(device)
y = torch.tensor([10,20,30]).to(device)
z = x + y
print(z)
print(z.to("cpu"))
=> tensor([11, 22, 33], device='cuda:1')
=> tensor([11, 22, 33])
tensorをgpuに転送
deviceを定義することでコードが簡潔になる
torch cuda
device = "cuda:1" if torch.cuda.is_available() else "cpu"
x = torch.tensor([1,2,3]).to(device)
y = torch.tensor([10,20,30])
z = x + y
=> RuntimeError: expected device cuda:1 and dtype Long but got
device cpu and dtype Long
cpu + gpuの演算はエラーとなるので注意
x.to(gpu) + y.to(gpu)もしくはx.to(cpu) + y.to(cpu)
終了

Pytorch 01

  • 1.
  • 2.
  • 3.
    torch tensor 1. pytorchの基本機能 2.numpyに似たインタフェース 3. 簡易にgpu演算が可能
  • 4.
    import torch x1 =torch.tensor([1,2]) print(x1) => tensor([1, 2]) torch tensor
  • 5.
    torch empty x2 =torch.empty(5, 3) print(x2) => tensor([[0., 0., 0.], [0., 0., 0.], [0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]) 1. 未初期化の配列を作成する 2. 割り当てられたメモリにあった値が初期値
  • 6.
    torch random x3 =torch.rand(5, 3) print(x3) => tensor([[0.8747, 0.1603, 0.2513], [0.6047, 0.7739, 0.9702], [0.1574, 0.1463, 0.1762], [0.8923, 0.5875, 0.5725], [0.3153, 0.1467, 0.9051]]) 1. 乱数の配列も可能
  • 7.
    torch size x4 =torch.empty(3, 2) print( x4.size() ) => torch.Size([3, 2]) 1. 配列のサイズを取得(np.shape的な)
  • 8.
    torch operations x =torch.tensor([1,2,3]) y = torch.tensor([10,20,30]) print(x + y) => tensor([11, 22, 33]) 配列同士の加算 print(torch.add(x, y)) => tensor([11, 22, 33]) 上記と動作は同じ
  • 9.
    torch operations x =torch.tensor([1,2,3]) y = torch.tensor([10,20,30]) y.add_(x) print(y) => tensor([11, 22, 33]) 破壊的メソッド(本例ではyの値を変更する) メソッド名末尾がアンダーバーの場合は破壊的
  • 10.
    torch index x =torch.tensor([[1,2,3], [4,5,6]]) print(x[:,1]) print(x[:, :2]) => tensor([2, 5]) => tensor([[1, 2], [4, 5]]) numpyライクなインデックス操作
  • 11.
    torch view x =torch.tensor([[1,2,3,4], [5,6,7,8]]) print(x.view(8)) print(x.view(-1, 2)) => tensor([1, 2, 3, 4, 5, 6, 7, 8]) => tensor([[1, 2], [3, 4], [5, 6], [7, 8]]) viewで行列をresize, reshapeする
  • 12.
    torch item x =torch.tensor([[1,2,3,4], [5,6,7,8]]) print( x[0, 0].item() ) print( type(x[0, 0].item()) ) => 1 => <class ‘int’> 1つの値が欲しい場合はitemで取得 取得した値はpython number型
  • 13.
    torch numpy bridge x1= torch.tensor([1,2,3,4]) x2 = x1.numpy() x3 = torch.from_numpy(x2) print(x) print(_x) print(__x) => tensor([1, 2, 3, 4]) => [1 2 3 4] => tensor([1, 2, 3, 4]) tensorとnumpyは相互に変換が可能
  • 14.
    torch cuda device ="cuda:1" if torch.cuda.is_available() else "cpu" x = torch.tensor([1,2,3]).to(device) y = torch.tensor([10,20,30]).to(device) z = x + y print(z) print(z.to("cpu")) => tensor([11, 22, 33], device='cuda:1') => tensor([11, 22, 33]) tensorをgpuに転送 deviceを定義することでコードが簡潔になる
  • 15.
    torch cuda device ="cuda:1" if torch.cuda.is_available() else "cpu" x = torch.tensor([1,2,3]).to(device) y = torch.tensor([10,20,30]) z = x + y => RuntimeError: expected device cuda:1 and dtype Long but got device cpu and dtype Long cpu + gpuの演算はエラーとなるので注意 x.to(gpu) + y.to(gpu)もしくはx.to(cpu) + y.to(cpu)
  • 16.