コンピュータとオセロ対戦31　～深層学習で勝敗予測、実験～

https://qiita.com/tt_and_tk/items/068f2afde6db637e189f

[前回](https://qiita.com/tt_and_tk/items/eb2a0389626b160f32f0)

# 今回の目標
前回行った深層学習の改善を行う予定でしたが、ミスを見つけたのでまずはその修正を行います。
そののち改善のための実験を行います。
# ここから本編
## ミス修正
データ集めの際、オセロの試合が終わった時に盤面を元に戻していませんでした。これにより、第二試合以降は始まった瞬間に終了しており十分なデータが集められていませんでした。
修正は以下の通り。

```py
data = []
result = []

turn_vari = [i for i in range(5, 61, 5)]
run = learn(0, 0, turn_vari, eva=eva)

for i in range(10):
    print("\r[" + "#" * (i+1) + " " * (10-i+1) + "]", end="")
    for black in PLAY_WAY:
        for white in PLAY_WAY:
            run.setup()   # この部分が足りなかった
            run.black_method = black
            run.white_method = white
            run.eva = eva
            data_ele, result_ele = run.play()
            for data_each_turn in data_ele:
                data.append(data_each_turn)
            for result_each_turn in result_ele:
                result.append([result_each_turn])

data = np.array(data).astype(np.float32)
result = np.array(result).astype(np.float32)

print("\r[" + "#" * 10 + "]")
```

さらに、盤面の記録にもミスがありました。
これまでの方法だとビットシフトしすぎて後半がすべて0になってしまっていました。

```py
    # これまで
    def data_set(self, data: list, turn_num: int) -> None:
        data.append([])
        if self.turn:
            my = ["b_u", "b_d"]
            opp = ["w_u", "w_d"]
        else:
            my = ["w_u", "w_d"]
            opp = ["b_u", "b_d"]
        for i in range(64):
            data[-1].append(int((self.bw[my[i >= 32]] & (1 << i)) != 0))
            data[-1].append(int((self.bw[opp[i >= 32]] & (1 << i)) != 0))

    # 修正
    def data_set(self, data: list, turn_num: int) -> None:
        data.append([])
        if self.turn:
            my = ["b_u", "b_d"]
            opp = ["w_u", "w_d"]
        else:
            my = ["w_u", "w_d"]
            opp = ["b_u", "b_d"]
        for i in range(32):
            if self.bw[my[0]] & 1 << i:
                data[-1].append(1)
            else:
                data[-1].append(0)
            if self.bw[opp[0]] & 1 << i:
                data[-1].append(1)
            else:
                data[-1].append(0)
        for i in range(32):
            if self.bw[my[1]] & 1 << i:
                data[-1].append(1)
            else:
                data[-1].append(0)
            if self.bw[opp[1]] & 1 << i:
                data[-1].append(1)
            else:
                data[-1].append(0)
```

データ集めのミスを修正すると今度は学習しなくなったので、まずturn_variを変更しました。データ量が少ないから学習しなかったのでは？　と考えたためです。

```py
# turn_vari = [i for i in range(5, 61, 5)]
turn_vari = [i for i in range(1, 61)]
```

また、よく考えると目的変数はマイナスになることもあるので、マイナス値の存在しないreluを活性化関数にするのは問題がありました。
ということでとりあえずtanhに変更。

```py
class Net(chainer.Chain):
    def __init__(self, n_in, n_hidden, n_out):
        super().__init__()
        with self.init_scope():
            self.l1 = L.Linear(n_in, n_hidden)
            self.l2 = L.Linear(n_hidden, n_hidden)
            self.l3 = L.Linear(n_hidden, n_out)
    
    def forward(self, x):
        h = F.tanh(self.l1(x))
        h = F.tanh(self.l2(h))
        h = self.l3(h)

        return h
```

batch_sizeが10ではほぼ学習しなかったので50にすると改善しました。これに関しては追って検証します。

```py
batch_size = 50

dataset = TupleDataset(data, result)

train_val, test = split_dataset_random(dataset, int(len(dataset) * 0.7), seed=0)
train, valid = split_dataset_random(train_val, int(len(train_val) * 0.7), seed=0)

train_iter = SerialIterator(train, batch_size=batch_size, repeat=True, shuffle=True)
```

また前回のn_batch変数が何もしていないことに気づいたので消しました。チュートリアル内にあったので書いてみたのですが、なぜ書いてあったのかは分かりません。

他の条件は前回と同じで再学習。
結果は以下。

![mean squared error of each epoch.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/0fa2c355-0b7c-5c5e-22a4-47b007fcfbc9.png)

![mean absolute error of each epoch.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/3177f6ac-cbd3-24df-2a50-b54b73740134.png)

```
test MSE: 371.1285
test MAE: 14.7923
```

前回と比べると見劣りする結果ですが、何とか学習はしてくれました。

## 実験
### ファイル構造
実験のためのファイル構造は以下の通り。

```
experiment
├── fig
│   └── ※実験データをグラフ化した画像が入る※
├── Net_box
│   └── ※ニューラルネットワークを定義したPythonファイルが入る※
├── BitBoard.py　　　・・・オセロのための基本クラスを定義
├── deep_learn.py　　・・・深層学習のためのクラスを定義
├── osero_learn.py 　・・・データ集めのためのクラスを定義
└── run.ipynb　　　　・・・実験実行のためのファイル
```
### BitBoard.py
変更はありません。
### osero_learn.py
記事冒頭で示した以外の変更はありません。
### deep_learn.py
#### インポート部分
下2行が上述したクラスです。

```py
from copy import deepcopy

import numpy as np
from chainer.datasets import TupleDataset, split_dataset_random
from chainer.iterators import SerialIterator
import chainer
import chainer.functions as F
from chainer import optimizers
from chainer.optimizer_hooks import WeightDecay
from chainer.dataset import concat_examples
import matplotlib.pyplot as plt

from BitBoard import osero
from osero_learn import learn
```

#### 定義部分

```py
PLAY_WAY = deepcopy(osero.PLAY_WAY)
del PLAY_WAY["human"]
PLAY_WAY = PLAY_WAY.values()

eva = [[
     1.0, -0.6,  0.6,  0.4,  0.4,  0.6, -0.6,  1.0,
    -0.6, -0.8,  0.0,  0.0,  0.0,  0.0, -0.8, -0.6,
     0.6,  0.0,  0.8,  0.6,  0.6,  0.8,  0.0,  0.6,
     0.4,  0.0,  0.6,  0.0,  0.0,  0.6,  0.0,  0.4,
     0.4,  0.0,  0.6,  0.0,  0.0,  0.6,  0.0,  0.4,
     0.6,  0.0,  0.8,  0.6,  0.6,  0.8,  0.0,  0.6,
    -0.6, -0.8,  0.0,  0.0,  0.0,  0.0, -0.8, -0.6,
     1.0, -0.6,  0.6,  0.4,  0.4,  0.6, -0.6,  1.0
] for i in range(2)]

check_point = [i for i in range(1, 61)]
```

#### コンストラクタ
とにかくインスタンス変数をたくさん作りました。

```py
class deep_learn:
    def __init__(self):
        self.PLAY_WAY = PLAY_WAY
        self.eva = eva
        self.data = []
        self.result = []
        self.osero = learn(0, 0, check_point)
        self.num = 1
        self.n_epoch = 50
        self.batch_size = 50
        class Inner_Net(chainer.Chain):
            pass
        self.Net = Inner_Net
        self.lr = 0.01
        self.optimizer = optimizers.SGD(lr=self.lr)
        self.WeightDecay = 0.00001
        self.hook_f = WeightDecay(self.WeightDecay)
        self.xlabel1, self.xlabel2 = "epoch", "epoch"
        self.ylabel1, self.ylabel2 = "mean squared error", "mean abolute error"
        self.fig_name1, self.fig_name2 = "MSE of each epoch", "MAE of each epoch"
        self.save_dir = "fig"
```

#### set_data
学習のためのデータ集めをするメソッド。

```py
    def set_data(self):
        self.data = []
        self.result = []

        for i in range(self.num):
            for black in self.PLAY_WAY:
                for white in self.PLAY_WAY:
                    self.osero.setup()
                    self.osero.black_method = black
                    self.osero.white_method = white
                    self.osero.eva = self.eva
                    data_ele, result_ele = self.osero.play()
                    for data_each_turn in data_ele:
                        self.data.append(data_each_turn)
                    for result_each_turn in result_ele:
                        self.result.append([result_each_turn])

        self.data = np.array(self.data).astype(np.float32)
        self.result = np.array(self.result).astype(np.float32)

        self.dataset = TupleDataset(self.data, self.result)
        train_val, self.test = split_dataset_random(\
            self.dataset,
            int(len(self.dataset) * 0.7),
            seed = 0
        )
        self.train, self.valid = split_dataset_random(\
            train_val,
            int(len(train_val) * 0.7),
            seed = 0
        )
        self.train_iter = SerialIterator(
            self.train,
            batch_size = self.batch_size,
            repeat = True,
            shuffle = True
        )
```

#### fit
学習を行うメソッド。

```py
    def fit(self):
        net = self.Net()

        optimizer = self.optimizer
        optimizer.setup(net)

        for param in net.params():
            if param.name != "b":
                param.update_rule.add_hook(self.hook_f)
        
        results_train = {
            "MSE": [],
            "MAE": []
        }
        results_valid = {
            "MSE": [],
            "MAE": []
        }

        self.train_iter.reset()

        for epoch in range(self.n_epoch):
            while True:
                train_batch = self.train_iter.next()
                
                x_train, t_train = concat_examples(train_batch)

                y_train = net(x_train)
                MSE_train = F.mean_squared_error(y_train, t_train)
                MAE_train = F.mean_absolute_error(y_train, t_train)

                net.cleargrads()
                MSE_train.backward()

                self.optimizer.update()

                if self.train_iter.is_new_epoch:
                    with chainer.using_config("train", False), chainer.using_config("enable_backprop", False):
                        x_valid, t_valid = concat_examples(self.valid)
                        y_valid = net(x_valid)
                        MSE_valid = F.mean_squared_error(y_valid, t_valid)
                        MAE_valid = F.mean_absolute_error(y_valid, t_valid)

                    results_train["MSE"].append(MSE_train.array)
                    results_train["MAE"].append(MAE_train.array)
                    results_valid["MSE"].append(MSE_valid.array)
                    results_valid["MAE"].append(MAE_valid.array)

                    break
        
        self.results_train = results_train
        self.results_valid = results_valid
        self.net = net
```

#### plot
学習過程での平均二乗誤差と平均絶対誤差をグラフ化するメソッド。

```py
    def plot(self):
        fig = plt.figure(figsize=(10, 10))
        plt.plot(self.results_train["MSE"], label="train")
        plt.plot(self.results_valid["MSE"], label="valid")
        plt.legend()
        plt.xlabel(self.xlabel1)
        plt.ylabel(self.ylabel1)
        plt.title(self.fig_name1)
        plt.savefig(self.save_dir + "/" + self.fig_name1)
        plt.clf()
        plt.close()

        fig = plt.figure(figsize=(10, 10))
        plt.plot(self.results_train["MAE"], label="train")
        plt.plot(self.results_valid["MAE"], label="valid")
        plt.legend()
        plt.xlabel(self.xlabel2)
        plt.ylabel(self.ylabel2)
        plt.title(self.fig_name2)
        plt.savefig(self.save_dir + "/" + self.fig_name2)
        plt.clf()
        plt.close()
```

#### cal_test_error
テストデータでの平均二乗誤差と平均絶対誤差を計算するメソッド。

```py
    def cal_test_error(self):
        x_test, t_test = concat_examples(self.test)
        with chainer.using_config("train", False),\
             chainer.using_config("enable_backprop", False):
            y_test = self.net(x_test)
            MSE_test = F.mean_squared_error(y_test, t_test)
            MAE_test = F.mean_absolute_error(y_test, t_test)
        return MSE_test, MAE_test
```

### Net_box
ニューラルネットワークを定義したファイルを置いたフォルダ。

#### plane.py
ミス修正の時に用いたクラス。

```py
from chainer import Chain
import chainer.links as L
import chainer.functions as F

class Net(Chain):
    def __init__(self):
        super().__init__()
        n_in = 128
        n_hidden = 128
        n_out = 1
        with self.init_scope():
            self.l1 = L.Linear(n_in, n_hidden)
            self.l2 = L.Linear(n_hidden, n_hidden)
            self.l3 = L.Linear(n_hidden, n_out)

    def __call__(self, x):
        h = F.tanh(self.l1(x))
        h = F.tanh(self.l2(h))
        h = self.l3(h)

        return h
```

#### n_layer.py
層の数のみ変更したクラス。

```py
from chainer import Chain
import chainer.links as L
import chainer.functions as F

n_in = 128
n_hidden = 128
n_out = 1

class Net2(Chain):
    def __init__(self):
        super().__init__()
        with self.init_scope():
            self.l1 = L.Linear(n_in, n_hidden)
            self.l2 = L.Linear(n_hidden, n_out)
    
    def __call__(self, x):
        h = F.tanh(self.l1(x))
        h = self.l2(h)
        
        return h


class Net3(Chain):
    def __init__(self):
        super().__init__()
        with self.init_scope():
            self.l1 = L.Linear(n_in, n_hidden)
            self.l2 = L.Linear(n_hidden, n_hidden)
            self.l3 = L.Linear(n_hidden, n_out)
    
    def __call__(self, x):
        h = F.tanh(self.l1(x))
        h = F.tanh(self.l2(h))
        h = self.l3(h)
        
        return h


class Net4(Chain):
    def __init__(self):
        super().__init__()
        with self.init_scope():
            self.l1 = L.Linear(n_in, n_hidden)
            self.l2 = L.Linear(n_hidden, n_hidden)
            self.l3 = L.Linear(n_hidden, n_hidden)
            self.l4 = L.Linear(n_hidden, n_out)
    
    def __call__(self, x):
        h = F.tanh(self.l1(x))
        h = F.tanh(self.l2(h))
        h = F.tanh(self.l3(h))
        h = self.l4(h)
        
        return h
    

class Net5(Chain):
    def __init__(self):
        super().__init__()
        with self.init_scope():
            self.l1 = L.Linear(n_in, n_hidden)
            self.l2 = L.Linear(n_hidden, n_hidden)
            self.l3 = L.Linear(n_hidden, n_hidden)
            self.l4 = L.Linear(n_hidden, n_hidden)
            self.l5 = L.Linear(n_hidden, n_out)
    
    def __call__(self, x):
        h = F.tanh(self.l1(x))
        h = F.tanh(self.l2(h))
        h = F.tanh(self.l3(h))
        h = F.tanh(self.l4(h))
        h = self.l5(h)
        
        return h
```

#### kind_function.py
活性化関数の種類のみ変更したクラス。

```py
from chainer import Chain
import chainer.links as L
import chainer.functions as F

functions_arr = [
    F.relu,
    F.tanh,
    F.elu,
    F.leaky_relu,
    F.rrelu,
    F.selu
]

functions_name = [
    "relu",
    "tanh",
    "elu",
    "leaky_relu",
    "rrelu",
    "selu"
]

func = None

class Net(Chain):
    def __init__(self):
        super().__init__()
        n_in = 128
        n_hidden = 128
        n_out = 1
        with self.init_scope():
            self.l1 = L.Linear(n_in, n_hidden)
            self.l2 = L.Linear(n_hidden, n_hidden)
            self.l3 = L.Linear(n_hidden, n_out)

    def __call__(self, x):
        h = func(self.l1(x))
        h = func(self.l2(h))
        h = self.l3(h)

        return h
```

### run.ipynb
まず、以下のインポートを行います。

```py
from deep_learn import *
import Net_box.plane as plane
```

#### データ量変更
全ての手法の総当たり戦を、全部で何通りするかをnum変数で表します。

```py
run = deep_learn()
run.Net = plane.Net

num_arr = [1, 10, 50]
run.save_dir = "fig/data_quantity"
i = 1
MSE_test = []
MAE_test = []

for num in num_arr:
    print("\r%d/%d" % (i, len(num_arr)), end="")
    run.num = num
    run.fig_name1 = "MSE of each epoch (data quantity is %d)" % num
    run.fig_name2 = "MAE of each epoch (data quantity is %d)" % num
    run.set_data()
    run.fit()
    run.plot()
    test_error = run.cal_test_error()
    MSE_test.append(test_error[0])
    MAE_test.append(test_error[1])
    i += 1
```

結果はこちら。

![MSE of each epoch (data quantity is 1).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/e59dacec-950b-86ce-97b1-68b265ab672b.png)
![MSE of each epoch (data quantity is 10).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/e0b9fb66-da9e-83ea-c253-e3feb26198c3.png)
![MSE of each epoch (data quantity is 50).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/6e5dc29f-d2e1-dd55-4086-75770ea9e3d3.png)
![MAE of each epoch (data quantity is 1).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/403b4486-c8a4-22e6-ebc0-abf524c335e4.png)
![MAE of each epoch (data quantity is 10).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/89ac2d48-cfd7-ce5f-c800-9b623da6ad02.png)
![MAE of each epoch (data quantity is 50).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/0f520b8d-1901-c467-8d3b-7276c10c11a9.png)

```
MSE:  [variable(187.12271), variable(328.72275), variable(488.58707)]
MAE:  [variable(9.387474), variable(13.689546), variable(17.651814)]
```

意外にもデータ量を増やすほど上手く学習できないと分かりました。1が最優秀という結果に。
またデータ量1および10のとき、どちらもエポック数10回まではトレーニングデータと確認データの精度はほぼ同じですが、それ以降は異なっています。理由は分かりません。
また、平均絶対誤差において、最初は1より10の方が誤差が小さいですが、10回以降では逆転しています。平均二乗誤差ではそうはなっておらず、10回目以降の誤差の値が異なるのみでした。平均二乗誤差に大きな差がないにもかかわらず平均絶対誤差が異なる理由として、データ量10では全体的に小さい誤差が多いが、たまに大きな誤差が混じっていると考えられます。
十分学習した時の誤差は、平均も二乗も1が勝っていました。

#### バッチサイズ変更

```py
run = deep_learn()
run.Net = plane.Net

batch_arr = [5, 10, 25, 50, 75, 100, 150, 200, 300]
run.save_dir = "fig/batch_size"
i = 1
MSE_test = []
MAE_test = []

run.set_data()

for batch_size in batch_arr:
    print("\r%d/%d" % (i, len(batch_arr)), end="")
    run.batch_size = batch_size
    run.fig_name1 = "MSE of each epoch (batch size is %d)" % batch_size
    run.fig_name2 = "MAE of each epoch (batch size is %d)" % batch_size
    run.fit()
    run.plot()
    test_error = run.cal_test_error()
    MSE_test.append(test_error[0])
    MAE_test.append(test_error[1])
    i += 1
```

結果はこちら。

![MSE of each epoch (batch size is 5).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/e92e2c75-2863-83ff-8dbc-ba1762deaf3d.png)
![MSE of each epoch (batch size is 10).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/5bf1c4ff-f18c-fb79-3610-82c1e1dc336c.png)
![MSE of each epoch (batch size is 25).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/64a4abd1-0d77-b25d-6ce3-180683da8ad2.png)
![MSE of each epoch (batch size is 50).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/e3fe86c5-7108-0544-67a4-e52e5ab13255.png)
![MSE of each epoch (batch size is 75).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/ba6fc7b3-971c-547e-e05f-4874e04aa012.png)
![MSE of each epoch (batch size is 100).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/ddb4121a-e3a7-67f4-9cac-ab65f195cdd9.png)
![MSE of each epoch (batch size is 150).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/9609277f-b609-07e9-36d5-a9a1e6f85a1b.png)
![MSE of each epoch (batch size is 200).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/80ebda41-58f1-1a2f-9f4c-eab62865dafa.png)
![MSE of each epoch (batch size is 300).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/295f36b3-af43-ba7a-7ac7-9bdc0e47d425.png)

![MAE of each epoch (batch size is 5).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/ffa9b2ef-10b0-a6a0-b81e-b8e7a6dc85f0.png)
![MAE of each epoch (batch size is 10).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/218d9e61-9def-f6f0-e1ec-837953be974a.png)
![MAE of each epoch (batch size is 25).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/b1d0aca6-af5d-3ce1-d842-d918ed9448c2.png)
![MAE of each epoch (batch size is 50).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/1fa6e385-d094-50b5-e142-1d186e56a168.png)
![MAE of each epoch (batch size is 75).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/cba7d5a9-3b23-fc21-a52e-33bb50c46c7f.png)
![MAE of each epoch (batch size is 100).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/7b87b366-9ae9-e955-6105-b3a7ca7af53e.png)
![MAE of each epoch (batch size is 150).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/c3026c0b-3b95-830e-4b39-06b5dab4f58d.png)
![MAE of each epoch (batch size is 200).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/9d6d3db4-b693-40c9-bc17-0a6db5323a12.png)
![MAE of each epoch (batch size is 300).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/ee84eed9-f8aa-67b1-dee2-46530b62792b.png)

```
MSE:  [variable(208.4556), variable(205.58469), variable(212.24141), variable(205.57669), variable(223.43636), variable(193.73814), variable(199.80457), variable(215.08165), variable(213.957)]
MAE:  [variable(9.421841), variable(9.583598), variable(9.503884), variable(9.683197), variable(9.855479), variable(9.166233), variable(8.903477), variable(9.325055), variable(9.886406)]
```

増やすほど結果がよくなるような、単純な結果にはなりませんでした。
最終結果はどれもほぼ同じでしたが、収束までの時間に差がありました。バッチ数を減らすほど少ないエポック数で最適解に近づいているようです。しかしバッチ数が少ないほど1つのエポックにかかる時間は増加します。
最優秀を決めるとすれば、テストデータでの誤差が小さく、収束までの実時間もそう長くない150でしょうか。

#### 層の数を変更

```py
import Net_box.n_layer as n_layer

run = deep_learn()

Net_arr = [n_layer.Net2, n_layer.Net3, n_layer.Net4, n_layer.Net5]
run.save_dir = "fig/n_layer"
i = 1
MSE_test = []
MAE_test = []

run.set_data()

for Net in Net_arr:
    print("\r%d/%d" % (i, len(Net_arr)), end="")
    run.Net = Net
    run.fig_name1 = "MSE of each epoch (number of layer is %d)" % (i + 1)
    run.fig_name2 = "MAE of each epoch (number of layer is %d)" % (i + 1)
    run.fit()
    run.plot()
    test_error = run.cal_test_error()
    MSE_test.append(test_error[0])
    MAE_test.append(test_error[1])
    i += 1
```

結果はこちら。

![MSE of each epoch (number of layer is 2).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/32b0b17a-0bb8-8ede-d6e3-d34e1960b6ec.png)
![MSE of each epoch (number of layer is 3).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/0f39f3d6-d54e-d8f5-f626-0e42b1b2daf9.png)
![MSE of each epoch (number of layer is 4).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/f0f1e9c9-4d84-a82e-a75a-ac7ad3721f2f.png)
![MSE of each epoch (number of layer is 5).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/c4b03506-7823-5e36-0604-0df38b7eed13.png)
![MAE of each epoch (number of layer is 2).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/08624fa9-38f4-d9d8-9eb6-cbbb54912396.png)
![MAE of each epoch (number of layer is 3).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/80c0a7c6-e17b-434d-a75e-357cf361262e.png)
![MAE of each epoch (number of layer is 4).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/3b8cb7da-d327-f026-7c72-22ec5b9a19a7.png)
![MAE of each epoch (number of layer is 5).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/ac547745-05eb-c7b1-320e-786e7f90deaa.png)

```
MSE:  [variable(108.787415), variable(100.24213), variable(100.931335), variable(117.44594)]
MAE:  [variable(7.6647296), variable(7.145885), variable(7.118979), variable(7.5313835)]
```

層の数を増やすほど精度は上がると予想していましたがそうではないようです。こちらも、バッチサイズと同じく最終的な誤差はほぼ同じ結果となりました。
また、層の数が増えるほど収束に時間がかかるようです。これは、層の数が増えるほど調整しなければならないパラメータが増えるためだと思われます。
テストデータでの最優秀は4層ですが、20付近で一度誤差が大きくなっていること、また3層の方が収束が早く精度もそう変わらないことから3層が最優秀と考えました。

#### 活性化関数の種類を変更
前回用いたrelu、今回用いたtanh、マイナス値の存在するelu、leaky_relu、rrelu、seluについて調べました。

```py
import Net_box.kind_function as kind_f

run = deep_learn()

f_arr = kind_f.functions_arr
f_name = kind_f.functions_name
run.save_dir = "fig/kind_function"
i = 1
MSE_test = []
MAE_test = []

run.set_data()

for function in f_arr:
    print("\r%d/%d" % (i, len(f_arr)), end="")
    kind_f.func = function
    run.Net = kind_f.Net
    run.fig_name1 = "MSE of each epoch (function is %s)" % f_name[i - 1]
    run.fig_name2 = "MAE of each epoch (function is %s)" % f_name[i - 1]
    run.fit()
    run.plot()
    test_error = run.cal_test_error()
    MSE_test.append(test_error[0])
    MAE_test.append(test_error[1])
    i += 1
```

結果は、reluとtanh以外nanになりました。

![MSE of each epoch (function is relu).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/684c9fb8-a22b-b499-2173-dc1a32dc0bf5.png)
![MSE of each epoch (function is tanh).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/73329b1a-26ab-0ac4-c227-1458db68bb66.png)
![MAE of each epoch (function is relu).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/b1c480fc-45df-a057-0388-d6294a996c45.png)
![MAE of each epoch (function is tanh).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/2c3ed758-6557-0ad4-6d21-135dd12b112d.png)

```
['relu', 'tanh', 'elu', 'leaky_relu', 'rrelu', 'selu']
MSE:  [variable(555.87366), variable(204.87903), variable(nan), variable(nan), variable(nan), variable(nan)]
MAE:  [variable(19.49682), variable(9.199303), variable(nan), variable(nan), variable(nan), variable(nan)]
```

reluとtanh以外はデータが取れず、reluは全く学習していないことがグラフからも分かります。
消去法でtanhを選ぶべきでしょう。

#### 学習率を変更

```py
run = deep_learn()
run.Net = plane.Net

lr_arr = [0.0001, 0.0005, 0.001, 0.005, 0.01, 0.025, 0.05, 0.1, 0.2, 0.3]
run.save_dir = "fig/lr"
i = 1
MSE_test = []
MAE_test = []

run.set_data()

for lr in lr_arr:
    print("\r%d/%d" % (i, len(lr_arr)), end="")
    run.lr = lr
    run.fig_name1 = "MSE of each epoch (lr is %f).png" % lr
    run.fig_name2 = "MAE of each epoch (lr is %f).png" % lr
    run.fit()
    run.plot()
    test_error = run.cal_test_error()
    MSE_test.append(test_error[0])
    MAE_test.append(test_error[1])
    i += 1
```

![MSE of each epoch (lr is 0.000100).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/9909198e-be9c-8e6f-ac42-5d4922b9a172.png)
![MSE of each epoch (lr is 0.000500).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/cd4ca38b-7132-009d-fe31-2899dbabfe27.png)
![MSE of each epoch (lr is 0.001000).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/f10afdb2-960d-f470-2d4f-58f88668e3a4.png)
![MSE of each epoch (lr is 0.005000).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/6443df40-072f-9c02-ea3d-5904e2baa8c4.png)
![MSE of each epoch (lr is 0.010000).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/11963fb2-803d-54cf-2780-4acc96388905.png)
![MSE of each epoch (lr is 0.025000).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/8df3da85-dcfe-2333-dde1-01bdb2111c17.png)
![MSE of each epoch (lr is 0.050000).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/045d333c-f721-8cd4-b2e9-1a1a2c57a222.png)
![MSE of each epoch (lr is 0.100000).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/b3aab5db-bd5e-6b86-27e4-2aa2364e475b.png)
![MSE of each epoch (lr is 0.200000).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/b5445ce2-6a3a-bf00-7092-d587c37b2e31.png)
![MSE of each epoch (lr is 0.300000).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/947c01cf-4ad8-058d-fe83-80d86326f728.png)

![MAE of each epoch (lr is 0.000100).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/c60eab87-8765-4ee3-0e8f-ca77c39447e6.png)
![MAE of each epoch (lr is 0.000500).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/55913b54-a755-364e-0eff-cb9485a55da4.png)
![MAE of each epoch (lr is 0.001000).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/23db01af-a554-4c99-49d8-b462ae2e3709.png)
![MAE of each epoch (lr is 0.005000).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/adfd53a8-ad2b-49bf-87a7-c0755c0e0d69.png)
![MAE of each epoch (lr is 0.010000).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/1b7b62e0-bfff-0e38-e0d5-eabe3cb79d2c.png)
![MAE of each epoch (lr is 0.025000).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/bbc8a37b-8184-2642-b6d3-48b61af54dd5.png)
![MAE of each epoch (lr is 0.050000).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/a3c76e95-499d-131c-576c-15655fc5bdc7.png)
![MAE of each epoch (lr is 0.100000).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/6f4d8a07-ecf4-db2f-a749-b2531cfe5668.png)
![MAE of each epoch (lr is 0.200000).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/f885d2b3-4b32-b644-d7e1-113f2057bcf7.png)
![MAE of each epoch (lr is 0.300000).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/c73680b6-6735-89f5-f3b0-893cb21b1ebd.png)

```
MSE:  [variable(170.00241), variable(171.05908), variable(181.1228), variable(192.46042), variable(161.02898), variable(163.08765), variable(170.65488), variable(177.69069), variable(170.5684), variable(163.26263)]
MAE:  [variable(8.892699), variable(8.849699), variable(9.052602), variable(9.407764), variable(8.696438), variable(8.609232), variable(8.800114), variable(8.914179), variable(8.9228115), variable(8.645447)]
```

最終結果に大きな差異はなかったですが、0.3については30以降、確認データの誤差が大きくなっているのが気になりました。
0.01が最優秀でした。

#### 最適化関数を変更
URLに載っていた最適化関数の中で、chainerで実装されていたものをすべて試してみました。

[URL](https://qiita.com/ZoneTsuyoshi/items/8ef6fa1e154d176e25b8)

```py
# https://qiita.com/ZoneTsuyoshi/items/8ef6fa1e154d176e25b8

from chainer import optimizers

opt_arr = [
    optimizers.SGD(),
    optimizers.MomentumSGD(),
    optimizers.AdaGrad(),
    optimizers.RMSprop(),
    optimizers.AdaDelta(),
    optimizers.Adam(),
    optimizers.RMSpropGraves(),
    optimizers.SMORMS3(),
    optimizers.AMSGrad(),
    optimizers.AdaBound(),
    optimizers.AMSBound()
]

opt_name = [
    "SGD",
    "MomentumSGD",
    "AdaGrad",
    "RmSprop",
    "AdaDelta",
    "Adam",
    "RMSpropGraves",
    "SMORMS3",
    "AMSGrad",
    "AdaBound",
    "AMSBound"
]

run = deep_learn()
run.Net = plane.Net

run.save_dir = "fig/optimizer"
i = 1
MSE_test = []
MAE_test = []

run.set_data()

for opt in opt_arr:
    print("\r%d/%d" % (i, len(opt_arr)), end="")
    run.optimizer = opt
    run.fig_name1 = "MSE of each epoch (optimizer is %s)" % opt_name[i - 1]
    run.fig_name2 = "MAE of each epoch (optimizer is %s)" % opt_name[i - 1]
    run.fit()
    run.plot()
    test_error = run.cal_test_error()
    MSE_test.append(test_error[0])
    MAE_test.append(test_error[1])
    i += 1
```

![MSE of each epoch (optimizer is SGD).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/6db6d7c5-f38a-b583-241e-3f35b9081032.png)
![MSE of each epoch (optimizer is MomentumSGD).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/4be06f60-45ad-c66f-a987-f48b32ad42ea.png)
![MSE of each epoch (optimizer is AdaGrad).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/e47e6e5d-8de4-d9cf-73a9-d135de03ac0c.png)
![MSE of each epoch (optimizer is RmSprop).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/0c0d4233-a6c0-526b-58d7-56fd5a2307d7.png)
![MSE of each epoch (optimizer is AdaDelta).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/00ef018d-cae2-8904-0d15-5df164b8e7ae.png)
![MSE of each epoch (optimizer is Adam).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/aa30b2c9-d66c-00ea-2248-7cd93decade2.png)
![MSE of each epoch (optimizer is RMSpropGraves).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/b3da980a-3735-e9c9-b88d-532f81ce61a1.png)
![MSE of each epoch (optimizer is SMORMS3).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/41d4ac74-0188-9fc5-191a-072d1b94573c.png)
![MSE of each epoch (optimizer is AMSBound).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/86b85a55-de5b-e97e-a1ab-a25b3c1898c7.png)
![MSE of each epoch (optimizer is AdaBound).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/6a308371-f85e-e33d-dffc-0aa648bf5030.png)
![MSE of each epoch (optimizer is AMSGrad).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/1546789e-ecf9-c565-5791-c5cc961c9ef6.png)

![MAE of each epoch (optimizer is SGD).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/ad64c158-2050-ded8-15cc-03f02b631f26.png)
![MAE of each epoch (optimizer is MomentumSGD).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/840539f4-3d78-5409-b913-a506dc61ee35.png)
![MAE of each epoch (optimizer is AdaGrad).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/1da2409a-6e64-b877-11d7-082f2a86c24f.png)
![MAE of each epoch (optimizer is RmSprop).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/1c68d811-123a-6e39-8d77-03651bc57803.png)
![MAE of each epoch (optimizer is AdaDelta).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/f4a38170-e831-8f39-5292-95886b9705c1.png)
![MAE of each epoch (optimizer is Adam).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/2cb6b4b7-f2fe-ab3f-d823-39e04e1da358.png)
![MAE of each epoch (optimizer is RMSpropGraves).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/ca634a04-2f24-1b40-e716-cc414cfb9504.png)
![MAE of each epoch (optimizer is SMORMS3).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/14d8f43f-c337-cb8e-abf5-ec0727b39fc7.png)
![MAE of each epoch (optimizer is AMSGrad).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/73676ef8-3854-febc-ebbf-ce3b0615e414.png)
![MAE of each epoch (optimizer is AdaBound).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/9f1ada5e-d85d-464e-4737-b6f525be9d71.png)
![MAE of each epoch (optimizer is AMSBound).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/95b5733d-f048-79b7-5874-dcc87f431cc7.png)

```
['SGD', 'MomentumSGD', 'AdaGrad', 'RmSprop', 'AdaDelta', 'Adam', 'RMSpropGraves', 'SMORMS3', 'AMSGrad', 'AdaBound', 'AMSBound']
MSE:  [variable(140.35452), variable(351.79584), variable(362.52658), variable(130.66309), variable(105.02744), variable(230.22269), variable(200.24446), variable(137.35747), variable(74.48441), variable(104.179436), variable(163.56358)]
MAE:  [variable(7.8985395), variable(15.806046), variable(14.46501), variable(7.234192), variable(6.230004), variable(10.566997), variable(10.3872595), variable(8.752416), variable(6.492539), variable(5.505145), variable(8.154634)]
```

MomentumSGDは誤差もあり得ないほど大きいし学習も全くしていない尖った結果になりました。SGDは問題なく学習できているので、この場合一昔前の勾配情報は必要ない、むしろ悪手であると考えられます。またMomentumSGDのグラフを見ると波打っていること、また盤面から最終結果を予測するという今回の課題から、評価関数の谷は非常にたくさんあると考えられます。
AdaGradも個性的な結果になりました。各変数ごとに勾配に応じて重みを更新するアルゴリズムですが、非常にゆっくり学習していることから、評価関数には非常に深い谷が存在する可能性もあると考えました。
RmSpropのグラフは大きな山が複数できる結果になりました。これはある程度過去の勾配情報は無視するアルゴリズムなのですが、それが(山を除き)SGDに近い形のグラフを作っているということは、やはり評価関数には大きな谷があるということでしょうか。よく分かりません。
AdaDelta、Adam、RMSpropGraves、SMORMS3、AMSGrad、AdaBound、AMSBoudnは順調に学習できていることが分かります。
AdaDeltaはRMSpropの次元数を合わせたものですが、その考え方は有効であることがここから分かります。
Adamは綺麗に収束していますが、最終的な誤差はいまいちでした。
テストデータで最優秀、かつ収束も綺麗で早かったAdaBoundを採用しようと思います。

#### 重み減衰係数を変更

```py
run = deep_learn()
run.Net = plane.Net

WD_arr = [0.000001, 0.000005, 0.00001, 0.00002, 0.00005, 0.0001, 0.0005, 0.001]
run.save_dir = "fig/WeightDecay"
i = 1
MSE_test = []
MAE_test = []

run.set_data()

for WD in WD_arr:
    print("\r%d/%d" % (i, len(WD_arr)), end="")
    run.WeightDecay = WD
    run.fig_name1 = "MSE of each epoch (coefficient of WeiteDecay is %f).png" % WD
    run.fig_name2 = "MAE of each epoch (coefficient of WeiteDecay is %f).png" % WD
    run.fit()
    run.plot()
    test_error = run.cal_test_error()
    MSE_test.append(test_error[0])
    MAE_test.append(test_error[1])
    i += 1
```

![MSE of each epoch (coefficient of WeiteDecay is 0.000001).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/4cbc2c93-19fb-3820-3fd0-d7809c23c56b.png)
![MSE of each epoch (coefficient of WeiteDecay is 0.000005).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/90d4b7f7-9e1c-97ae-5db2-be2528915ec6.png)
![MSE of each epoch (coefficient of WeiteDecay is 0.000010).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/22074494-146b-7cb4-c3c6-857dc261e705.png)
![MSE of each epoch (coefficient of WeiteDecay is 0.000020).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/231746f7-5480-71d3-bd55-b31480774e16.png)
![MSE of each epoch (coefficient of WeiteDecay is 0.000050).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/ccef09a5-28fd-e119-29aa-4e9a434318be.png)
![MSE of each epoch (coefficient of WeiteDecay is 0.000100).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/59645025-2bcb-527b-7a5f-5ffa21414c9f.png)
![MSE of each epoch (coefficient of WeiteDecay is 0.000500).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/897d36d7-cebb-e517-0014-5b1e02d6456e.png)
![MSE of each epoch (coefficient of WeiteDecay is 0.001000).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/4a415fbe-32f0-d44c-4536-b0a910dad285.png)

![MAE of each epoch (coefficient of WeiteDecay is 0.001000).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/ea15dc8b-d78e-996c-89ff-02342774797e.png)
![MAE of each epoch (coefficient of WeiteDecay is 0.000001).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/00f089f3-b17d-78ff-d3b3-d0a91848d566.png)
![MAE of each epoch (coefficient of WeiteDecay is 0.000005).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/ecb0f4aa-5eff-219a-43d4-72bf3a69a602.png)
![MAE of each epoch (coefficient of WeiteDecay is 0.000010).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/8e36feda-1682-f8fd-5ead-b32b8a1d79b4.png)
![MAE of each epoch (coefficient of WeiteDecay is 0.000020).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/90de93c2-83fa-c2b0-22a5-89547e8d1159.png)
![MAE of each epoch (coefficient of WeiteDecay is 0.000050).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/8d55df0c-a16e-5a3a-77c5-91c6be55b605.png)
![MAE of each epoch (coefficient of WeiteDecay is 0.000100).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/470c09f7-a448-baf9-3316-f8e2d4fdf257.png)
![MAE of each epoch (coefficient of WeiteDecay is 0.000500).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/8ec2dfe1-f75e-b5c1-9979-02e412e3daf8.png)

```
MSE:  [variable(323.6231), variable(337.4172), variable(317.3086), variable(345.4895), variable(360.81476), variable(347.40695), variable(317.54373), variable(336.22287)]
MAE:  [variable(11.564267), variable(11.702841), variable(11.491479), variable(12.228395), variable(12.072598), variable(11.99491), variable(11.335259), variable(11.767622)]
```

どれもあまり大きな違いはない結果になりました。
テストデータでの最優秀は0.00001でした。

#### 更新回数を変更
変更と書きましたが、実際は行けるところまで学習させてみただけです。

```py
run = deep_learn()
run.Net = plane.Net

run.save_dir = "fig/n_epoch"

run.set_data()

run.n_epoch = 1000
run.fig_name1 = "MSE of each epoch (n_epohc is %d)" % run.n_epoch
run.fig_name2 = "MAE of each epoch (n_epoch is %d)" % run.n_epoch

run.fit()
run.plot()
MSE_test, MAE_test = run.cal_test_error()
```

![MSE of each epoch (n_epohc is 1000).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/b5e3b794-fdf3-63a1-10b6-7e8b4c19eaf4.png)
![MAE of each epoch (n_epoch is 1000).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/dab4e46a-7fa3-7ba3-bcc4-afc74d627693.png)

```
MSE:  variable(173.23218)
MAE:  variable(8.805884)
```

1000回もする必要はさすがになかったようです。
今まで通り50回でも十分な精度は出ます。
精度検証は50回で行い、本番の学習は念のため100回で行こうと思います。

#### データ集めの際のターン数を変更
完全に失念していました。
check_pointについて、変更しながらデータ集めをし学習させてみます。

```py
import osero_learn as ol
import deep_learn as dl

run = dl.deep_learn()
run.Net = plane.Net

run.save_dir = "fig/check_point"

cp_arr = [
    [i for i in range(1, 61)],
    [i for i in range(5, 61, 5)],
    [i for i in range(10, 61, 10)]
]
cp_name = [
    "every 1",
    "every 5",
    "every 10"
]
i = 1
MSE_test = []
MAE_test = []

for cp in cp_arr:
    print("\r%d/%d" % (i, len(cp_arr)), end="")
    run.osero = ol.learn(0, 0, cp)
    run.set_data()
    run.fig_name1 = "MSE of each epoch (turn is %s)" % cp_name[i - 1]
    run.fig_name2 = "MAE of each epoch (turn is %s)" % cp_name[i - 1]
    run.fit()
    run.plot()
    test_error = run.cal_test_error()
    MSE_test.append(test_error[0])
    MAE_test.append(test_error[1])
    i += 1
```

実行結果はこちら。

![MSE of each epoch (turn is every 1).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/667f85f9-dc09-7205-f511-13b3547a0113.png)
![MSE of each epoch (turn is every 5).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/492c93af-6512-e223-d631-3f418df51184.png)
![MSE of each epoch (turn is every 10).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/a94ef8e1-7cd0-c947-d009-8e0ad98a013d.png)

![MAE of each epoch (turn is every 1).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/181e174b-59d0-2a0b-c45f-1a4b122a2fab.png)
![MAE of each epoch (turn is every 5).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/da60671d-94fa-e054-2487-db7fa30bc139.png)
![MAE of each epoch (turn is every 10).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/c02dcbd0-ed6d-4c9b-5221-1634462afa6a.png)

```
['every 1', 'every 5', 'every 10']
MSE:  [variable(134.67699), variable(360.08517), variable(555.0388)]
MAE:  [variable(7.625587), variable(15.11074), variable(18.970434)]
```

言うまでもなくevery 1が最適ですね。

#### 重み減衰を変更
こちらも失念しておりました。
chainerに実装されているものをすべて試すつもりでしたが、GradientClippingのみなぜかエラーが出て実行できなかったため試せていません。
引数については大体で設定しました。

```py
import chainer.optimizer_hooks as hf

run = deep_learn()
run.Net = plane.Net

run.save_dir = "fig/hook_function"

hf_arr = [
    hf.WeightDecay(0.00001),
    hf.Lasso(0.00001),
    # hf.GradientClipping(1.0),
    hf.GradientHardClipping(-1.0, 1.0),
    hf.GradientNoise(0.3),
    hf.GradientLARS()
]
hf_name = [
    "WeightDecay",
    "Lasso",
    # "GradientClippint",
    "GradientHardClipping",
    "GradientNoise",
    "GradientLARS"
]
i = 1
MSE_test = []
MAE_test = []

run.set_data()

for hook_f in hf_arr:
    print("\r%d/%d" % (i, len(hf_arr)), end="")
    run.hook_f = hook_f
    run.fig_name1 = "MSE of each epoch (hook function is %s)" % hf_name[i - 1]
    run.fig_name2 = "MAE of each epoch (hook function is %s)" % hf_name[i - 1]
    run.fit()
    run.plot()
    test_error = run.cal_test_error()
    MSE_test.append(test_error[0])
    MAE_test.append(test_error[1])
    i += 1
```

![MSE of each epoch (hook function is WeightDecay).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/68b08a3d-4bc3-289a-d9c5-8801905763b4.png)
![MSE of each epoch (hook function is Lasso).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/a8e772af-81c9-e3c1-214f-1b279be6258b.png)
![MSE of each epoch (hook function is GradientHardClipping).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/b6944b79-76b6-ea1f-90e6-c32802f5ee3c.png)
![MSE of each epoch (hook function is GradientNoise).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/880bc6cf-9595-cf74-cd91-f8005e638d9d.png)
![MSE of each epoch (hook function is GradientLARS).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/e16c8be6-5052-a15a-90d5-c604663d92e5.png)

![MAE of each epoch (hook function is WeightDecay).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/a184ff07-d07e-3b30-9d0f-f372d4b5618e.png)
![MAE of each epoch (hook function is Lasso).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/8998bc9c-c2ff-114f-70a3-136d43631102.png)
![MAE of each epoch (hook function is GradientHardClipping).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/11019d29-9e1c-e465-2066-27379e94949f.png)
![MAE of each epoch (hook function is GradientNoise).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/5ba60c6d-f98c-8cad-5a6d-9686192512a2.png)
![MAE of each epoch (hook function is GradientLARS).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1925706/b279ac1f-2aab-cfee-695c-7043573f72a3.png)

```
['WeightDecay', 'Lasso', 'GradientHardClipping', 'GradientNoise', 'GradientLARS']
MSE:  [variable(173.38802), variable(165.7673), variable(149.28122), variable(170.66235), variable(334.34393)]
MAE:  [variable(8.827514), variable(8.926463), variable(8.31733), variable(8.63603), variable(13.599926)]
```

WeightDecayとLasso以外はネット上に全く情報がなく、考察のしようがないです。
とりあえずGradientLARSはうまく学習できていないことしか分かりません。
テストデータでの誤差が小さく、収束もまだ余裕がありそうに見えるGradientHardClippingを採用したいと思います。

### まとめ

今回の実験で分かった、オセロ学習において有効であろうパラメータを以下に示します。

* データ量　5x5の25試合、全ターンを記録
* バッチサイズ　150
* 層の数　3
* 活性化関数　tanh
* 学習率　0.01
* 最適化関数　AdaBound
* 重み減衰　GradientHardClipping
* 更新回数　50回で十分、本番に使用するモデルは100回

重み減衰係数については、WeightDecayの時のみ有効なデータなのでここには書きません。
また、記事が長くなってきたので、GradientHardClippingのパラメータやこれらを組み合わせての実験は次回行いたいと思います。

# フルバージョン

https://github.com/TTaisei/osero/tree/main/deep_learn
# 次回は
前述した通り、GradientHardClippingのパラメータを変更しての実験およびまとめに記した条件を組み合わせての実験を行います。

[次回](https://qiita.com/tt_and_tk/items/a95c5632be0a1f643408)

https://qiita.com/tt_and_tk/items/068f2afde6db637e189f