Embedding a Neural Net

1 view (last 30 days)
S. Moore
S. Moore on 20 Feb 2015
Edited: S. Moore on 21 Feb 2015
I have a NARX model (see attached file for an illustration) to compute the voltage response of an electrical circuit. The output voltage y(t) is a result of both instantaneous and time-delayed effect of applied current x(t). Therefore, the input delay is setup to include the instantaneous value of x(t), along with past values.
I would like to set this up to operate one sample at a time in an infinite loop where the model output (voltage) is compared to the real-time measured voltage:
% Take analog-to-digital measurements of current and voltage
I_meas = current_measurement; % single measurement of current
V_meas = voltage_measurement; % single measurement of voltage
X = con2seq(I_meas); % convert to cell
% Now the neural network computes a single point of modeled voltage
Y_hat = net(X,Xi,Ai);
% Compute the difference between measurement and modeled result
V_hat = cell2mat(Y_hat); % convert back to variable
Model_Error = (V_meas - Y_hat);
repeat loop and take another sample;
How do I setup the net for this usage case?
  2 Comments
Greg Heath
Greg Heath on 21 Feb 2015
Why in the world would you want to implement this one timestep at a time???
Since there is no such thing as an infinite loop, NEVER try to design a program that has any.
S. Moore
S. Moore on 21 Feb 2015
Edited: S. Moore on 21 Feb 2015
Because I have a system that produces input and output measurements (corresponding to a current and voltage measurement) on a regular time interval. The model needs to run every time step to produce an estimate alongside the actual system that is generating the measured output. The system is presently using a Kalman filter to perform this function. The Kalman filter is nice because it adjusts itself to changing conditions. I eventually want my neural network to perform similarly once I get a stable system running.
In comparison to the existing model and Kalman filter, the NARX neural network models the system very well. The Kalman presently runs and creates a model output once every time step, using the new voltage and current measurements. In the same way, I want the neural net to run once per timestep every time a new measurement is available.
For example, a device monitoring building power quality: the device is started and the model runs continually and indefinitely. There are models running "infinitely" in all sorts of applications.
So before actually embedding the system, I'd like to simulate it first. Therefore, the simulation should take a time-series dataset and step through each measurement point, just like the measurements points will be presented to the model in real life.
% setup the net using Dataset no.1
clear
load DataSets
clear Xs Xi Ai Ts X T Y
X=con2seq(Input_1');
T=con2seq(Output_1');
net_temp=narxnet(0:5,1:5,10);
[Xs,Xi,Ai,Ts] = preparets(net_temp,X,{},T);
net = train(net_temp,Xs,Ts,Xi,Ai);
% test the net using Dataset no.2
clear Xs Xi Ai Ts X T Y
X=con2seq(Input_2');
T=con2seq(Output_2');
[Xs,Xi,Ai,Ts] = preparets(net,X,{},T);
Y = net(Xs,Xi,Ai);
perf = perform(net,Ts,Y)
% now simulate a timestepped version
% using same dataset as above, but feeds one measurement at a time
% uses a FOR loop to step through the timestep data file
% simulates real life
clear Y Ymodel Xs Xi X T error
X = Input_2;
T = Output_2;
starttime = 6;
endtime = length(Output_2);
for k = starttime:endtime
% setup the delays
Xi(1,1) = {X(k-5)};
Xi(1,2) = {X(k-4)};
Xi(1,3) = {X(k-3)};
Xi(1,4) = {X(k-2)};
Xi(1,5) = {X(k-1)};
Xi(2,1) = {T(k-5)};
Xi(2,2) = {T(k-4)};
Xi(2,3) = {T(k-3)};
Xi(2,4) = {T(k-2)};
Xi(2,5) = {T(k-1)};
% setup the inputs
Xs(1,1) = {X(k)};
Xs(2,1) = {T(k)};
% model the output for this timestep
Y = net(Xs,Xi,Ai);
% convert cell to double, so I can plot it later
Ymodel(k) = cell2mat(Y);
% calculate error of the modeled output vs. measured output
error(k) = (T(k) - Ymodel(k))^2;
end
figure
plot(Ymodel(starttime:endtime),'b')
hold
plot(T(starttime:endtime),'r')
hold off
title('Measured Data is RED, Modeled Result is BLUE')

Sign in to comment.

Answers (0)

Categories

Find more on Sequence and Numeric Feature Data Workflows in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!