In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you what is the difference between .data and .detach () in pytorch, I believe most people do not know much about it, so share this article for your reference. I hope you will learn a lot after reading this article. Let's learn about it together.
As we all know, a tensor in pytorch has two attributes by default, one is data and the other is grid. But the data here is also tensor. Sometimes we don't want to worry about gradient grid, so we use .data to extract the data here.
A = torch.tensor (requires_grad = True) out = a.sigmoid () c = out.data # need to go note that the variables obtained by .data "separation" will share the same data as the original variables, and the newly separated tensor is not derivable, c has changed, the original tensor will also change c.zeroo# change the value of c, the original out will also change
Tensor .data returns the same data tensor as x, and this new tensor and the original tensor share the data, one changes, the other changes, and the newly separated tensor require s_grad = False, that is, it is not derivable. (this is actually the same with detach.)
In addition to .data, pytorch also provides. Detach () to implement the same operation. Regardless of grid, just extract data, you can use .detach (). And we prefer to use .detach ().
A = torch.tensor (requires_grad = True) out = a.sigmoid () c = out.detach () # notice that the variables obtained by .detach () "detach" will share the same data as the original variables, and the newly separated tensor is not derivable, c has changed, and the original tensor will also change c. Zero() # change the value of c The original out will also change.
Tensor .detach () returns the same data tensor as x, and this new tensor and the original tensor share the data, one changes, the other changes, and the newly separated tensor's require s_grad = False, that is, it is not derivable. (in fact, .data is the same.)
So what's the difference? Why is .datach () more recommended?
For example, the above out contains both data and grid. Now I don't want to worry about gradients, so I can use out.data to extract purebred tensor, and there is no such thing as gradients for out.data, which is c. But I c. Zero _ () modified the value of out, and then bp will be successful! There is no gradient to be able to bp! So .data has its drawbacks. There is no such problem with .detach (), so it is recommended to use .datach ().
These are all the contents of the article "what's the difference between .data and .detach () in pytorch". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to 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.