プリンキピア

第20章敵対的生成ネットワーク

20.2.3. Discriminator20.2.3 識別器

The discriminator is a normal convolutional network network except that it uses a leaky ReLU as its activation function. Given α∈[0,1], its definition is
英語のヒント
識別器は、活性化関数として漏れのあるReLU(leaky ReLU)を使う点を除けば、ふつうの畳み込みネットワークである。α∈[0,1]が与えられたとき、その定義は次のとおりである。
leaky ReLU(x)={xif x>0αxotherwise.(20.2.2)
As it can be seen, it is normal ReLU if α=0, and an identity function if α=1. For α∈(0,1), leaky ReLU is a nonlinear function that give a non-zero output for a negative input. It aims to fix the “dying ReLU” problem that a neuron might always output a negative value and therefore cannot make any progress since the gradient of ReLU is 0.
英語のヒント
見てのとおり、α=0ならふつうのReLUであり、α=1なら恒等関数である。α∈(0,1)では、leaky ReLUは負の入力に対して0でない出力を与える非線形関数である。これは、ニューロンが常に負の値を出力し、ReLUの勾配が0なので何の進歩もできなくなるという「死んだReLU」の問題を直すことを目指している。
alphas = [0, .2, .4, .6, .8, 1]
x = torch.arange(-2, 1, 0.1)
Y = [nn.LeakyReLU(alpha)(x).detach().numpy() for alpha in alphas]
d2l.plot(x.detach().numpy(), Y, 'x', 'y', alphas)
The basic block of the discriminator is a convolution layer followed by a batch normalization layer and a leaky ReLU activation. The hyperparameters of the convolution layer are similar to the transpose convolution layer in the generator block.
英語のヒント
識別器の基本ブロックは、畳み込み層の後にバッチ正規化層とleaky ReLU活性化が続くものである。畳み込み層のハイパーパラメータは、生成器のブロックの転置畳み込み層のものと似ている。
class D_block(nn.Module):
    def __init__(self, out_channels, in_channels=3, kernel_size=4, strides=2,
                padding=1, alpha=0.2, **kwargs):
        super(D_block, self).__init__(**kwargs)
        self.conv2d = nn.Conv2d(in_channels, out_channels, kernel_size,
                                strides, padding, bias=False)
        self.batch_norm = nn.BatchNorm2d(out_channels)
        self.activation = nn.LeakyReLU(alpha, inplace=True)

    def forward(self, X):
        return self.activation(self.batch_norm(self.conv2d(X)))
A basic block with default settings will halve the width and height of the inputs, as we demonstrated in Section 7.3. For example, given a input shape nh=nw=16, with a kernel shape kh=kw=4, a stride shape sh=sw=2, and a padding shape ph=pw=1, the output shape will be:
英語のヒント
7.3節で示したように、既定の設定の基本ブロックは入力の幅と高さを半分にする。たとえば、入力の形状nh=nw=16、カーネルの形状kh=kw=4、ストライドの形状sh=sw=2、パディングの形状ph=pw=1が与えられると、出力の形状は次のようになる。
nh′×nw′=⌊(nh−kh+2ph+sh)/sh⌋×⌊(nw−kw+2pw+sw)/sw⌋=⌊(16−4+2×1+2)/2⌋×⌊(16−4+2×1+2)/2⌋=8×8.(20.2.3)
x = torch.zeros((2, 3, 16, 16))
d_blk = D_block(20)
d_blk(x).shape
torch.Size([2, 20, 8, 8])
The discriminator is a mirror of the generator.
英語のヒント
識別器は生成器を鏡に映したものである。
n_D = 64
net_D = nn.Sequential(
    D_block(n_D),  # Output: (64, 32, 32)
    D_block(in_channels=n_D, out_channels=n_D*2),  # Output: (64 * 2, 16, 16)
    D_block(in_channels=n_D*2, out_channels=n_D*4),  # Output: (64 * 4, 8, 8)
    D_block(in_channels=n_D*4, out_channels=n_D*8),  # Output: (64 * 8, 4, 4)
    nn.Conv2d(in_channels=n_D*8, out_channels=1,
              kernel_size=4, bias=False))  # Output: (1, 1, 1)
It uses a convolution layer with output channel 1 as the last layer to obtain a single prediction value.
英語のヒント
最後の層として、出力チャネルが1の畳み込み層を使い、一つの予測値を得る。
x = torch.zeros((1, 3, 64, 64))
net_D(x).shape
torch.Size([1, 1, 1, 1])