torch.nn.functional与torch.nn的区别


重新认真学习PyTorch,发现自己没有理清torch.nn.functionaltorch.nn中模块的关系。在网络上找了一些资料,以防遗忘,在此记录。


问题描述:在torch.nn.functionaltorch.nn的文档中,不难发现有很多相似函数名和方法。例如,nn.LogSoftmaxnn.functional.log_softmax。这些方法有什么不同,在代码中有应如何区分使用呢?

简单的总结,torch.nn.Module中的方法是stateful function;而torch.nn.functional中的方法是stateless function。

更具体地说,Module用于自定义的方法。比如在__init__中用于初始化自定义的每层网络结构;在forward中制定每层网络结构的连接方式。这个以上网络结构的定义中,用于构建模块并且需要定义各种配置参数的使用Module,而torch.nn.functional中的方法仅被用于作为算子使用,例如,ReLU等激活函数。

举例来说,在下面的这段代码中,两个dropout函数根据不同的使用方法采用了两个不同的类。

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(1, 10, kernel_size=5)
        self.conv2 = nn.Conv2d(10, 20, kernel_size=5)
        self.conv2_drop = nn.Dropout2d()
        self.fc1 = nn.Linear(320, 50)
        self.fc2 = nn.Linear(50, 10)

    def forward(self, x):
        x = F.relu(F.max_pool2d(self.conv1(x), 2))
        x = F.relu(F.max_pool2d(self.conv2_drop(self.conv2(x)), 2))
        x = x.view(-1, 320)
        x = F.relu(self.fc1(x))
        x = F.dropout(x, training=self.training)
        x = self.fc2(x)
        return F.log_softmax(x)

参考资料:https://discuss.pytorch.org/t/how-to-choose-between-torch-nn-functional-and-torch-nn-module/2800


Author: yangli
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint polocy. If reproduced, please indicate source yangli !
  TOC