python - 我应该在 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),
)
解决方案
您的模型初始化对我来说看起来不错,只需编写 forward 函数来调用定义的操作__init__
,您就应该一切就绪。
至于关于可学习参数的第二个问题,ReLU 和 Leaky ReLU 只是执行预定义操作的激活函数。例如,ReLU 定义为max(0,x)
,因此它只是输出两个输入的最大值。虽然 ReLU 和 Leaky ReLU 都没有可学习的参数,但 Leaky ReLU 确实有一个超参数,即negative_slope
,从它的函数定义来看max(0, x) + negative_slope * min(0,x)
。
推荐阅读
- python - 为什么 SymPy 没有为我的方程找到任何解?
- apache - Apache 限制:记分牌已满,不在 MaxRequestWorkers.Increase ServerLimit
- python - 终端无法从 https://pypi.python.org/simple/sh/ 下载
- flutter - 制作圆形边框装饰
- android - 我的应用程序的用户 ID 评论部分正在检索覆盖各处的 Fire-base 存储链接,并且评论员的图像未显示
- java - 如何将 Oracle 日期格式转换为 Java?
- pandas - 如何在 spark sql 查询中使用熊猫列表
- google-apps-script - 抱歉...关于表单提交触发器的另一个问题
- javascript - 销毁创建的未添加到 DOM 的 HTMLElement
- c# - 使用 Linq 构建和过滤笛卡尔积集合(自定义对象)