How to calculate gradient of output of a neural network with respect to its parameters?
14 ビュー (過去 30 日間)
古いコメントを表示
I am new to the Deep Learning toolbox. I am working on a Reinforcement Learning problem wherein I need to calculate the derivative of the output of NN with respect to parameters.
More specifically, let the I/O relation of the neural network be defined as
, where x is the input, y is the output, and θ contains the weights and biases of the neural network. For a specific input
, I am interested in calculating
. Any idea how I should go about this with the deep learning toolbox?
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/598305/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/598950/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/598955/image.png)
0 件のコメント
採用された回答
Jon Cherrie
2021 年 4 月 27 日
You can use dlgradient and dlfeval to take derivatives with respect to the input or to the parameters
Here's an example:
I'm going to let this function play the role of the neural network. You should use dlnetwork to create a more complex network.
function y = f(x,theta)
y = sum(sin(theta(1)*x+theta(2)));
end
Then I need to scope the computation of the function so that dlfeval knows where to apply auto-diff. I do that by defining a function that evaluates the network and computes the gradient of interest. In this case:
function [y, dy] = fun_and_deriv(x,theta)
y = f(x,theta);
dy = dlgradient(y,theta);
end
Then I can compute the derivative:
x = dlarray(0:10);
theta = dlarray([1 2]);
[y, dy] = dlfeval(@fun_and_deriv,x,theta)
which gives:
y =
1×1 dlarray
-0.9668
dy =
1×2 dlarray
0.6788 -1.1095
0 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Custom Training Loops についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!