gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 20:17:55 25 4
gpt4 key购买 nike

新手问题请见谅,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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com