torch.nn.Relu
and torch.nn.functional.relu
结果一致,不同点如下
1 nn.Relu
不同的是 nn.Relu
作为一层结构,必须添加到 nn.Module
容器中才能使用
使用方式如下,定义时不接输入,定义好后,使用 Relu()(input)
进行参数传递
class BasicBlock(nn.Module):
expansion = 1
def __init__(self):
super(BasicBlock, self).__init__()
self.relu = nn.ReLU()
...
def forward(self, x):
x = self.relu(x)
2 F.relu
而 F.relu
是函数,可以直接在 forward
中调用,使用 relu(input)
进行参数传递,具体方式如下
import torch.nn.functional as F
class BasicBlock(nn.Module):
expansion = 1
def __init__(self):
super(BasicBlock, self).__init__()
...
def forward(self, x):
x = F.relu(x)
注意,torch.nn
中的代码实现基本都会调用 torch.nn.functional
中的方法