一:卷积神经网络的搭建
class NetG(nn.Module):
'''
生成器定义
'''
def __init__(self, opt):
super(NetG, self).__init__()
ngf = opt.ngf # 生成器feature map数
self.maina = nn.Sequential(
# 输入是一个nz维度的噪声,我们可以认为它是一个1*1*nz的feature map
nn.ConvTranspose2d(opt.nz, ngf * 16, 4, 1, 0, bias=False),
#noises = t.randn(opt.gen_search_num, opt.nz, 1, 1).normal_(opt.gen_mean, opt.gen_std)
nn.BatchNorm2d(ngf * 16),
nn.ReLU(True),
# 上一步的输出形状:(ngf*8) x 4 x 4
#
nn.ConvTranspose2d(ngf * 16, ngf * 10, 4, 2, 1, bias=False),
nn.BatchNorm2d(ngf * 10),
nn.ReLU(True),
# # # 上一步的输出形状: (ngf*4) x 8 x 8
# # #
nn.ConvTranspose2d(ngf * 10, ngf * 8, 4, 3, 1, bias=False),
nn.BatchNorm2d(ngf * 8),
nn.ReLU(True),
# # # 上一步的输出形状: (ngf*2) x 16 x 16
# #
nn.ConvTranspose2d(ngf * 8, ngf*8, 4, 3, 1, bias=False),
nn.BatchNorm2d(ngf*8),
nn.ReLU(True),
# # # 上一步的输出形状:(ngf) x 32 x 32
nn.ConvTranspose2d(ngf * 8, ngf * 6, 4, 2, 1, bias=False),
nn.BatchNorm2d(ngf * 6),
nn.ReLU(True),
# #
nn.ConvTranspose2d(ngf * 6, ngf , 4, 1, 1, bias=False),
nn.BatchNorm2d(ngf ),
nn.ReLU(True),
nn.ConvTranspose2d(ngf, 3, 5, 3, 1, bias=False),
nn.Tanh() # 输出范围 -1~1 故而采用Tanh
# 输出形状:3 x 96 x 96
)
def forward(self, input):
return self.maina(input)
class NetG(nn.Module):
''