优化一下下面的模型# 生成器输入100噪声输出12828class GeneratornnModule    def init__self        superGenerator self__init        selflinear = nnSequential            nnLinear100 256            nnTanh            nnLi 优化后的模型如下:

生成器,输入100噪声输出(1,28,28)

class Generator(nn.Module): def init(self): super(Generator, self).init() self.linear = nn.Sequential( nn.Linear(100, 256), nn.BatchNorm1d(256), nn.ReLU(), nn.Linear(256, 512), nn.BatchNorm1d(512), nn.ReLU(), nn.Linear(512, 28*28), nn.Tanh() )

def forward(self, x):
    x = self.linear(x)
    x = x.view(-1, 28, 28)
    return x

辨别器,输入(1,28,28),输出真假,推荐使用LeakRelu

class Discriminator(nn.Module): def init(self): super(Discriminator, self).init() self.linear = nn.Sequential( nn.Linear(28*28, 512), nn.LeakyReLU(0.2), nn.Dropout(0.3),

        nn.Linear(512, 256),
        nn.LeakyReLU(0.2),
        nn.Dropout(0.3),

        nn.Linear(256, 1),
        nn.Sigmoid()
    )

def forward(self, x):
    x = x.view(-1, 28*28)
    x = self.linear(x)
    return x

优化包括:

  1. 在生成器中添加BatchNormalization层和ReLU激活函数,使得生成的图像更加平滑和真实。

  2. 在辨别器中添加Dropout层,防止过拟合。

  3. 在LeakyReLU激活函数中加入负斜率,使得模型更加稳定。

优化一下下面的模型# 生成器输入100噪声输出12828class GeneratornnModule def __init__self superGenerator self__init__ selflinear = nnSequential nnLinear100 256 nnTanh nnLi

原文地址: https://www.cveoy.top/t/topic/dvGk 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录