initialization of neural network in simulink

Hi,
I'm trying to build a neural network in simulink to control a motor. I build this system with matlab code and it works, but when I want to do it in simulink, I dont know how seperate intialization from the rest.
I mean, in matlab code, I wirte initialization code and after that I developed my system in a "for loop", so the system works with k samples.
But in simulink how should I seperate initialization from the rest of the code?
In below, there is a sample of my matlab function which I have problem with.
function [Uc,S] = fcn(Ec, Xc,Xc1,Xc2,Xc3,Ec1,S1, Ue)
d = Ue;
% err = Ec;
k = r;
x = Xc;
B=0.005; % this is network training rate
%% neural network initialization
N=4; % number of hidden neuron
dim=4; % number of input neuron
Wij=0.05*rand(N,dim); %input to hidden weights
W1j =0.05*rand(1,N); %hidden to output weights
%% neural network
for j = 1:4
S(j) = tanh(Wij(j,1)*Xc+Wij(j,2)*Xc1 + Wij(j,3)*Xc2 + Wij(j,4)*Xc3);
end
% input layer
net1 = [Xc;Xc1;Xc2;Xc3];
O1 = net1;
% hidden layyer
net2 = Wij*O1;
O2 = [S(1),S(2),S(3),S(4)];
%output layer
net3 = W1j.*O2;
u = net3(1,1)+ net3(1,2) + net3(1,3)+net3(1,4);
%% updating weights
% Updating W1j
sigma = sqrt(B)*Ec1 + d;
for j = 1 :4
W1j_new(j) = sigma /(4*S1(j));
end
%Updating Wij
if i == 1
for j = 1:4
Wij_new(j,1) = 1/(4*Xc)*tanh(sigma/(4*W1j(1,j)));
end
elseif i == 2
for j = 1:4
Wij_new(j,2) = 1/(4*Xc1)*tanh(sigma/(4*W1j(1,j)));
end
elseif i == 3
for j = 1:4
Wij_new(j,3) = 1/(4*Xc2)*tanh(sigma/(4*W1j(1,j)));
end
elseif i == 4
for j = 1:4
Wij_new(j,4) = 1/(4*Xc3)*tanh(sigma/(4*W1j(1,j)));
end
end
Wij = Wij_new;
W1j = W1j_new(4,:,1);
Uc = u;

Antworten (1)

David Willingham
David Willingham am 15 Jun. 2021

1 Stimme

Hi,
Are you looking to train a NN model directly from Simulink? Or run a pretrained model in Simulink?
A typical workflow we see is to use MATLAB to first train your neural network. Once you have trained your neural network, you can use the GENSIM command from the Neural Network Toolbox to export the network to Simulink and simulate it. More information on the GENSIM function can be found at the following URL:

1 Kommentar

erfan hassani
erfan hassani am 15 Jun. 2021
Bearbeitet: erfan hassani am 15 Jun. 2021
Thank you for answering me. I do not want to use NN Toolbox.
I'll try to explain my problem more.
I'm using neural netwrok to tune PID parameters to do real time control. here is my simulink picture:
Each NNP, NNI, and NND are neural blocks this way:
As I said before, when I'm using matlab coding, I initialize my network and after that I use a for loop ( k= 1:length(t)) to tune my PID and use it for my PLANT.
But I dont know when how to this with Simulink, since I do not have for loops, and I want to do real time control.
In matlab functions like NNC or NNE, I should initilize the network and then each time the signal is coming, it should update waights and PID parameters.
My code for NNC is below, and I think I should use "flag" to do initialization for first time (case 0) not for each signal.
function [Uc,S] = fcn(Ec, Xc,Xc1,Xc2,Xc3,Ec1,S1, Ue)
d = Ue;
% err = Ec;
k = r;
x = Xc;
B=0.005; % this is network training rate
%% neural network initialization
N=4; % number of hidden neuron
dim=4; % number of input neuron
Wij=0.05*rand(N,dim); %input to hidden weights
W1j =0.05*rand(1,N); %hidden to output weights
%% neural network
for j = 1:4
S(j) = tanh(Wij(j,1)*Xc+Wij(j,2)*Xc1 + Wij(j,3)*Xc2 + Wij(j,4)*Xc3);
end
% input layer
net1 = [Xc;Xc1;Xc2;Xc3];
O1 = net1;
% hidden layyer
net2 = Wij*O1;
O2 = [S(1),S(2),S(3),S(4)];
%output layer
net3 = W1j.*O2;
u = net3(1,1)+ net3(1,2) + net3(1,3)+net3(1,4);
%% updating weights
% Updating W1j
sigma = sqrt(B)*Ec1 + d;
for j = 1 :4
W1j_new(j) = sigma /(4*S1(j));
end
%Updating Wij
if i == 1
for j = 1:4
Wij_new(j,1) = 1/(4*Xc)*atanh(sigma/(4*W1j(1,j)));
end
elseif i == 2
for j = 1:4
Wij_new(j,2) = 1/(4*Xc1)*atanh(sigma/(4*W1j(1,j)));
end
elseif i == 3
for j = 1:4
Wij_new(j,3) = 1/(4*Xc2)*atanh(sigma/(4*W1j(1,j)));
end
elseif i == 4
for j = 1:4
Wij_new(j,4) = 1/(4*Xc3)*atanh(sigma/(4*W1j(1,j)));
end
end
Wij = Wij_new;
W1j = W1j_new(4,:,1);
Uc = u;

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Deep Learning Toolbox finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2019b

Gefragt:

am 15 Jun. 2021

Bearbeitet:

am 15 Jun. 2021

Community Treasure Hunt

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

Start Hunting!

Translated by