在TensorFlow中,我们可以在序列模型中添加L1或L2正则化。我找不到PyTorch中等效的方法。在定义网络时,我们如何为权重添加正则化到PyTorch中:
class Net(torch.nn.Module):
def __init__(self, n_feature, n_hidden, n_output):
super(Net, self).__init__()
self.hidden = torch.nn.Linear(n_feature, n_hidden) # hidden layer
""" How to add a L1 regularization after a certain hidden layer?? """
""" OR How to add a L1 regularization after a certain hidden layer?? """
self.predict = torch.nn.Linear(n_hidden, n_output) # output layer
def forward(self, x):
x = F.relu(self.hidden(x)) # activation function for hidden layer
x = self.predict(x) # linear output
return x
net = Net(n_feature=1, n_hidden=10, n_output=1) # define the network
# print(net) # net architecture
optimizer = torch.optim.SGD(net.parameters(), lr=0.2)
loss_func = torch.nn.MSELoss() # this is for regression mean squared loss