initialization of neural network in simulink
2 次查看(过去 30 天)
显示 更早的评论
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;
0 个评论
回答(1 个)
David Willingham
2021-6-15
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:
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Deep Learning Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!