Clear Filters
Clear Filters

error in neural ODE simulation

5 views (last 30 days)
I am discretizing DDE to ODE and constructing Neural ODE for this discretized ODE. But when I run this neural ODE using dlode45 in result of this I am getting complex dlarray and if i want to further proceed for loss function I get errors.
phi=@(x) cos(x); % Initial history function
g=@(x,y,par) par(1)*y/(1+y^par(3))-par(2)*x; % Mackey-Glass equation
par=[4;2;9.65]; % Parameters for the equation
tau=1; % Delay
M=30; % Number of discretization points
T=20; %
function xsol=DDE_neural(g,par,tau,phi,M,T)
nx = 1;
hiddenSize = 5;
NDDE = struct;
NDDE.fc1 = struct;
sz = [hiddenSize nx];
NDDE.fc1.Weights = initializeGlorot(sz);
NDDE.fc1.Bias = initializeZeros([sz(1) 1]);
NDDE.fc2 = struct;
sz = [hiddenSize hiddenSize];
NDDE.fc2.Weights = initializeGlorot(sz);
NDDE.fc2.Bias = initializeZeros([sz(1) 1]);
NDDE.fc3 = struct;
sz = [nx hiddenSize];
NDDE.fc3.Weights = initializeGlorot(sz);
function Y=DDEtoODE_euler(t, X_n, NDDE)
D = zeros(M, M + 1);
D(1:end-1, 1:end-2) = diag((1/(2*h)) * ones(M-1, 1));
D(1:end-1, 2:end-1) = D(1:end-1, 2:end-1) + diag(zeros(M-1, 1));
D(1:end-1, 3:end) = D(1:end-1, 3:end) + diag((-1/(2*h)) * ones(M-1, 1));
D(end, end-1:end) = (1/h) * [1,-1];
Y(1,1) = NDDE.fc3.Weights*tanh(NDDE.fc2.Weights*tanh(NDDE.fc1.Weights*g(X_n(1),X_n(M+1),par)+NDDE.fc1.Bias)+NDDE.fc2.Bias);
function bias = initializeZeros(sz)
bias = zeros(sz,'single');
bias = dlarray(bias);
function weights = initializeGlorot(sz)
Z = 2*rand(sz,'single') - 1;
bound = sqrt(6 / (sz(2)+ sz(1)));
weights = bound * Z;
weights = dlarray(weights);
here I have written only the part of neural ODE simulation
  1 Comment
Torsten on 6 Jan 2024
So you want to write your own delay ode solver because you work with dlarrays ? I wouldn't dare to do so.

Sign in to comment.

Accepted Answer

UDAYA PEDDIRAJU on 15 Jan 2024
Hi Muhammad,
Based on the code you provided, it seems that you are getting a complex “dlarray” when you run the neural ODE using “dlode45”. This could be due to a variety of reasons, such as incorrect input arguments, incorrect implementation of the neural ODE, or incorrect use of the “dlode45” function.
The occurrence of complex numbers in your neural ODE simulation might be due to the Mackey-Glass equation's term “y^par(3)” when y is negative and par(3) is not an integer. To resolve this, you can try:
  1. Ensure that the variable “y” remains positive if the Mackey-Glass equation in your model requires it.
  2. Check for numerical stability in the “DDEtoODE_euler” function and consider using a smaller integration step size or a different ODE solver if necessary.
  3. Verify that all initial conditions and parameters lead to real-valued solutions throughout the simulation.
  4. Debug the code to pinpoint where the complex values originate and adjust the model or numerical method accordingly.
You can refer to the following documentation for more details:

More Answers (0)

Community Treasure Hunt

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

Start Hunting!