How to implement in Matlab Deep Learning PyTorch detach or TensorFlow stop_gradient?

1 回表示 (過去 30 日間)
John Smith
John Smith 2021 年 8 月 19 日
回答済み: Damien T 2022 年 11 月 7 日
Hello!
Pytorch has a facility to detach a tensor so that it will never require a gradient, i.e. (from here):
In order to enable automatic differentiation, PyTorch keeps track of all operations involving tensors for which the gradient may need to be computed (i.e., require_grad is True). The operations are recorded as a directed graph. The detach() method constructs a new view on a tensor which is declared not to need gradients, i.e., it is to be excluded from further tracking of operations, and therefore the subgraph involving this view is not recorded.
TensorFlow has a comparable facility stop_gradient.
These are useful when one needs copies of expressions that are treated as constants and whose gradient should not be calculated during learning.
How does one implement such a thing in Matlab's Deep Learning Toolbox? (Possibly in a custom training loop)
Thx,
D

採用された回答

Damien T
Damien T 2022 年 11 月 7 日
You just need to call the following. It will turn a traced dlarray into a standard Matlab variable, hence the autodiff engine will treat the new ("detached") variable as a constant.
myDetachedVar = extractdata(myVar);

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeImage Data Workflows についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by