[英]Function of Activation functions
is it possible to define a function of activation function?是否可以定义激活 function 的 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".它输出“TypeError:f() 采用 0 位置 arguments 但给出了 1”。 It doesn't work even i gave it an argument x.
即使我给了它一个参数x,它也不起作用。
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
.或者功能方法,您可以在
nn.functional
中找到nn.Tanh
的等价物作为tanh
。
>>> f = nn.functional.tanh
>>> output = f(x)
Indeed, you are not providing argument to the function.实际上,您没有为 function 提供论据。 Is this what you are trying to do?
这是你想要做的吗?
def f(x):
return nn.Tanh(x)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.