PyTorch中的层类型及其激活功能有什么区别?

2024-04-25 18:25:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用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

这些机制之间有什么区别?哪一个更适合哪一个目的?你知道吗


Tags: 方法in功能layerl1that图书馆方式
1条回答
网友
1楼 · 发布于 2024-04-25 18:25:56

激活函数只是一个非线性函数,它没有任何参数。所以,你的第一个方法没有任何意义!你知道吗

但是,可以使用sequential包装器将线性层与tanh激活结合起来。你知道吗

model = nn.Sequential(
    nn.Linear(n_in, n_out),
    nn.Tanh()
)
output = model(input)

相关问题 更多 >