Face Difficulty when converting tensorflow model to Matlab
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Ze
le 29 Juil 2024
Réponse apportée : William Rose
le 29 Juil 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_grad3(model, t, x,a_data,u_data,a_data_n,mean_a,mean_u,w_upd,w1,w2,w3,p_data,stdt,stdx,xdim,ydim,twf,xwf,p_dataf):
% A tf.GradientTape is used to compute derivatives in TensorFlow
with tf.GradientTape(persistent=True) as tape:
% Split t and x to compute partial derivatives
tape.watch(model.trainable_variables)
loss, Lu, Lr, Lm, Pn,w1n,Lr1,Lr2,w_upd,kP = compute_loss3(model, t, x, a_data, u_data, a_data_n,mean_a,mean_u,w_upd,w1,w2,w3,p_data,stdt,stdx,xdim,ydim,twf,xwf,p_dataf)
grads_u = tape.gradient(loss, model.trainable_variables) % loss function
del tape
return grads_u, loss, Lu, Lr, Lm,Pn,w1n,Lr1,Lr2,w_upd,kP
I followed my previous question response: https://www.mathworks.com/matlabcentral/answers/2140906-face-difficulty-when-converting-tensorflow-model-to-matlab?s_tid=mlc_ans_men_view&mentions=true#answer_1491566
My matlab code is the following:
function [grads_u, loss, Lu, Lr, Lm, Pn, w1n, Lr1, Lr2, w_upd, kP] = get_grad3(model, t, x, a_data, u_data, a_data_n, mean_a, mean_u, w_upd, w1, w2, w3, p_data, stdt, stdx, xdim, ydim, twf, xwf, p_dataf)
% Define a function for computing loss and other required variables
function [loss, Lu, Lr, Lm, Pn, w1n, Lr1, Lr2, w_upd, kP] = computeLossGradients(model, t, x)
% Compute the loss and other outputs using the compute_loss3 function
[loss, Lu, Lr, Lm, Pn, w1n, Lr1, Lr2, w_upd, kP] = compute_loss3(model, t, x, a_data, u_data, a_data_n, mean_a, mean_u, w_upd, w1, w2, w3, p_data, stdt, stdx, xdim, ydim, twf, xwf, p_dataf);
% Compute gradients of the loss with respect to the model's learnable parameters
grads_u = dlgradient(loss, model.LearnableParameters, 'EnableHigherDerivatives', true);
end
% Use dlfeval to compute the loss and other variables
[loss, Lu, Lr, Lm, Pn, w1n, Lr1, Lr2, w_upd, kP] = dlfeval(@computeLossGradients, model, t, x);
end
My error message is
Unrecognized function or variable 'w_upd'.
Error in get_grad3/computeLossGradients (line 6)
[loss, Lu, Lr, Lm, Pn, w1n, Lr1, Lr2, w_upd, kP] = compute_loss3(model, t, x, a_data, u_data, a_data_n, mean_a, mean_u, w_upd, w1, w2, w3, p_data, stdt, stdx, xdim, ydim, twf, xwf, p_dataf);
Error in deep.internal.dlfeval (line 17)
[varargout{1:nargout}] = fun(x{:});
Error in deep.internal.dlfevalWithNestingCheck (line 19)
[varargout{1:nargout}] = deep.internal.dlfeval(fun,varargin{:});
Error in dlfeval (line 31)
[varargout{1:nargout}] = deep.internal.dlfevalWithNestingCheck(fun,varargin{:});
Error in get_grad3 (line 12)
[loss, Lu, Lr, Lm, Pn, w1n, Lr1, Lr2, w_upd, kP] = dlfeval(@computeLossGradients, model, t, x);
Error in PWIP_initialize_v2 (line 114)
[grads_u, loss, Lu, Lr, Lm, Pn, w1n, Lr1, Lr2, w_upd, kP] = get_grad3(model, tw, xw, a_data, u_data, a_data_n, mean_a, mean_u, epoch, w1, w2, w3, p_data, stdt, stdx, xdim, ydim, twf, xwf, p_dataf);
I have also tried the symbolic differentiation and it works okay.
function [grads_u, loss, Lu, Lr, Lm, Pn, w1n, Lr1, Lr2, w_upd, kP] = get_grad3(model, t, x, a_data, u_data, a_data_n, mean_a, mean_u, w_upd, w1, w2, w3, p_data, stdt, stdx, xdim, ydim, twf, xwf, p_dataf)
% Compute the loss and other required variables
[loss, Lu, Lr, Lm, Pn, w1n, Lr1, Lr2, w_upd, kP] = compute_loss3(model, t, x, a_data, u_data, a_data_n, mean_a, mean_u, w_upd, w1, w2, w3, p_data, stdt, stdx, xdim, ydim, twf, xwf, p_dataf);
% Define symbolic variables
num_vars = numel(model.trainable_variables);
syms model_sym [1 num_vars]
% Convert loss to symbolic
loss_sym = sym(loss);
% Calculate gradients
grads_u = sym('grads_u', [1 num_vars]);
for i = 1:num_vars
grads_u(i) = diff(loss_sym, model_sym(i));
end
end
Can anyone point out how can I improve the code by changing the function input and output?
Thanks for all suggestions.
0 commentaires
Réponse acceptée
William Rose
le 29 Juil 2024
@Ze,
Your function compute_loss_gradients(...) calls function compute_loss3(). The call to compute_loss3(...) includes w_upd as one of the parameters passed to the function. (w_upd is also one of the outputs from the function.) The error occurs because w_upd has not been passed to compute_loss_gradients(...), so it is unknown, and cannot be passed to compute_loss3().
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Labeling, Segmentation, and Detection dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!