error in neural ODE simulation
Mostrar comentarios más antiguos
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; %
xsol=DDE_neural(g,par,tau,phi,M,T);
function xsol=DDE_neural(g,par,tau,phi,M,T)
theta=linspace(-tau,0,M+1)';
X0(:,1)=phi(theta);
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);
xsol=dlode45(@DDEtoODE_euler,[0,T],dlarray(X0(:,1)),NDDE,DataFormat="CB");
function Y=DDEtoODE_euler(t, X_n, NDDE)
h=tau/M;
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(2:M+1,1)=D*X_n;
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);
end
function bias = initializeZeros(sz)
bias = zeros(sz,'single');
bias = dlarray(bias);
end
function weights = initializeGlorot(sz)
Z = 2*rand(sz,'single') - 1;
bound = sqrt(6 / (sz(2)+ sz(1)));
weights = bound * Z;
weights = dlarray(weights);
end
end
here I have written only the part of neural ODE simulation
1 comentario
Torsten
el 6 de En. de 2024
So you want to write your own delay ode solver because you work with dlarrays ? I wouldn't dare to do so.
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Semiconductors and Converters en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!