python - PyTorch 中 'Module' 的定义到底是什么?

标签 python class pytorch

新手问题请见谅,Module就是说model吗?

这就是它听起来的样子,当文档说:

Whenever you want a model more complex than a simple sequence of existing Modules you will need to define your model (as a custom Module subclass).

或者...当他们提到 Module 时,他们指的是更正式和计算机科学的东西,比如协议(protocol)/接口(interface)类型的东西吗?

最佳答案

这是一个简单的容器。

来自nn.Module的文档

Base class for all neural network modules. Your models should also subclass this class. Modules can also contain other Modules, allowing to nest them in a tree structure. You can assign the submodules as regular attributes. Submodules assigned in this way will be registered, and will have their parameters converted too when you call .cuda(), etc.

来自tutorial :

All network components should inherit from nn.Module and override the forward() method. That is about it, as far as the boilerplate is concerned. Inheriting from nn.Module provides functionality to your component. For example, it makes it keep track of its trainable parameters, you can swap it between CPU and GPU with the .to(device) method, where device can be a CPU device torch.device("cpu") or CUDA device torch.device("cuda:0").

模块是一个容器,层、模型子部分(例如 torchvisionresnet 中的 BasicBlock)和模型应该从中继承。他们为什么要这样做?因为从 nn.Module 继承允许你调用像 to("cuda:0"), .eval(), .parameters() 或轻松注册 Hook 。

  • 为什么不直接将“模块”称为模型,而将层称为“层”?我想也许这只是语义和 split 头发,但仍然......

这是一个 API 设计选择,我发现只有一个 Module 类而不是两个单独的 ModelLayers 更清晰并且允许更大的自由度(将模型的一部分发送到 GPU 更容易,只获取某些层的参数...)。

关于python - PyTorch 中 'Module' 的定义到底是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51804692/

相关文章:

python - 如何将二进制数字列表变成点分十进制数字列表?

python - 如何为我的类(class)构造多项式序列?

Pytorch:如何创建一个随机整数张量,其中某个百分比具有某个值?例如,25% 为 1,其余为 0

torch - PyTorch 中的无量纲转置

python - 使用 PyCharm 进行 pytorch 调试超时

python - pika 中的 AsyncioConnection 和 SelectConnection 适配器有什么区别

python - csv 到嵌套的 JSON?

python - 当列是一系列列表时,如何有条件地添加到 pandas 数据框列中的单元格选择?

class - 'class' 与 'object' 相关,因为 'interface' 与 ...?

c++ - 在 C++ 中使用创建的类动态分配数组时遇到问题