我正在尝试使用 pytorch 编写一些简单的神经网络。我是这个图书馆的新手。我遇到了两种实现相同想法的方法:一个具有固定激活函数(例如 tanh)的层。
第一种实现方式:
l1 = nn.Tanh(n_in, n_out)
第二种方式:
l2 = nn.Linear(n_in, n_out) # linear layer, that do nothing with its input except summation
但在前向传播中使用:
import torch.nn.functional as F
x = F.tanh(l2(x)) # x - value that propagates from layer to layer
这些机制之间有什么区别?哪个更适合哪些用途?
最佳答案
激活函数只是一个非线性函数,它没有任何参数。所以,您的第一种方法没有任何意义!
但是,您可以使用 sequential将线性层与 tanh
激活相结合的包装器。
model = nn.Sequential(
nn.Linear(n_in, n_out),
nn.Tanh()
)
output = model(input)
关于python - PyTorch 中的层类型及其激活函数有何区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56117566/