In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "what's the use of torch.nn.ModuleList". Interested friends might as well take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what's the use of torch.nn.ModuleList"?
Brief introduction
Class torch.nn.ModuleList (modules=None) Holds submodules in a list. It can maintain multiple sub-modules in the form of a list. ModuleList can be indexed like a regular Python list, but modules it contains are properly registered, and will be visible by all Module methods. ModuleList can be indexed like the python list, and the modules in it will be registered correctly, and the modules it saves can be visible by all Module methods. The reason why it cannot be saved directly with the python list is that PyTorch needs to automatically track the calculation chart and calculate the automatic gradient, if you directly use the python list or python dictionary to save the module. Then the gradient cannot be calculated automatically. The Parameters parameter modules (iterable, optional)-the parameter an iterable of modules to add accepts is the iterable type of the module module and is used to add to the ModuleList append (module) method Appends a given module to the end of the list. Parameters module (nn.Module)-module to append is used to add a module to the extend (modules) method Appends modules from a Python iterable to the end of the list at the end of the list. Add multiple module Parameters parameters modules (iterable)-iterable of modules to append module from the python iterable object to the list. The insert (index, module) method Insert a given module before a given index in the list. Parameters index (int)-index to insert. Module (nn.Module)-module to insert this method inserts a module into the ModuleList at a specified location just like a python normal list
Demo code:
Class MyModule (nn.Module): def _ init__ (self): super (MyModule, self). _ _ init__ () self.linears = nn.ModuleList ([nn.Linear (10,10) for i in range (10)]) def forward (self, x): # ModuleList can act as an iterable, or be indexed using intsfor i, lin enumerate (self.linears): X = self.linears [I / / 2] (x) + l (x) return x Note: submodules must be placed in the attributes at the top level It is impossible to put it in a list or dictionary. Because if it is not at the top level, but in a list or dictionary, the optimizer cannot accurately locate these sub-modules and therefore cannot optimize and update the parameters of these modules. If you need a list or dictionary to store your submodules, you need to use the nn.ModuleList and nn.ModuleDict methods provided by pytorch. Note: the sub-module submodules must be located in the top-level attribute and cannot be stored in a list or dictionary, otherwise the optimizer cannot accurately locate these learning parameters and therefore cannot optimize the parameters correctly. If necessary, we can use nn.ModuleList and nn.ModuleDict provided by pytorch to implement list and dictionary functions. At this point, I believe you have a deeper understanding of "what is the use of torch.nn.ModuleList"? you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.