第20章敵対的生成ネットワーク
20.2.2. The Generator20.2.2 生成器
The generator needs to map the noise variable , a length- vector, to a RGB image with width and height to be . In Section 14.11 we introduced the fully convolutional network that uses transposed convolution layer (refer to Section 14.10) to enlarge input size. The basic block of the generator contains a transposed convolution layer followed by the batch normalization and ReLU activation.
英語のヒント
class G_block(nn.Module):
def __init__(self, out_channels, in_channels=3, kernel_size=4, strides=2,
padding=1, **kwargs):
super(G_block, self).__init__(**kwargs)
self.conv2d_trans = nn.ConvTranspose2d(in_channels, out_channels,
kernel_size, strides, padding, bias=False)
self.batch_norm = nn.BatchNorm2d(out_channels)
self.activation = nn.ReLU()
def forward(self, X):
return self.activation(self.batch_norm(self.conv2d_trans(X)))In default, the transposed convolution layer uses a kernel, a strides, and a padding. With a input shape of , the generator block will double input’s width and height.
英語のヒント
既定では、転置畳み込み層はのカーネル、のストライド、のパディングを使う。入力の形状がなら、生成器のブロックは入力の幅と高さを2倍にする。
(20.2.1)
x = torch.zeros((2, 3, 16, 16))
g_blk = G_block(20)
g_blk(x).shapetorch.Size([2, 20, 32, 32])If changing the transposed convolution layer to a kernel, strides and zero padding. With a input size of , the output will have its width and height increased by 3 respectively.
英語のヒント
転置畳み込み層を、のカーネル、のストライド、パディングなしに変えたとする。入力の大きさがなら、出力の幅と高さはそれぞれ3だけ増える。
x = torch.zeros((2, 3, 1, 1))
g_blk = G_block(20, strides=1, padding=0)
g_blk(x).shapetorch.Size([2, 20, 4, 4])The generator consists of four basic blocks that increase input’s both width and height from 1 to 32. At the same time, it first projects the latent variable into channels, and then halve the channels each time. At last, a transposed convolution layer is used to generate the output. It further doubles the width and height to match the desired shape, and reduces the channel size to . The tanh activation function is applied to project output values into the range.
英語のヒント
生成器は、入力の幅と高さの両方を1から32に増やす四つの基本ブロックからなる。同時に、まず潜在変数をチャネルに射影し、それから毎回チャネルを半分にする。最後に、転置畳み込み層を使って出力を生成する。これはさらに幅と高さを2倍にして目的のの形状に合わせ、チャネルの大きさをに減らす。出力の値をの範囲に射影するために、tanh活性化関数を当てはめる。
n_G = 64
net_G = nn.Sequential(
G_block(in_channels=100, out_channels=n_G*8,
strides=1, padding=0), # Output: (64 * 8, 4, 4)
G_block(in_channels=n_G*8, out_channels=n_G*4), # Output: (64 * 4, 8, 8)
G_block(in_channels=n_G*4, out_channels=n_G*2), # Output: (64 * 2, 16, 16)
G_block(in_channels=n_G*2, out_channels=n_G), # Output: (64, 32, 32)
nn.ConvTranspose2d(in_channels=n_G, out_channels=3,
kernel_size=4, stride=2, padding=1, bias=False),
nn.Tanh()) # Output: (3, 64, 64)Generate a 100 dimensional latent variable to verify the generator’s output shape.
英語のヒント
100次元の潜在変数を生成して、生成器の出力の形状を確かめる。
x = torch.zeros((1, 100, 1, 1))
net_G(x).shapetorch.Size([1, 3, 64, 64])