In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "multiple model construction methods of Pytorch". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
The previous example is mainly carried out by Sequential, here to make a comprehensive and full summary, used to be just a simple stack of the network, read this chapter to know that there are so many techniques that can be used.
For the basic network construction, the Module class is mainly used, and the sequential class is the main class.
Module can be regarded as the base class, while the derived classes are Sequential, ModuleList and ModuleDict classes, each of which has its own characteristics.
Sequential class:
The Sequential class mainly inherits from the Module class and is the simplest way to accumulate the network layer by layer.
For example, the simplest way to overlay:
Net=nn.Sequential (nn.Linear (784256), nn.ReLU (), nn.Linear (256, 10))
The characteristics of using this method are:
1. Simple, but not customized by high flexibility
two。 Instead of customizing the forward forward propagation function, the parameter sizes between layers must match
ModuleList class:
As follows:
Net=nn.ModuleList ([nn.Linear (784256), nn.ReLU ()]) net.append (nn.Linear (256) 10))
This is also similar to Sequential, which is tabulated to construct a network.
However, it should be noted that the Forward function needs to be defined, and only the Sequential class does not need a custom Forward function.
ModuleDict class:
ModuleDict is similar to the way Sequential classes are defined, and can be indexed and looked up through a dictionary
Net=nn.ModuleDict ({'linear':nn.Linear (784256),' act':nn.ReLU ()}) net ['output'] = nn.Linear (256 Magi 10)
But similar to ModuleList, the forward forward propagation function needs to be redefined
Use a variety of Module to build complex networks:
For example:
Class FancyMLP (nn.Module): def _ init__ (self, * * kwargs): super (FancyMLP, self). _ init__ (* * kwargs) self.rand_weight = torch.rand ((20,20), requires_grad=False) # untrainable parameter (constant parameter) self.linear = nn.Linear (20,20) def forward (self X): X = self.linear (x) # using the constant parameters created And the relu function and mm function x = nn.functional.relu (torch.mm (x, self.rand_weight.data) + 1) # in nn.functional multiplex the full connection layer. Equivalent to the shared parameter x = self.linear (x) # control flow between the two fully connected layers, we need to call the item function to return scalars to compare while x.norm (). Item () > 1: X / = 2 if x.norm (). Item () < 0.8: X * = 10 return x.sum ()
It is worth noting that although there is only one Linear layer, there is more operating space for forward.
The forward function is equivalent to passing through two linear layers, and then processes the data in it
Therefore, there can be more operating space for Module, such as complex network customization using the above network.
For example, you can nest multiple layers of Module and define their own forward functions in the following way
Class SelfMLP (nn.Module): def _ init__ (self,**kwargs): super (SelfMLP, self). _ init__ (* * kwargs) self.ran_weight=torch.rand ((20 kwargs 20), requires_grad=False) self.linear=nn.Linear (20 Magne20) def forward (self X): x=self.linear (x) x=nn.functional.relu (torch.mm (x)) class NestMLP (nn.Module): def _ _ init__ (self,**kwargs): super (NestMLP, self). _ init__ (* * kwargs) self.net=nn.Sequential (nn.Linear (40 kwargs 30), nn.ReLU () def forward (self,x): return self.net (x) net=nn.Sequential (NestMLP ()) This is the end of the introduction of "multiple Model Construction methods for Pytorch" by nn.Linear (30 ~ (20)), SelfMLP (). Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.