読者です 読者をやめる 読者になる 読者になる

TadaoYamaokaの日記

山岡忠夫 Home で公開しているプログラムの開発ネタを中心に書いていきます。

Chainerで位置ごとに異なるバイアスを定義する

前回の日記で、AlphaGoのSL policy networkをChainerで定義した際に、layer13の位置ごとに異なるバイアスを、(19*19)次元のベクトルとして、Convolution2Dを1次元にreshapeした後、足し合わせていたが、ミニバッチにした場合うまくいかないことが分かった。

Variable同士の和は、shapeが同じでないとエラーになる。
ミニバッチにした場合は、shapeが異なってしまうためエラーとなる。

そこで、FunctionとLinkを独自に定義して、バイアスのみ足す関数を定義した。
Chainerのfunctions.linearとlinks.Linearのソースを元に、パラメータWの処理を削除しただけである。

※7/9追記:ChainerにBias Linkが追加されました。最新のChainerでは下記のようにBias関数を定義する必要がありません。

バイアスのみ足す関数
from chainer import function, link

# bias
def _as_mat(x):
    if x.ndim == 2:
        return x
    return x.reshape(len(x), -1)

class MyBiasFunction(function.Function):

    def forward(self, inputs):
        x = _as_mat(inputs[0])
        b = inputs[1]
        y = x + b
        return y,

    def backward(self, inputs, grad_outputs):
        x = _as_mat(inputs[0])
        gy = grad_outputs[0]

        gx = gy.reshape(inputs[0].shape)
        gb = gy.sum(0)
        return gx, gb

class MyBias(link.Link):

    def __init__(self, size, bias=0, initial_bias=None):
        super(MyBias, self).__init__()
        self.add_param('b', size)
        if initial_bias is None:
            initial_bias = bias
        self.b.data[...] = initial_bias

    def __call__(self, x):
        return MyBiasFunction()(x, self.b)


これを使って、前回のコードをミニバッチに対応させると以下のようになる。

ミニバッチに対応させたSL policy network
import numpy as np
import chainer
from chainer import cuda, Function, Variable, optimizers, function, link
from chainer import Link, Chain
import chainer.functions as F
import chainer.links as L

# bias
def _as_mat(x):
    if x.ndim == 2:
        return x
    return x.reshape(len(x), -1)

class MyBiasFunction(function.Function):

    def forward(self, inputs):
        x = _as_mat(inputs[0])
        b = inputs[1]
        y = x + b
        return y,

    def backward(self, inputs, grad_outputs):
        x = _as_mat(inputs[0])
        gy = grad_outputs[0]

        gx = gy.reshape(inputs[0].shape)
        gb = gy.sum(0)
        return gx, gb

class MyBias(link.Link):

    def __init__(self, size, bias=0, initial_bias=None):
        super(MyBias, self).__init__()
        self.add_param('b', size)
        if initial_bias is None:
            initial_bias = bias
        self.b.data[...] = initial_bias

    def __call__(self, x):
        return MyBiasFunction()(x, self.b)


feature_num = 2
k = 192
model = Chain(
    layer1=L.Convolution2D(in_channels = feature_num, out_channels = k, ksize = 5, pad = 2),
    layer2=L.Convolution2D(in_channels = k, out_channels = k, ksize = 3, pad = 1),
    layer3=L.Convolution2D(in_channels = k, out_channels = k, ksize = 3, pad = 1),
    layer4=L.Convolution2D(in_channels = k, out_channels = k, ksize = 3, pad = 1),
    layer5=L.Convolution2D(in_channels = k, out_channels = k, ksize = 3, pad = 1),
    layer6=L.Convolution2D(in_channels = k, out_channels = k, ksize = 3, pad = 1),
    layer7=L.Convolution2D(in_channels = k, out_channels = k, ksize = 3, pad = 1),
    layer8=L.Convolution2D(in_channels = k, out_channels = k, ksize = 3, pad = 1),
    layer9=L.Convolution2D(in_channels = k, out_channels = k, ksize = 3, pad = 1),
    layer10=L.Convolution2D(in_channels = k, out_channels = k, ksize = 3, pad = 1),
    layer11=L.Convolution2D(in_channels = k, out_channels = k, ksize = 3, pad = 1),
    layer12=L.Convolution2D(in_channels = k, out_channels = k, ksize = 3, pad = 1),
    layer13=L.Convolution2D(in_channels = k, out_channels = 1, ksize = 1, nobias = True),
    layer13_2=MyBias(19*19))

model.to_gpu()

optimizer = optimizers.SGD()
optimizer.setup(model)

data1 = [[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,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[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]]

board_data = np.array([data1, data2], dtype=np.float32)

board = Variable(cuda.to_gpu(board_data))

t = Variable(cuda.to_gpu(np.array([5, 5])))

def forward_backward(x, t):
    z1 = F.relu(model.layer1(x))
    z2 = F.relu(model.layer2(z1))
    z3 = F.relu(model.layer3(z2))
    z4 = F.relu(model.layer4(z3))
    z5 = F.relu(model.layer5(z4))
    z6 = F.relu(model.layer6(z5))
    z7 = F.relu(model.layer7(z6))
    z8 = F.relu(model.layer8(z7))
    z9 = F.relu(model.layer9(z8))
    z10 = F.relu(model.layer10(z9))
    z11 = F.relu(model.layer11(z10))
    z12 = F.relu(model.layer12(z11))
    u13 = model.layer13(z12)

    u13_1d = model.layer13_2(F.reshape(u13, (len(u13.data), 19*19)))

    y13 = F.softmax(u13_1d)

    loss = F.softmax_cross_entropy(u13_1d, t)
    print(loss.data)

    loss.backward()

for i in range(10):
    optimizer.zero_grads()
    forward_backward(board, t)
    optimizer.update()