# How to give multiple inputs to the train function of Neural Network?

254 views (last 30 days)
MathWorks Support Team on 1 Sep 2017
Edited: Demet on 25 Mar 2022
I am creating a neural network in MATLAB and am trying to feed it multiple inputs. How do I define the inputs and feed them into the network? If input 1 is some matrix of numbers and input 2 is another matrix of numbers, how do I correctly define both inputs? And then how do I feed them into the network?
For example, if I do "train(net,input,target)", then how do I send all of the inputs?
Also, how do I connect both the input blocks to the network?

MathWorks Support Team on 1 Sep 2017
Consider the following examples:
Given two inputs, x1 and x2 - You can create a cell array, x which can be then passed to the "train" function.
x1 = [4 5 6];
x2 = [0 1 0];
x = {x1;x2};
t = [0 0 1];
net = feedforwardnet;
net.numinputs = 2;
net = configure(net,x);
net = train(net,x,t);
view(net)
For both the input blocks to be connected to the neural network, there are multiple ways depending on the architecture you require:
You can connect the inputs to the network by altering the "inputConnect" property of the neural network as below:
x1 = [4 5 6];
x2 = [0 1 0];
x = {x1;x2};
t = [0 0 1];
net = feedforwardnet;
net.numinputs = 2;
net.inputConnect = [1 1; 0 0];
net = configure(net,x);
net = train(net,x,t);
view(net)
or
x1 = [4 5 6];
x2 = [0 1 0];
x = {x1;x2};
t = [0 0 1];
net = feedforwardnet;
net.numinputs = 2;
net.inputConnect = [1 0; 0 1];
net = configure(net,x);
net = train(net,x,t);
view(net)
Bhushan Khirwadkar on 3 Sep 2021
The above problem if the no of inputs are increased from 2 to 3 then what changes should be made in the code to run the neural network with 3 inputs and 1 outputs. Not sure, but which line should be changed to get it done?
net.numinputs = 2;
net.inputConnect = [1 1; 0 0];

### More Answers (4)

javad moini on 18 Jul 2019
can you please explain more about variable t at train function ?
##### 1 CommentShowHide None
Julia Cooper on 20 Aug 2019
Edited: MathWorks Support Team on 19 Apr 2021
Please refer to the following documentation page for more information regarding the "T" parameter of the "train" function:

Marcelo Olmedo on 6 May 2020
Hello! Attached example taking data from a specific range in an Excel file. The key is in the input data composition. I hope it works. Cheers
##### 1 CommentShowHide None
Laura Payo Fernández on 25 May 2020
Hi Marcelo,
I have the same problem that it was mentionated above. I have a cell array with 80 matrix of 3x5. I want two inputs and I did that in this example, so now, I have a cell array with 80 elements in which there are two cell arrays of 3x5.
I put that in my network and I obtain:
Input data{1,1} is not numeric or logical.
I don´t know how I can do for this, Can you help me please? I have been trying a lot of things.
Thanks

Xin Yee Tai on 11 Jun 2020
Hello,
Thanks again for the idea with multiple inputs in building the neural network.
For my project, I would like to build the neural network 2 inputs with single output. In the other input, I would like to add the corresponding output into the neural network too. However, based your solution, it allows me to add input in the neural network instead of corresponding outputs. For example I have the training input of 8x3000 and output training of 2x3000. Now I have additional data (10x200) which I hope to be added so that it can be the 'guard' during training (which means the training data might be affected by these additional data during training). Therefore, I hope to build the neural network structure which allow me to add the additional inputs and the corresponding outputs as the auxialiary input of the neural network.
I hope any experts can help me on this! Thank you!
##### 1 CommentShowHide None
alsharif taha on 5 Dec 2020
how to input logical data to matlab to realize forexample an AND gate . i have tried to train this net but it gives error
p1=[0,0]
p2=[1,0]
p3=[0,1]
p4=[1,1]
t1=0
t2=0
t3=0
t4=1
p=[p1 p2 p3 p4]
t=[t1 t2 t3 t4]
net1=newff(p,t,3)
net1=train(net1,p,t)
Error using network/train (line 340)
Input data size does not match net.inputs{1}.size

Demet on 19 Mar 2022
Edited: Demet on 25 Mar 2022
This is an example of what the network configuration should look like if you have a two-dimensional matrix data for each inputs:
a=[4 5 6; 5 5 8; 8 9 2; 8 9 6 ]
b=[8 9 5; 7 8 8; 8 8 6;8 9 2]
inputData={a;b};
target = [0 0 1];
net.numinputs = 2;
net.inputConnect = [1 1; 0 0];
net = configure(net,inputData);
net = train(net,inputData,target);
view(net)
The layout required for it to have three inputs:
a=[4 5 6; 5 5 8; 8 9 2; 8 9 6 ]
b=[8 9 5; 7 8 8; 8 8 6;8 9 2]
c=[7 7 7; 7 7 7; 7 7 7;7 7 7]
inputData={a;b;c};
target = [0 0 1];
net.numinputs = 3;
net.inputConnect = [1 1 1; 0 0 0];
net = configure(net,inputData);
net = train(net,inputData,target);
view(net)
This shows what any data should look like to run the network:
for r=1:(length(target)) %number of samples=3
for t=1:(length(inputData{1,1}(:,1))) %dimension of the network=4
for z=1:(length(inputData{1,1}(1,:)))%number of inputs=net.numinputs
dataInput{z,1}(t,r)=inputData{r}(t,z); %be sure that your input data looks like that
end
end
end

### Tags

No tags entered yet.

R2017a

### Community Treasure Hunt

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

Start Hunting!