为什么我们需要从pytorch中的nn.模块继承?

发布于 2025-02-04 10:28:27 字数 598 浏览 2 评论 0 原文

我正在浏览Udacity的 with pytorch课程的深度学习介绍教师的神经网络部分说:“在初始方法中,我们需要致电超级,我们需要这样做,因为那时Pytorch将知道注册所有不同的层和操作,如果您不这样做,能够跟踪您添加到网络中的内容,并且它将无法使用”。您能否详细说明并说明这里的超级关键字的作用到底是什么?

此外,Jupyter笔记本电脑说了以下有关超级使用的使用,

class Network(nn.Module):

在这里,我们从 nn.module 继承。与 super().__ init __()这创建了一个跟踪体系结构 并提供许多有用的方法和属性。这是强制性的 从 nn.module 继承时 网络。班级的名称本身可以是任何东西。

I am going through Udacity's Intro To Deep Learning with Pytorch course In the Neural Network part the instructor says that "in the init method, we need to call super, we need to do that because then PyTorch will know to register all the different layers and operation, if you don't do this part it wont be able to track the things that you are adding to your network and it wont work". Could you kindly elaborate and explain what exactly is the role of super keyword here and what does nn.Module inherits which helps in "keeping track" of changes.

Further the Jupyter notebooks says the following about use of super,

class Network(nn.Module):

Here we're inheriting from nn.Module. Combined with
super().__init__() this creates a class that tracks the architecture
and provides a lot of useful methods and attributes. It is mandatory
to inherit from nn.Module when you're creating a class for your
network. The name of the class itself can be anything.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

日裸衫吸 2025-02-11 10:28:28

类网络(nn.module)表示您定义了网络类,该类从 nn.module >)继承了所有方法和属性网络是子类, nn.module 是父类)

通过使用 super() 。代码>将与其父 __ INIT __ 相同。

class Network(nn.Module):
    def __init__(self):
        super(Network, self).__init__()

例如,当您制作模型时:
model = Network()
通过调用此模型,实际上 nn.module 将处理此调用并跟踪更改。

请访问 nn.module 源代码。

class Network(nn.Module) means that you defined a Network class that inherits all the methods and properties from nn.Module (Network is child class and nn.Module is parent class)

By using super().__init__() , Network's __init__ will be same as its parent __init__.

class Network(nn.Module):
    def __init__(self):
        super(Network, self).__init__()

For example when you make a model:
model = Network()
by calling this model, actually nn.Module will handle this call and track changes.

visit this link if you want to see nn.Module source codes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文