python - Torch.nn 有特定的激活函数吗?
问题描述
我的网络有一个带有 Relu 激活函数的输出层,但我希望输出类似于“Relu+1”,即我希望输出都大于 1 并且具有相同形状的 Relu 函数。我应该如何更改我的 torch.nn 网络?我的代码是这样的:
self.actor = nn.Sequential(
nn.Linear(state_dim, 256),
nn.ReLU(),
nn.Linear(256, 256),
nn.ReLU(),
nn.Linear(256, action_dim),
nn.ReLU()
)
解决方案
我能想到的方法有两种。
- 做
self.actor
一个nn.Module
对象
class Actor(nn.Module):
def __int__(self, state_dim, action_dim):
super().__init__()
self.linear1 = nn.Linear(state_dim, 256)
self.relu = nn.ReLU()
self.linear2 = nn.Linear(256, 256)
self.linear3 = nn.Linear(256, action_dim)
def forward(self, x):
x = self.linear1(x)
x = self.relu(x)
x = self.linear2(x)
x = self.relu(x)
x = self.linear2(x)
x = self.relu(x) + 1
return x
class ......
self.actor = Actor(state_dim, action_dim)
- 创建一个
Module
类来执行此操作并将其添加到self.actor
class Add1(nn.Module):
def forward(self, x):
return x + 1
class ......
self.actor = nn.Sequential(
nn.Linear(state_dim, 256),
nn.ReLU(),
nn.Linear(256, 256),
nn.ReLU(),
nn.Linear(256, action_dim),
nn.ReLU(),
Add1()
)
推荐阅读
- javascript - 将输入中的数据保存到文件中,以便以后在 JS 中查看
- soapui - 如何在带有excel文件的sql查询中传递soapui参数?
- python - 运行具有相对导入的模块作为 __main__
- nlp - 如何微调 BERT 来总结文章
- java - 无法在 main.java (JavaFX) 中添加 Canvas
- java - 带有@JsonProperty 的通用 JSON 响应
- stormcrawler - Stormcrawler 中的 URL 重定向使该页面的爬网处于错误状态
- json - Vanilla JS (FUN ONE):从字符串“map”访问深埋 JSON 节点的最佳方式
- python - 使用 Python3 itertools 排列:如何指定所有 r > 1
- javascript - jQuery - 在页面之间来回更改时保留表单数据