How to train a neural network with multiple samples of inputs and outputs

4 views (last 30 days)
I am currently designing a neural network that takes in two input matrices (1 x 1000 and 4 x 1000) and outputs a matrix of size (4 x 1000). The 1x1000 sized input remains the same for all training. However, the 4x1000 input matrix is sampled from a 20x1000 total input matrix (5 sample inputs). The output matrix is also sampled from a 20x1000 total output matrix (5 sample outputs). Essentially, given a certain input of the (4x1000) matrix, we should expect a corresponding (4x1000) output matrix. The way that the data is being trained on can be seen below:
%% Neural Net Training Process
t = linspace(0,1,1000);
% Parsing Training Input Data (Terminal Conditions Matrix)
x_term = zeros(20,1000);
xf1 = ones(4,1000);
xf2 = 2*ones(4,1000);
xf4 = 4*ones(4,1000);
xf5 = 5*ones(4,1000);
xf6 = 6*ones(4,1000);
x_term(1:4,:) = xf1;
x_term(5:8,:) = xf2;
x_term(9:12,:) = xf4;
x_term(13:16,:) = xf5;
x_term(17:20,:) = xf6;
x_input = {t;x_term};
% Parsing Training Output Data (State Trajectory Matrix)
x_output = zeros(20,1000);
[~,x1] = ode45(@(t,x1) 2*x1,t,[1;1;1;1]);
[~,x2] = ode45(@(t,x2) 4*x2,t,[1;1;1;1]);
[~,x4] = ode45(@(t,x4) 7*x4,t,[1;1;1;1]);
[~,x5] = ode45(@(t,x5) 8*x5,t,[1;1;1;1]);
[~,x6] = ode45(@(t,x6) 0.5*x6,t,[1;1;1;1]);
x_output(1:4,:) = x1';
x_output(5:8,:) = x2';
x_output(9:12,:) = x4';
x_output(13:16,:) = x5';
x_output(17:20,:) = x6';
% Neural Net Architecture Initialization
netconfig = [40 40];
net = fitnet(40,'trainFcn','trainbfg');
net.numInputs = 2;
net.inputConnect = [1 1; 0 0];
%net = configure(net, x_input, x_output);
% Training the Network Over Five Trajectories
for i=1:4:20
curr_x_term = x_input{2}(i:i+3,:); % 20x1000 input matrix is split into 4x1000 separate matrices
curr_xin = {t;curr_x_term};
curr_xout = x_output(i:i+3,:); % 20x1000 output matrix is split into 4x1000 separate matrices
net = train(net, curr_xin, curr_xout);
end
% Testing Network (Testing network with a selected test trajectory)
test_x_term = x_input{2}(1:4,:); % first sampled trajectory (4x1000) that was trained on
test_xin = {t;test_x_term};
Y = net(test_xin);
As can be seen by running the network, the neural network is trained on each sample. However, the output of the neural net will only correspond to the last sample that it was trained on. I am wondering if there is a way to train the network in a way where the entire 20x1000 input and output matrices can be used at one time while the network is able to distinguish between each sample when training. If any advice can be given in regards to this issue, it would be truly appreciated.
  1 Comment
Devin Hunter
Devin Hunter on 9 Aug 2022
Also, I am aware that the output of Y may not perfectly match with the output given from the last sample, but I placed these ode functions within the script so that you all could see how exactly my data is structured in the algorithm that I am training on. My main question is regarding the methods of sampling data when training the network so that the network can differentiate between each sample.

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!