第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 , its definition is
英語のヒント
識別器は、活性化関数として漏れのあるReLU(leaky ReLU)を使う点を除けば、ふつうの畳み込みネットワークである。が与えられたとき、その定義は次のとおりである。
(20.2.2)
As it can be seen, it is normal ReLU if , and an identity function if . For , 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.
英語のヒント
見てのとおり、ならふつうのReLUであり、なら恒等関数である。では、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 , with a kernel shape , a stride shape , and a padding shape , the output shape will be:
英語のヒント
7.3節で示したように、既定の設定の基本ブロックは入力の幅と高さを半分にする。たとえば、入力の形状、カーネルの形状、ストライドの形状、パディングの形状が与えられると、出力の形状は次のようになる。
(20.2.3)
x = torch.zeros((2, 3, 16, 16))
d_blk = D_block(20)
d_blk(x).shapetorch.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 as the last layer to obtain a single prediction value.
英語のヒント
最後の層として、出力チャネルがの畳み込み層を使い、一つの予測値を得る。
x = torch.zeros((1, 3, 64, 64))
net_D(x).shapetorch.Size([1, 1, 1, 1])