"RecordingArrays cannot be used across tapes" when backpropagating dlode45

7 views (last 30 days)
Hi! The program works well but throws error "RecordingArrays cannot be used across tapes" from dlgradient when I added 'GradientMode="adjoint"' to dlode45,
Error using deep.internal.recording.RecordingArray/recordBinary
RecordingArrays cannot be used across tapes.
Error in deep.internal.recording.RecordingArray/permute (line 13)
x = recordBinary(x,order,op);
Error in dlarray/interp1>iGetDataV (line 131)
dataV = permute(dataV, viewToData);
Error in dlarray/interp1 (line 62)
[dataV, dimsV] = iGetDataV(v, isvector(xq));
Error in NeuralODE_Lstm_WindEppFiber_red_v2_adjGrad>odeModel (line 1344)
F_t = interp1(t_ref, F, t)'; % Current excitation
Error in NeuralODE_Lstm_WindEppFiber_red_v2_adjGrad>@(t,y,theta)odeModel(t,y,theta,F,tspan) (line 1322)
X_i = dlode45(@(t, y, theta) odeModel(t, y, theta, F, tspan), tspan, X0, neuralOdeParameters, DataFormat="CB", GradientMode="adjoint");
Error in internal_dlodeBackward>iComputeBatchedAugRightHandSide (line 141)
f = dlodefun(t,z,theta);
Error in deep.AcceleratedFunction/parenReference (line 255)
[varargout{1:nargout}] = obj.Function(inputs{:});
Error in deep.internal.fevalWithNewTape (line 21)
[varargout{1:nargout}] = fun(varargin{:});
Error in internal_dlodeBackward>augDynamicsRightHandSide (line 113)
[f, adf_dz, adf_dtheta] = deep.internal.fevalWithNewTape(gradientFcn,...
Error in internal_dlodeBackward>@(t,s)augDynamicsRightHandSide(t,s,originalTheta,dlodefun,yStateSize,numObs,originalSize,originalFormat,numLearnables,gradientBatchFcn) (line 44)
augDynamics = @(t,s) augDynamicsRightHandSide(t,s,originalTheta,dlodefun,yStateSize,...
Error in internal_dlodeBackward (line 63)
f0 = augDynamics(initialTime,augState);
Error in deep.internal.recording.operations.DlodeOp/backward (line 81)
[outputs{:}] = internal_dlodeBackward(dy,y,op.Y0Format,op.Odefun,tspan,op.Options,...
Error in deep.internal.recording.RecordingArray/backwardPass (line 89)
grad = backwardTape(tm,{y},{initialAdjoint},x,retainData,false,0);
Error in dlarray/dlgradient (line 132)
[grad,isTracedGrad] = backwardPass(y,xc,pvpairs{:});
Error in NeuralODE_Lstm_WindEppFiber_red_v2_adjGrad>modelGradients (line 1334)
gradients = dlgradient(loss, neuralOdeParameters);
Error in deep.internal.dlfeval (line 17)
[varargout{1:nargout}] = fun(x{:});
Error in dlfeval (line 40)
[varargout{1:nargout}] = deep.internal.dlfeval(fun,varargin{:});
Error in NeuralODE_Lstm_WindEppFiber_red_v2_adjGrad (line 486)
[gradients, loss, X_i] = dlfeval(@modelGradients, t, XTrain_Tr, index_batch, neuralOdeParameters, YTrain_Tr, U_Peak_mean);
Here is the odeModel:
function y = odeModel(t, y, theta, F, t_ref)
% Inputs:
% t = time
% y = state vector
% theta = parameters
% F = excitation
% t_ref = time points of the excitation
F_t = interp1(t_ref, F, t)'; % Current input signal, interpolate to find the signal value at t
y = tanh(theta.fc1.Weights*y + theta.fc1.Bias);
y = tanh(theta.fc2.Weights*y + theta.fc2.Bias) + theta.fc3.Weights*F_t;
end
And looks like the error is within "interp1". However, even I tried to replace the "interp1" by the following lines of code the error presists.
t_left = t_ref(t_ref<=t); F_left = F(t_ref<=t,:);
t_right = t_ref(t_ref>t); F_right = F(t_ref>t,:);
t1 = t_left(end);
if isempty(t_right)
t2=-1;
F2=0;
else
t2=t_right(1);
F2 = F_right(1,:)';
end
F1 = F_left(end,:)';
F_t = (F1*(t2-t) + F2*(t-t1))/(t2-t1);
Thanks in advance!

Answers (1)

Yash
Yash on 16 Jan 2024
Hi,
The error "RecordingArrays cannot be used across tapes" is thrown when using dlgradient with the 'GradientMode="adjoint"' option. This error occurs because the interp1 function is not supported in the adjoint gradient mode.
You can try using a different interpolation method that is supported in the adjoint gradient mode, such as linear interpolation or spline interpolation. You can use the 'griddedInterpolant' function to create an interpolant object and then evaluate it at the desired time points.
You can refer to the documentation of the 'griddedInterpolant' function here: https://in.mathworks.com/help/matlab/ref/griddedinterpolant.html
Hope this helps

Categories

Find more on Special Functions in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!