[A-00104]PyTorchを使ってみた
pythonの機械学習ライブラリであるpytorchを使ってみました。
・インストール
公式ドキュメントは下記
https://pytorch.org/get-started/previous-versions/
pip install torch==2.0.0 torchvision==0.15.1 torchaudio==2.0.1
・クイックスタート
試しに動かしてみます。上記のライブラリをインストール後、下記のコードを動かす事ができます。
import torch
from torch import nn
from torch.utils.data import DataLoader
from torchvision import datasets
from torchvision.transforms import ToTensor
training_data = datasets.FashionMNIST(
root="data",
train=True,
download=True,
transform=ToTensor()
)
test_data = datasets.FashionMNIST(
root="data",
train=False,
download=True,
transform=ToTensor()
)
batch_size = 64
train_dataloader = DataLoader(training_data, batch_size=batch_size)
test_dataloader = DataLoader(test_data, batch_size=batch_size)
for X, y in test_dataloader:
print(f"Shape of X [N, C, H, W]: {X.shape}")
print(f"Shape of y: {y.shape} {y.dtype}")
break
(.venv)MacBook-Pro:test2_project$ python3 mlpytorch.py
Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/train-images-idx3-ubyte.gz
Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/train-images-idx3-ubyte.gz to data/FashionMNIST/raw/train-images-idx3-ubyte.gz
100.0%
Extracting data/FashionMNIST/raw/train-images-idx3-ubyte.gz to data/FashionMNIST/raw
Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/train-labels-idx1-ubyte.gz
Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/train-labels-idx1-ubyte.gz to data/FashionMNIST/raw/train-labels-idx1-ubyte.gz
100.0%
Extracting data/FashionMNIST/raw/train-labels-idx1-ubyte.gz to data/FashionMNIST/raw
Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/t10k-images-idx3-ubyte.gz
Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/t10k-images-idx3-ubyte.gz to data/FashionMNIST/raw/t10k-images-idx3-ubyte.gz
100.0%
Extracting data/FashionMNIST/raw/t10k-images-idx3-ubyte.gz to data/FashionMNIST/raw
Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/t10k-labels-idx1-ubyte.gz
Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/t10k-labels-idx1-ubyte.gz to data/FashionMNIST/raw/t10k-labels-idx1-ubyte.gz
100.0%
Extracting data/FashionMNIST/raw/t10k-labels-idx1-ubyte.gz to data/FashionMNIST/raw
Shape of X [N, C, H, W]: torch.Size([64, 1, 28, 28])
Shape of y: torch.Size([64]) torch.int64
・モデルの作成
次はモデルを作ってその中身を覗いてみます。
import torch
from torch import nn
from torch.utils.data import DataLoader
from torchvision import datasets
from torchvision.transforms import ToTensor
# Get cpu, gpu or mps device for training.
device = (
"cuda"
if torch.cuda.is_available()
else "mps"
if torch.backends.mps.is_available()
else "cpu"
)
print(f"Using {device} device")
# Define model
class NeuralNetwork(nn.Module):
def __init__(self):
super().__init__()
self.flatten = nn.Flatten()
self.linear_relu_stack = nn.Sequential(
nn.Linear(28*28, 512),
nn.ReLU(),
nn.Linear(512, 512),
nn.ReLU(),
nn.Linear(512, 10)
)
def forward(self, x):
x = self.flatten(x)
logits = self.linear_relu_stack(x)
return logits
model = NeuralNetwork().to(device)
print(model)
(.venv)MacBook-Pro:test3_prj$ python3 testModel.py
Using cpu device
NeuralNetwork(
(flatten): Flatten(start_dim=1, end_dim=-1)
(linear_relu_stack): Sequential(
(0): Linear(in_features=784, out_features=512, bias=True)
(1): ReLU()
(2): Linear(in_features=512, out_features=512, bias=True)
(3): ReLU()
(4): Linear(in_features=512, out_features=10, bias=True)
)
)
・テンソルを作成する
正直、上記のようなモデルの作成などはもう少し知識をつけなくてはわかりません。
なのでまずはテンソルが何なのかを考えたいと思います。
公式ではテンソルはデータの抽象化と定義しています。百聞は一見にしかずという事でどんな動きをするのかみてみたいと思います。torchのemptyメソッドで3行4列のテンソルを作成します。要は線形代数みたいなものです。
import torch
import math
x = torch.empty(3, 4)
print(type(x))
print(x)
(.venv)MacBook-Pro:test3_prj$ python3 tensorTest.py
<class 'torch.Tensor'>
tensor([[9.8091e-45, 0.0000e+00, 0.0000e+00, 0.0000e+00],
[0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00],
[0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00]])
emptyメソッドで作成したテンソル(行列)はデフォルトでメモリに割り当てられた値が入るようです。
次に0,1,ランダムな値で構成されたテンソル(行列)を作成したいと思います。
import torch
import math
if __name__ == "__main__":
zeros = torch.zeros(2, 3)
print(zeros)
ones = torch.ones(2, 3)
print(ones)
torch.manual_seed(1729)
rand = torch.rand(2, 3)
print(rand)
(.venv)MacBook-Pro:test3_prj$ python3 tensorTest.py
tensor([[0., 0., 0.],
[0., 0., 0.]])
tensor([[1., 1., 1.],
[1., 1., 1.]])
tensor([[0.3126, 0.3791, 0.3087],
[0.0736, 0.4216, 0.0691]])
・ランダムテンソルの生成と再現性について
公式のドキュメントからのネタです。前述のコードで使用したmanual_seedメソッドは乱数ジェネレータですが、初期化する事により再現性がある事がわかります。その再現性を確認しましょう。
import torch
import math
if __name__ == "__main__":
torch.manual_seed(1729)
random1 = torch.rand(2, 3)
print(random1)
random2 = torch.rand(2, 3)
print(random2)
torch.manual_seed(1729)
random3 = torch.rand(2, 3)
print(random3)
random4 = torch.rand(2, 3)
print(random4)
(.venv)MacBook-Pro:test3_prj$ python3 tensorTest.py
tensor([[0.3126, 0.3791, 0.3087],
[0.0736, 0.4216, 0.0691]])
tensor([[0.2332, 0.4047, 0.2162],
[0.9927, 0.4128, 0.5938]])
tensor([[0.3126, 0.3791, 0.3087],
[0.0736, 0.4216, 0.0691]])
tensor([[0.2332, 0.4047, 0.2162],
[0.9927, 0.4128, 0.5938]])
上記の通り、初期化したタイミングで同じ値が繰り返されています。これらのメソッドは研究などで利用するようなので頭の片隅に止めておきましょう。
・テンソルデータの操作
先ほどは行列の要素を数字で指定し、そこにonesメソッドを用いて規定の値のテンソルを作成しました。
今度は自分で指定する方法でテンソルを作ってみましょう。
import torch
import math
if __name__ == "__main__":
some_constants = torch.tensor([[3.1415926, 2.71828], [1.61803, 0.0072897]])
print(some_constants)
some_integers = torch.tensor((2, 3, 5, 7, 11, 13, 17, 19))
print(some_integers)
more_integers = torch.tensor(((2, 4, 6), [3, 6, 9]))
print(more_integers)
(.venv)MacBook-Pro:test3_prj$ python3 tensorTest.py
tensor([[3.1416, 2.7183],
[1.6180, 0.0073]])
tensor([ 2, 3, 5, 7, 11, 13, 17, 19])
tensor([[2, 4, 6],
[3, 6, 9]])
上記のようにtensorメソッドを使用すれば自分で値を決めてテンソルを作成する事が可能です。
・テンソルの要素のデータ型を指定する
先ほどから紹介しているテンソルの要素は全てFloatになっています。これをデータ型を指定し、定義する事ができます。
import torch
if __name__ == "__main__":
a = torch.ones((2, 3), dtype=torch.int16)
print(a)
b = torch.rand((2, 3), dtype=torch.float64) * 20
print(b)
c = b.to(torch.int32)
print(c)
(.venv)MacBook-Pro:test3_prj$ python3 tensorTest.py
tensor([[1, 1, 1],
[1, 1, 1]], dtype=torch.int16)
tensor([[19.4630, 15.4067, 6.5362],
[11.7561, 3.4164, 2.2495]], dtype=torch.float64)
tensor([[19, 15, 6],
[11, 3, 2]], dtype=torch.int32)
コメントを残す