首页 > 解决方案 > 我应该在 torch.nn 中的模型初始化中添加什么?

问题描述

在模型初始化中应该添加什么?LeakyReLU 和 Dropout 在给定模型中是否具有可学习的参数,例如线性层的权重?

class Discriminator(nn.Module):
    def __init__(self):
        super(Discriminator, self).__init__()

        self.label_embedding = nn.Embedding(opt.n_classes, opt.n_classes)
        self.lin= nn.LeakyReLU(0.2, inplace=True)
        print( self.lin.parameters)
        self.model = nn.Sequential(
            nn.Linear(opt.n_classes + int(np.prod(img_shape)), 512),
            nn.LeakyReLU(0.2, inplace=True),
            nn.Linear(512, 512),
            nn.Dropout(0.4),
            nn.LeakyReLU(0.2, inplace=True),
            nn.Linear(512, 512),
            nn.Dropout(0.4),
            nn.LeakyReLU(0.2, inplace=True),
            nn.Linear(512, 1),
        )

标签: pythondeep-learningpytorchdiscriminatorgenerative-adversarial-network

解决方案


您的模型初始化对我来说看起来不错,只需编写 forward 函数来调用定义的操作__init__,您就应该一切就绪。

至于关于可学习参数的第二个问题,ReLU 和 Leaky ReLU 只是执行预定义操作的激活函数。例如,ReLU 定义为max(0,x),因此它只是输出两个输入的最大值。虽然 ReLU 和 Leaky ReLU 都没有可学习的参数,但 Leaky ReLU 确实有一个超参数,即negative_slope,从它的函数定义来看max(0, x) + negative_slope * min(0,x)


推荐阅读