Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What is the difference between .data and .detach () in pytorch

Shulou Source: shulou.com Published: 2022-06-01 05:54:50 09月20日 Update

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!

Tags: Data variables tensor gradient derivation change same article content follow recommendation success as we all know not much thing two that is examples most of the attributes Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Huawei vpn MySQL Shulou Tech Info