python - 如何在pytorch中分离最后一层深层网络?
问题描述
我的深层网络是:
self.actor = nn.Sequential(
nn.Linear(state_dim, 256),
nn.Softplus(),
nn.Linear(256, 256),
nn.Softplus(),
nn.Linear(256, action_dim),
nn.Softplus())
现在,我希望网络提供两个单独的输出,如下所示:
也就是说,只有网络的最后一层不同,可能最后一层的激活函数不同。我应该如何更改上面的代码?
解决方案
您要构建的模型不再是连续的,因为最后有两个并行分支。您可以保留公共主干并使用两个额外的单独层分开。就像是:
class Model(nn.Module):
def __init__(self):
super.__init__()
self.actor = nn.Sequential(
nn.Linear(state_dim, 256),
nn.Softplus(),
nn.Linear(256, 256),
nn.Softplus())
self.outA = nn.Sequential(
nn.Linear(256, action_dim),
nn.Softplus())
self.outB = nn.Sequential(
nn.Linear(256, action_dim),
nn.Softplus())
def forward(self, x):
features = self.actor(x)
return self.outA(features), self.outB(features)
推荐阅读
- angular - Angular - 如何在重定向后显示路由的初始名称
- python - Python的`符号是什么
- python - 在 groupby Pandas 中获得连胜
- simulink - pyfmi 失败,Stepsize 必须是非负数且可被 0.2' 整除
- java - 在 JSON 中使用变量并在 POJO 中基于节进行反序列化
- postgresql - 在 Postgres 13 中为高更新表调整 FILLFACTOR
- r - 如果存在下标越界错误,如何自动删除 R 中的矩阵列?
- c++ - 共享库依赖实际so文件
- django - Django 模型管理员导入导出数据
- excel - HLOOKUP 替代方案,无需更改表或添加行?