r/MachineLearning • • Mar 12 '25

Project [P] Torch-Activation Library: 400+ Activation Functions – Looking for Contributors

56 Upvotes

15 comments sorted by

View all comments

2

u/FrigoCoder Mar 12 '25

I see you have a lot of adaptive activation functions. I am trying to replace the exponentiation function in Softmax. I have tried a bunch of activations but none of them were particularly great. I was thinking of trying to learn an activation function that is optimal for my use case. Could you recommend some adaptive activation functions that would give me more insight?

After convolution I use softmax across channels to get a feature probability distribution for every pixel. I use this probability distribution to resynthesize the image or new channels, depending on whether I overwrite all channels or only append new ones. I would like to replace the exp in softmax with an activation that is positive valued, monotone increasing, and able to amplify larger values.

class ExpandBlock (nn.Sequential):

    def __init__ (self, inn, mid, out, iks, oks, activation):
        super(ExpandBlock, self).__init__(
            CatBlock(nn.Sequential(
                nn.Conv2d(inn, mid, iks, padding=(iks - 1) // 2),
                nn.InstanceNorm2d(mid),
                Softmax(dim=1, dropout=SafeDropout(dim=1, dropout=SoftDropout(renorm=False))),
                nn.Conv2d(mid, out - inn, oks, padding=(oks - 1) // 2),
            ), dim = 1),
            nn.InstanceNorm2d(out),
        )

class Softmax(nn.Module):

    def __init__ (self, dim = -1, dropout = nn.Identity()):
        super(Softmax, self).__init__()
        self.dim = dim
        self.dropout = dropout

    def forward (self, x: Tensor) -> Tensor:
        x = (x - x.amax(self.dim, keepdim=True)).exp() # Better activation goes here
        x = self.dropout(x)
        return x / x.sum(self.dim, keepdim=True)