Pytorch custom activation functions?

前端 未结 2 1979
终归单人心
终归单人心 2021-02-09 11:49

I\'m having issues with implementing custom activation functions in Pytorch, such as Swish. How should I go about implementing and using custom activation functions in Pytorch?<

2条回答
  •  情书的邮戳
    2021-02-09 12:20

    You can write a customized activation function like below (e.g. weighted Tanh).

    class weightedTanh(nn.Module):
        def __init__(self, weights = 1):
            super().__init__()
            self.weights = weights
    
        def forward(self, input):
            ex = torch.exp(2*self.weights*input)
            return (ex-1)/(ex+1)
    

    Don’t bother about backpropagation if you use autograd compatible operations.

提交回复
热议问题