PyTorch之nn.ReLU与F.ReLU的区别

import torch.nn as nn
import torch.nn.functional as F
import torch.nn as nn

class AlexNet_1(nn.Module):

    def __init__(self, num_classes=n):
        super(AlexNet, self).__init__()
        self.features = nn.Sequential(
            nn.Conv2d(3, 64, kernel_size=3, stride=2, padding=1),
            nn.BatchNorm2d(64),
            nn.ReLU(inplace=True),
         )

    def forward(self, x):
        x = self.features(x)

class AlexNet_2(nn.Module):

    def __init__(self, num_classes=n):
        super(AlexNet, self).__init__()
        self.features = nn.Sequential(
            nn.Conv2d(3, 64, kernel_size=3, stride=2, padding=1),
            nn.BatchNorm2d(64),
         )

    def forward(self, x):
        x = self.features(x)
        x = F.ReLU(x)

在如上网络中,AlexNet_1与AlexNet_2实现的结果是一致的,但是可以看到将ReLU层添加到网络有两种不同的实现,即nn.ReLU和F.ReLU两种实现方法。
其中nn.ReLU作为一个层结构,必须添加到nn.Module容器中才能使用,而F.ReLU则作为一个函数调用,看上去作为一个函数调用更方便更简洁。具体使用哪种方式,取决于编程风格。在PyTorch中,nn.X都有对应的函数版本F.X,但是并不是所有的F.X均可以用于forward或其它代码段中,因为当网络模型训练完毕时,在存储model时,在forward中的F.X函数中的参数是无法保存的。也就是说,在forward中,使用的F.X函数一般均没有状态参数,比如F.ReLU,F.avg_pool2d等,均没有参数,它们可以用在任何代码片段中。

如果您觉得我的文章对您有所帮助,欢迎扫码进行赞赏!

PyTorch之nn.ReLU与F.ReLU的区别_第1张图片

1. pytorch学习笔记(九):PyTorch结构介绍

2. Whats the difference between nn.relu() vs F.relu()

3. pytorch中nn和nn.functional有什么区别?

你可能感兴趣的:(PyTorch,PyTorch,nn.ReLU,F.ReLU,functional)