필터 지우기
필터 지우기

dlgradient throws Value to differentiate is non-scalar. It must be a traced real dlarray scalar.error

조회 수: 2 (최근 30일)
Hi everyone,
With dfleval I call a function with which I do a number of computations. I need to calculate few gradients within that function but it throws an error. Here is the code.
function [grads,loss] = schooling(data,Q,params)
% Trains the model
% receives the model and input parameters
l = data(2:end,1); %x coordinates
t = data(1,2:end); %time coordinates
[T,D] = NN(params,t,l); % feed forward
[Tt,Tx] = dlgradient(T,t,l); % here I have error message. % all inputs T,t,l are dlarrays. The function schooling is
% called from within dfleval
Txdx = dlgradient(D.*Tx,l);
Tgt = data(2:end,2:end); % readings
loss1 = (T-Tgt).^2;
loss2 = (Txdx+Q-Tt).^2;
loss = sum(loss1+loss2,1);
loss = sum(loss);
grads = dlgradient(loss,params);
end

채택된 답변

Walter Roberson
Walter Roberson 2023년 11월 25일
편집: Walter Roberson 2023년 11월 25일
[Tt,Tx] = arrayfun(@(Tscalar) dlgradient(Tscalar,t,l), T, 'uniform', 0);
Tt and Tx will then be cell arrays the same size of T, in which the entries are the gradients for the corresponding T values.
You will also need to adjust the code further down.
The basic issue is that your T is not a scalar dlarray object.
  댓글 수: 1
Alim
Alim 2023년 11월 26일
Thank you Walter. Just spot on. Indeed my T was not a scalar rather a matrix. I summed up all its elements and put in dlgradient and it worked. Thank you again

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by