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?
Neeta Dsouza answered .
2025-11-20
function y = f(x,theta) y = sum(sin(theta(1)*x+theta(2))); end
function [y, dy] = fun_and_deriv(x,theta) y = f(x,theta); dy = dlgradient(y,theta); end
x = dlarray(0:10); theta = dlarray([1 2]); [y, dy] = dlfeval(@fun_and_deriv,x,theta)
y =
1×1 dlarray
-0.9668
dy =
1×2 dlarray
0.6788 -1.1095