【发布时间】:2022-01-16 15:51:10
【问题描述】:
我有一个简单的模型:
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.fc1 = nn.Linear(3, 10)
self.fc2 = nn.Linear(10, 30)
self.fc3 = nn.Linear(30, 2)
def forward(self, x):
x = torch.relu(self.fc1(x))
x = torch.relu(self.fc2(x))
x = torch.tanh(self.fc3(x))
return x
net = Model()
我怎样才能使权重始终在某个值(例如 -1,1)之间?
我尝试了以下方法:
self.fc1 = torch.tanh(nn.Linear(3, 10))
我不完全确定这会始终将它们保持在该值(即使梯度更新试图将它们推得更远)。
但出现以下错误:
TypeError: tanh(): argument 'input' (position 1) must be Tensor, not Linear
【问题讨论】:
-
您希望线性层的权重在 (-1, 1) 范围内还是线性层的输出在 (-1, 1) 范围内?如果您希望输出在该范围内,请使用
tanh,否则使用@yakhyo 指定的Clipping技术。 -
我运行了上面你指定的代码sn-p。它运行良好,并没有引发上述指定的错误。
标签: python neural-network pytorch