Face Difficulty when converting tensorflow model to Matlab
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: (notice that model.net_u input cannot accept dlarray format, dlarrya has to be done after model.net_u fucntion)
function get_r(model, tw, xw, a_data_n, mean_a, mean_u, kP, dim1, dim2, w1, stdt, stdx)
% Compute derivatives using the MATLAB automatic differentiation functionality
% Run the model
[a, u, p] = model.net_u(tw, xw);
a = dlarray(a);
tw = dlarray(tw);
xw = dlarray(xw);
% Compute gradients by iterating over each element
At = dlgradient(a, tw);
Px = dlgradient(p, xw);
ux = dlgradient(u, xw);
ut = dlgradient(u, tw);
end
Also, my a, u, p variable all have the shape 39600 * 1
My error message is
Error using dlarray/dlgradient (line 105)
Value to differentiate is not traced. It must be a traced real dlarray scalar. Use dlgradient inside a function called by dlfeval to
trace the variables.
Error in get_r (line 12)
At = dlgradient(sum(a, ‘all’), tw);
Can anyone point out how can I improve the code by adding dlfeval and other codes as well.
Additionally, sometimes when I add dlfeval, it will create the following error. What does this error mean?
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{:});
Thanks for all suggestions!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: (notice that model.net_u input cannot accept dlarray format, dlarrya has to be done after model.net_u fucntion)
function get_r(model, tw, xw, a_data_n, mean_a, mean_u, kP, dim1, dim2, w1, stdt, stdx)
% Compute derivatives using the MATLAB automatic differentiation functionality
% Run the model
[a, u, p] = model.net_u(tw, xw);
a = dlarray(a);
tw = dlarray(tw);
xw = dlarray(xw);
% Compute gradients by iterating over each element
At = dlgradient(a, tw);
Px = dlgradient(p, xw);
ux = dlgradient(u, xw);
ut = dlgradient(u, tw);
end
Also, my a, u, p variable all have the shape 39600 * 1
My error message is
Error using dlarray/dlgradient (line 105)
Value to differentiate is not traced. It must be a traced real dlarray scalar. Use dlgradient inside a function called by dlfeval to
trace the variables.
Error in get_r (line 12)
At = dlgradient(sum(a, ‘all’), tw);
Can anyone point out how can I improve the code by adding dlfeval and other codes as well.
Additionally, sometimes when I add dlfeval, it will create the following error. What does this error mean?
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{:});
Thanks for all suggestions! 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: (notice that model.net_u input cannot accept dlarray format, dlarrya has to be done after model.net_u fucntion)
function get_r(model, tw, xw, a_data_n, mean_a, mean_u, kP, dim1, dim2, w1, stdt, stdx)
% Compute derivatives using the MATLAB automatic differentiation functionality
% Run the model
[a, u, p] = model.net_u(tw, xw);
a = dlarray(a);
tw = dlarray(tw);
xw = dlarray(xw);
% Compute gradients by iterating over each element
At = dlgradient(a, tw);
Px = dlgradient(p, xw);
ux = dlgradient(u, xw);
ut = dlgradient(u, tw);
end
Also, my a, u, p variable all have the shape 39600 * 1
My error message is
Error using dlarray/dlgradient (line 105)
Value to differentiate is not traced. It must be a traced real dlarray scalar. Use dlgradient inside a function called by dlfeval to
trace the variables.
Error in get_r (line 12)
At = dlgradient(sum(a, ‘all’), tw);
Can anyone point out how can I improve the code by adding dlfeval and other codes as well.
Additionally, sometimes when I add dlfeval, it will create the following error. What does this error mean?
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{:});
Thanks for all suggestions! deep learning MATLAB Answers — New Questions