Rasule
Rasule

Reputation: 19

Function of Activation functions

is it possible to define a function of activation function? I tried to do :

def activation():
    # return nn.Sin()
    # return nn.Tanh()
    # return nn.Sigmoid()
    # return nn.Tanhshrink()
    return nn.HardTanh(-1,1)
    # return nn.Hardswish()
    # return nn.functionnal.silu()

But i get an error when trying to call it. Here is an example:

def f():
  return nn.Tanh()
input = torch.randn(2)
output = f(input)
print(output)

it outputs "TypeError: f() takes 0 positional arguments but 1 was given". It doesn't work even i gave it an argument x.

Upvotes: 0

Views: 242

Answers (2)

Ivan
Ivan

Reputation: 40618

You can either use the object-oriented approach:

>>> f = nn.Tanh()
>>> output = f(x)

Or the functional approach where you will find the equivalent for nn.Tanh inside nn.functional as tanh.

>>> f = nn.functional.tanh
>>> output = f(x)

Upvotes: 2

Vladimir Ryabtsev
Vladimir Ryabtsev

Reputation: 11

Indeed, you are not providing argument to the function. Is this what you are trying to do?

def f(x):
  return nn.Tanh(x)

Upvotes: 0

Related Questions