Hello.
Welcome back.
So let's see how to build these multiple input multiple output neural network.
I'm gonna make a copy of the last project
can open it
right.
So let's take a look at the um the neural network again.
So this what we have.
So we set this time given the temperature humidity and air quality we not only we don't want to only
predict whether you are sad or happy.
We also want to predict whether you are sick or healthy active or inactive.
So given the three inputs we want to get three outputs from it.
And this is what we have right.
So a way of viewing this as we mentioned earlier is to view this as a multiple input single output neural
network.
We can view this as three multiple inputs single output neural networks put together.
And what do I mean by this.
See if we take the the input we can just map all of them to a single output.
If we take the input again we map all of them to another single output and then we take all the input
we map all of them to a single output again.
So essentially this is three X multiple inputs single output neural networks put together.
So let's see how to implement this.
I'm going to come to our neural network to see file and what we're going to do is create a function
known as matrix vector multiplication.
And why am I saying matrix vector multiplication is because our weights are going to be stored in a
matrix right.
We're going to have the weight we're going to have a two dimensional array.
And when I say matrix just think of a two dimensional array.
For those of you who don't know.
Um.
So over here we have a two dimensional array.
The first row belongs to side the first row it's made up of weight that we use to compute the sad result.
The second row belongs to weight that we used to compute the sick resort.
The third row belongs to weight that we used to compute the active resort.
And when we want to view them in column in terms of columns all fares columns belong to weight that
we compute with the temperature input all second columns belong to weights that we compute with the
humidity input and all third columns belong to weights that we compute the air quality input.
So we're going to keep all of this back together in a matrix or two dimensional array and then we're
going to
and then we're going to sort of multiplied by effector rights and the vector is going to be our input
because we have just three inputs and we have this weight.
The input is going to be one dimensional Larry.
Therefore it is a vector.
And the weight as we've described here is a matrix two dimensional.
Hence we going to write a function to perform matrix vector multiplication or vector make matrix multiplication.
However you want to call it so.
Okay I'll come down here and then
let's see come down here.
I'll create a new function void.
I'm assuming a bit
void matrix
vector multiplication like this.
The first argument is going to be of type double and we're going to take a pointer to the input for
actual
The second argument is gonna be the inputs length.
We can see you into 30 to 40
inputs land.
The third argument is going to be effector to store the output because the output is going to be three
different values.
So we can store them in a one dimensional array.
So we are going to go back to you into 30 to underscore t and we said a vector to store the output.
So we see a double star you can call this output vector
and we can to improve readability.
We can just see another variable to store the outputs leant in.
In our case the output length is the same as the inputs length.
It's not always the case.
If we said we just wanted to uh if we said if we said we just wanted to predict sad and sick without
active then the outputs length it would be just too.
But this is just a coincidence that the input length is the same as the output length.
So if you want to try with a different neural network then let's make the output length flexible such
that you can pass an argument for your new output length or your custom output land.
I'm gonna say in that to underscore T over here you outlined
and the last argument is going to be a matrix to hold the weights and it's gonna be of type double
going to call this weight matrix
put an S over here and the size of this
is gonna be output length.
How do we know the size we can see from our image here.
The size of this is going to be the rows equal to the number of output at the columns because the number
of input you see the first row belongs to side which is an output.
Second row sic output third row active.
So the rows number of rows of course number of output and number of columns because number of inputs
so the size here is going to be output length
and input length here like this
and open
and close.
Right.
So now we can implement this and we are going to use a that for loop to help us do this.
Let's see you start off by saying for
I say 4 into K equals zero K is less than the output lent
K plus plus an inside this I'm going to do for I
for int i equals your eye is less than the input line.
And a plus plus open and close.
And what we are essentially trying to do is come over here take this.
Finish processing this row and then go to the next row.
Because remember input Len represent the rows.
The number of rows so the outer loop is the number of rows the inner loop is the number of input.
So when we come we first come to rule 1 and then we we process these values and then we go to row to
process these values and they rule 3.
That is what our nested for loop here would do for us and all we have to do here is say output vector
index K plus we use our accumulator we say input vector index I times.
Wait wait.
Matrix index K index I.
So you understand index K here.
So we first though it k equals zero.
And then ask k equals zero.
We do K equals zero equals zero K.
Cause your I equals 1 k equals zero aqueous 2.
So K equals zero represent this rule.
K equals zero equals zero is zero point two K.
Close your eyes because 1 it represents you point one K cause your eye equals to represent zero point
five.
So we process this and then it becomes k equals 1 and then we do K K was 1 equals U K equals 1 I of
course one in a K cos one equals two.
And then it becomes k equals 2.
Over here I know you get it.
Okay.
So that's how we will process this.
So this all we have to write for this and we can wrap it around our function known as multiple input
multiple output and n come over here.
Void
what will inputs
which will outputs n n.
And we take the same document a copy over here.
Okay
um let's see
come down here and open and close.
Right.
And we simply need to call our function our matrix vector multiplication function
and we pass the same document the first one is input vector
the second one is input lent
the first output vector
then outputs land and then way to matrix
right.
So put a semicolon here and we can expose this function
bring it over here
patient over here on a semicolon here
right.
So we can try it out in our main file.
We can start off okay.
We have sat here at zero point nine.
We don't need this.
We can just clean everything to prevent confusion.
Right.
So I'm gonna put some defined statements here to hold the indices.
I'm gonna see a sad prediction index.
So the resort offside is going to be stored in the first index of the output vector.
Then we're gonna have six prediction index.
So it's sad or happy sick or healthy active or inactive
and then when I say define output length here
defined in Poland
and we know our input is three in our output is three as well.
That's why I'm pausing three and three here and we have to create the effect or to hold the predicted
values
like this.
Right.
So this what we have now.
Okay let's declare our wait let's say double.
Remember we said a way is a two dimensional array and I'm going to say weights and the sizes outputs
led by inputs Len.
So the first row belongs to the decide prediction.
So we can see it a way to minus zero point two
hope on nine point five.
And then let's see two points 0 1 over there and then we go to row 2.
This is going to be for the sake of prediction
C minus four point eight seven point two six point three.
And then we can go to the third row.
There's going to be for the active prediction.
C minus four point five point four five.
And let's say zero point nine
like this.
And so I just put a bit of comment here.
The first column belongs to temperature.
The temperature puts the second column as humidity and this one the last one is air quality.
And when we want to view it according to Rose The first rule belongs to Saad
Sardo happy.
The second rule is sick or healthy and the last rule is active or inactive.
Right.
So once we've done this less let's say we have some input I'm going to create an input vector here
and the length of the vector is the inline
and we can see temperatures 30 degrees Celsius and humidity we can see it's 87.
Air quality can be at 110 and it will be helpful to have a comment here showing you our input arrangement
as well
right.
So once they start we can call our new network function.
So I'm going to come over here and say
multiple inputs multiple up and the first document as you can see over here is the input vector which
we call input or input vector here.
The second argument as you can see over here is the output vector.
And we call that I think the second argument is the input length and we have a defined statement to
that in Len.
The third argument is the output vector which we call predicted output.
The next argument is the output length which we have out Len.
The argument following that is the the weight matrix which we call weight
and that's all right.
So we can print some resorts here or come over here and see print f
sad prediction but this percentage F and then output courage return and then what I want is a one to
go to our predicted output vector and navigate to the sad prediction sad prediction index and I can
do the same for the rest sake prediction and active prediction
right.
So this order is I've got to build and see what it looks like.
We've got no arrow Click here to download onto the board let us open to return
or reset my board.
So this what we have.
Right.
So based on the weight this what we get sad prediction is this sick prediction is that an active prediction
and like I said the meaning of all of this can just be a way of encode in words to numbers such that
we know a particular numerical value represents what are you sad or sick.
And that I want to represent what you are you know whether you are sad or happy and I want to represent
what I you are sick or healthy etc. So this how to implement the multiple input multiple output neural
network and I'll see you in the next lesson.