Borrar filtros
Borrar filtros

(How) Higher derivative by 'dlgradient' or Higher derivative in matlab

8 visualizaciones (últimos 30 días)
jaehong kim
jaehong kim el 16 de Feb. de 2021
Editada: Maksym Tymchenko el 29 de Mayo de 2024
Hi
I am currently coding custom deep learning, but the process stopped at the higher derivative.
I am curious about how to make a higher derivative through dlgradient.
Alternatively, you are welcome to suggest a way to do higher derivatives in matlab.
For example, how to get ddydxx in the following example.
-----------------------------------------------------------------------------------------------------------------------
Code
clc,clear,close all
x=3;
x0=dlarray(x);
[fval,gradval,ggradval] = dlfeval(@Myfunc,x0);
function [fval,gradval,ggradval] = Myfunc(x)
y = 100*(3*x - 7*x.^2).^2;
dydx=dlgradient(y,x,'RetainData',true);
ddydxx=dlgradient(dydx,x);
end
-----------------------------------------------------------------------------------------------------------------------
Error
Error using dlfeval (line 43)
Value to differentiate must be a traced dlarray scalar.
Error in gradtest (line 7)
[fval,gradval,dd] = dlfeval(@Myfunc,x0,y);
-----------------------------------------------------------------------------------------------------------------------
Thanks for reading my question.
  4 comentarios
Walter Roberson
Walter Roberson el 18 de Feb. de 2021
To use automatic differentiation, you must call dlgradient inside a function and evaluate the function using dlfeval. Represent the point where you take a derivative as a dlarray object, which manages the data structures and enables tracing of evaluation
Maksym Tymchenko
Maksym Tymchenko el 29 de Mayo de 2024
Editada: Maksym Tymchenko el 29 de Mayo de 2024
To make the code above work, you need to set the name value argument "EnableHigherDerivatives" to true.
Here's the modified version of your code that works as expected:
clc,clear,close all
x=3;
x0=dlarray(x);
[fval,gradval,ggradval] = dlfeval(@Myfunc,x0);
function [y,dydx,ddydxx] = Myfunc(x)
y = 100*(3*x - 7*x.^2).^2;
dydx=dlgradient(y,x,EnableHigherDerivatives=true);
ddydxx=dlgradient(dydx,x);
end

Iniciar sesión para comentar.

Respuestas (0)

Community Treasure Hunt

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

Start Hunting!

Translated by