PyTorch 中的 "modules" 和 "functionals" 有什么区别?

What is the difference between "modules" and "functionals" in PyTorch?

在 PyTorch 中,我们有 torch.nn.funtional and torch.nn,前者中的 classes/functions 通常称为“函数”,而后者称为“模块”。

两者之间似乎有很多重叠,所以我想知道它们分别用于什么以及它们之间的区别是什么?

区别如下:

  • torch.nn.functional 是在 torch.Tensor.

    上应用 PyTorch 运算符的基本功能接口(就编程范例而言)
  • torch.nn 包含为这些运算符提供 object-oriented 接口的包装器 nn.Module

所以确实存在完全重叠,模块是访问这些函数提供的运算符的不同方式。

PyTorch 中的每个张量运算符都以函数及其包装器的形式提供 class。比如F.conv2d, F.relu, F.dropout, F.batch_norm等...都有对应的模块nn.Conv2d, nn.ReLU, nn.Dropout, nn.BatchNorm2d, 2d, 3d.