end0tknr's kipple - 新web写経開発

http://d.hatena.ne.jp/end0tknr/ から移転しました

install pytorch from source to python3.7 + centos8

メモ。

以下のurlにて、自身の環境に応じたインストール方法を、コマンドライン・レベルで示されます。

https://pytorch.org/

今回は、ソースからインストールですので、以下のようになります。

$ git clone --recursive https://github.com/pytorch/pytorch
$ cd pytorch
$ git submodule sync
$ git submodule update --init --recursive

$ curl -kL https://bootstrap.pypa.io/get-pip.py | sudo /usr/local/python3/bin/python3

$ sudo /usr/local/python3/bin/pip install pyyaml

# 私の手元環境では、3-4h程、要しました
$ sudo /usr/local/python3/bin/python3 setup.py install

# 追加でtorchvisionも
$ sudo /usr/local/python3/bin/pip install torchvision
#!/usr/local/python3/bin/python3
# -*- coding: utf-8 -*-
from __future__ import print_function
import getopt
import sys
import torch


def main():
    x = torch.rand(5, 3)
    print(x)

if __name__ == '__main__':
    main()

↑こう書くと、↓こう出力されます

[end0tknr@cent80 tmp]$ ./foo.py 
tensor([[0.0855, 0.8716, 0.3512],
        [0.7714, 0.7623, 0.8344],
        [0.8667, 0.1638, 0.0306],
        [0.8024, 0.4010, 0.4024],
        [0.9326, 0.5903, 0.6700]])

更にネットワーク定義(nn)は、次のように複数の書き方があります

#!/usr/local/python3/bin/python3
# -*- coding: utf-8 -*-
import torch
import torchvision
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
import torchvision.transforms as transforms
import numpy as np
from matplotlib import pyplot as plt


def main():
    print(make_nn_model_1())
    print(make_nn_model_2())
    print(make_nn_model_3())
    print(make_nn_model_4())
    print(make_nn_model_5())
    print(make_nn_model_6())

    
def make_nn_model_1():
    model = nn.Sequential(
        nn.Conv2d(1,20,5),
        nn.ReLU(),
        nn.Conv2d(20,64,5),
        nn.ReLU()
    )
    return model

def make_nn_model_2():
    model = torch.nn.Sequential()
    model.add_module("conv1", nn.Conv2d(1,20,5))
    model.add_module("relu1", nn.ReLU())
    model.add_module("conv2", nn.Conv2d(20,64,5))
    model.add_module("relu2", nn.ReLU())
    model
    return model

def make_nn_model_3():
    from collections import OrderedDict
    model = nn.Sequential(OrderedDict([
        ('conv1', nn.Conv2d(1,20,5)),
        ('relu1', nn.ReLU()),
        ('conv2', nn.Conv2d(20,64,5)),
        ('relu2', nn.ReLU())
    ]))
    return model

def make_nn_model_4():
    import torch.nn as nn
    import torch.nn.functional as F

    class Model(nn.Module):
        def __init__(self):
            super(Model, self).__init__()
            self.conv1 = nn.Conv2d(1, 20, 5)
            self.conv2 = nn.Conv2d(20, 64, 5)
            
        def forward(self, x):
            x = F.relu(self.conv1(x))
            return F.relu(self.conv2(x))
    model = Model()
    return model

def make_nn_model_5():
    class Model(nn.Module):
        def __init__(self):
            super(Model, self).__init__()
            self.convs = nn.ModuleList([nn.Conv2d(1, 20, 5), nn.Conv2d(20, 64, 5)])

        def forward(self, x):
            for i, l in enumerate(self.convs):
                x = l(x)
            return x
    model = Model()
    return model

def make_nn_model_6():
    class Model(nn.Module):
        def __init__(self):
            super(Model, self).__init__()
            self.convs = nn.ModuleDict({'conv1' : nn.Conv2d(1, 20, 5),
                                        'conv2' : nn.Conv2d(20, 64, 5)})

        def forward(self, x):
            for l in self.convs.values():
                x = l(x)
            return x
    model = Model()
    return model

    
if __name__ == '__main__':
    main()

↑こう書くと、↓こう出力されます

Sequential(
  (0): Conv2d(1, 20, kernel_size=(5, 5), stride=(1, 1))
  (1): ReLU()
  (2): Conv2d(20, 64, kernel_size=(5, 5), stride=(1, 1))
  (3): ReLU()
)
Sequential(
  (conv1): Conv2d(1, 20, kernel_size=(5, 5), stride=(1, 1))
  (relu1): ReLU()
  (conv2): Conv2d(20, 64, kernel_size=(5, 5), stride=(1, 1))
  (relu2): ReLU()
)
Sequential(
  (conv1): Conv2d(1, 20, kernel_size=(5, 5), stride=(1, 1))
  (relu1): ReLU()
  (conv2): Conv2d(20, 64, kernel_size=(5, 5), stride=(1, 1))
  (relu2): ReLU()
)
Model(
  (conv1): Conv2d(1, 20, kernel_size=(5, 5), stride=(1, 1))
  (conv2): Conv2d(20, 64, kernel_size=(5, 5), stride=(1, 1))
)
Model(
  (convs): ModuleList(
    (0): Conv2d(1, 20, kernel_size=(5, 5), stride=(1, 1))
    (1): Conv2d(20, 64, kernel_size=(5, 5), stride=(1, 1))
  )
)
Model(
  (convs): ModuleDict(
    (conv1): Conv2d(1, 20, kernel_size=(5, 5), stride=(1, 1))
    (conv2): Conv2d(20, 64, kernel_size=(5, 5), stride=(1, 1))
  )
)