2 回答

TA贡献1856条经验 获得超17个赞
您可以将其内联到自定义层中,而不是将其定义为特定函数。
例如,您的解决方案可能如下所示:
import torch
import torch.nn as nn
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.fc1 = nn.Linear(4, 10)
self.fc2 = nn.Linear(10, 3)
self.fc3 = nn.Softmax()
def forward(self, x):
return self.fc3(self.fc2(torch.tanh(self.fc1(x)/10)))
wheretorch.tanh(output/10)内联在模块的 forward 函数中。

TA贡献1946条经验 获得超4个赞
您可以使用乘法参数创建一个图层:
import torch
import torch.nn as nn
class CustomTanh(nn.Module):
#the init method takes the parameter:
def __init__(self, multiplier):
self.multiplier = multiplier
#the forward calls it:
def forward(self, x):
x = self.multiplier * x
return torch.tanh(x)
将其添加到您的模型中,CustomTanh(1/10)而不是nn.Tanh()。
添加回答
举报