In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article is to share with you about how to use the torch.nn.Parameter method, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
Explanation:
It is a subclass of Tensor, it can be used as a parameter of module, assign it to the property of module, then it will be automatically added to the parameter of module, that is, appear in the parameters () iterator. If you don't use the Parameter type, just use Tensor, and assign the Tensor class to the property of module, it won't have this effect, it won't appear in the parameter () iterator.
Experiment:
Import torchimport torch.nn as nnclass Model4CXQ (nn.Module): def _ _ init__ (self): super (Model4CXQ Self). _ _ init__ () # super (). _ _ init__ () self.attribute4cxq = nn.Parameter (torch.tensor (20200910.0)) self.attribute4lzq = nn.Parameter (torch.tensor (20200.0)) # self.attribute4scc = nn.Parameter (torch.Tensor (2.0)) # TypeError: new (): data must be a sequence (got float) # self.attribute4pq = nn.Parameter (torch.tensor (2)) # RuntimeError: Only Tensors of floating point dtype can require gradientsself .attribute4zh = nn.Parameter (torch.Tensor (2)) # self.attribute4yzb = nn.Parameter (torch.tensor (912.0)) self.attribute4yzb = (torch.tensor (912.0)) self.attribute4gcx = (torch.tensor (3)) self.attribute4ymw = (torch.Tensor (3)) def forward (self) X): passif _ _ name__ = = "_ _ main__": model = Model4CXQ () print () print ("print parameters". Center (50LJ) print () for param in model.parameters (): print (param) print () print ("print Dictionary" .center (50cet) for k, v in model.state_dict (). Items (): print (k, v)
Console output:
Copyright (C) Windows PowerShell Microsoft Corporation. All rights reserved. It took 861ms to try a new cross-platform PowerShell https://aka.ms/pscore6 to load personal and system profiles. (base) PS C:\ Users\ chenxuqi\ Desktop\ News4cxq\ test4cxq > conda activate ssd4pytorch2_2_0 (ssd4pytorch2_2_0) PS C:\ Users\ chenxuqi\ Desktop\ News4cxq\ test4cxq > &'D:\ Anaconda3\ envs\ ssd4pytorch2_2_0\ python.exe''c:\ Users\ chenxuqi\ .vscode\ extensions\ ms-python.python-2020.12.424452561\ lib\ python\ debugpy\ launcher' '56980' -' c:\ Users\ chenxuqi\ Desktop\ News4cxq\ test4cxq\ test8.py'--- print parameter-Parameter containing:tensor (20200910. Requires_grad=True) Parameter containing:tensor (20200.requires_grad=True) Parameter containing:tensor ([1.1673e-42, 0.0000e+00], requires_grad=True)-print dictionary-attribute4cxq tensor (20200910.) attribute4lzq tensor (20200.) attribute4zh tensor ([1.1673e-42 0.0000e+00]) (ssd4pytorch2_2_0) PS C:\ Users\ chenxuqi\ Desktop\ News4cxq\ test4cxq > above is how to use the torch.nn.Parameter method The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
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.