Call activation function from string

Hi,

Is there a way to call an activation function from a string?
For example something like this :

activation_string = "relu"
activation_function = nn.activation(activation_string)
u = activation_function(v)

It would be really practical to have something like this, for example to define the activation function in a config file, instead of inside the classes.

Thanks in advance,
Manu

2 Likes

you can create a dictionary with several activation function mappings:

activations = {
    'relu': nn.ReLU()
    'sigmoid': nn.Sigmoid(),
    'tanh': nn.Tanh()
}

and then call

activation_function = activations[activation_string]
u = activation_function(v)

Thank you for your answer.
I considered this option before asking the question but I just wanted to know if there was any built-in implementation before reinventing the wheel.
Thanks :wink:

You can dynamically access the method by using getattr:

activation_string = "relu"
activation_function = getattr(nn, activation_string)()  
u = activation_function(v)