Face Difficulty when converting tensorflow model to Matlab

2 visualizaciones (últimos 30 días)
Ze
Ze el 29 de Jul. de 2024
Editada: Harsh el 30 de Jul. de 2024
I have a part of tensorflow code that I need to translate to matlab, but fail to do that. I have checked deep learning toolbox and unable to resolve the issue. If someone can help me this question, it is very helpful.
My tensorflow code (Python) is the following:
def get_r(model,tw,xw,a_data_n,mean_a,mean_u,kP,dim1,dim2,w1,stdt,stdx):
% A tf.GradientTape is used to compute derivatives in TensorFlow
with tf.GradientTape(persistent=True) as tape: % This makes you record the gradients on the tape for the parameters defined
tape.watch(tw) % This is needed to 'follow' the time, for automatic differentiation with respect to time
tape.watch(xw) % This is needed to 'follow' the position, for automatic differentiation with respect to position
a,u,p = model.net_u(tw,xw)
Px = tape.gradient(p, xw)
At = tape.gradient(a, tw)
ux = tape.gradient(u, xw)
ut = tape.gradient(u, tw)
My matlab code is the following:
function [Lrt, Lr1, Lr2, p_pred_diff, kP] = get_r(model, tw, xw, a_data_n, mean_a, mean_u, kP, dim1, dim2, w1, stdt, stdx)
% Convert to dlarray
tw = dlarray(tw);
xw = dlarray(xw);
% Define a function for computing the model outputs and gradients
function [At, Px, ux, ut, a, u, p] = computeModelAndGradients(tw, xw, model, mean_a)
% Run the model
[a, u, p] = model.net_u(tw, xw);
% Compute gradients
At = dlgradient(sum(a, 'all'), tw, 'EnableHigherDerivatives', true);
Px = dlgradient(sum(p, 'all'), xw, 'EnableHigherDerivatives', true);
ux = dlgradient(sum(u, 'all'), xw, 'EnableHigherDerivatives', true);
ut = dlgradient(sum(u, 'all'), tw, 'EnableHigherDerivatives', true);
end
% Use dlfeval to compute the model outputs and gradients
[At, Px, ux, ut, a, u, p] = dlfeval(@computeModelAndGradients, tw, xw, model, mean_a);
My error message is
Error using deep.internal.dlfevalWithNestingCheck (line 14)
Nested dlfeval calls are not supported. To compute higher derivatives, set the 'EnableHigherDerivatives' option of
the dlgradient function to true.
Error in dlfeval (line 31)
[varargout{1:nargout}] = deep.internal.dlfevalWithNestingCheck(fun,varargin{:});
Error in get_r (line 20)
[At, Px, ux, ut, a, u, p] = dlfeval(@computeModelAndGradients, tw, xw, model, mean_a);
Error in compute_loss3 (line 10)
[Lrt, Lr1, Lr2, Pn, kP] = get_r(model, t, x, a_data_n, mean_a, mean_u, kPa, xdim, ydim, w_upd, stdt, stdx);
I have already enabled higherderivatives. Do I still have nested defevals in the code?
Can anyone point out how can I improve the code by changing the function input and output?
Thanks for all suggestions.
  1 comentario
Harsh
Harsh el 30 de Jul. de 2024
Editada: Harsh el 30 de Jul. de 2024
Hi @Ze, I found a MATLAB answer discussing a similar error, I hope it helps!
https://in.mathworks.com/matlabcentral/answers/1609780-error-message-dlgradient-inputs-must-be-traced-dlarray-objects-or-cell-arrays-structures-or-tabl

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre Agriculture en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by