So in this video, we will continue looking at the notebook for human activity recognition, we just
learned about how the data is organized so we have a decent enough understanding in order to pass it
into the format we need.
So let's start by defining a few constants.
Note that you can get these values by looking at the data files.
So we'll start with big tea, which we know is one twenty eight.
Next, we have entraining and test, which are as follows.
Basically you can find these by counting.
The number of rows in any of the data files is equal to nine since we have nine parallel time series
and K is equal to six since we have six classes.
OK, so the next step is to define a function and call the load data.
Again, it helps to think about what we need so that the code we write will go towards that goal.
And of course, this just goes back to what we learned in the section about Time series basics.
We'd like to have an X array of size and by TBD Biddy's and we'd like to have a wide array of length
then.
And this goes for both training and test.
OK, so we'll start by reallocating our eggs train a next test to raise, both of which are invited.
The next step is to instantiate a list containing the file names of each file, which contain our Time
series.
Note that this is for train.
The next step is due to find a far path prefix, which is just the folder where these files are located.
The next step is to live through each of the file names, since we use the enumerate function.
This gives us the index as well.
OK, so inside the loop, we begin by using pandas to load the file into the data frame.
You should recall this code from earlier.
The next step is to immediately convert the data frame into an empire which we call Little X. Now,
again, it's always helpful to think about shapes.
So what is the shape of little X?
Well, it has nine rows and rows a time series of length, Big T, therefore, little X has the shape.
And by T this makes sense since we have a D of these files and if we join them all together, we get
invited.
OK, so this means we have to index X train in the third dimension with the index little D, we assign
this portion of X train to be a little X, OK, so hopefully this all makes sense.
By the time this loop is over you will have all nine time series stored inside X train.
The next step is to load in our labels again, we can use pennies for convenience and immediately convert
it into an empire.
Since the labels start from one, we are going to subtract one so that they all start from zero.
We also want to flatten the labels, since using a data frame will give us to the objects of shape and
buy one for this code.
It's better to just have a onesie array of length n.
OK, so the next step is to do the exact same steps, but for the test set, since this code is pretty
much all the same, there is nothing to explain.
Basically anywhere you previously saw train, you now see test.
OK, so the next step is to use our function, which gives us X and Y for both training and test.
Now, just as a sanity check, let's plot the multivariate time series of a single sample of chosen
sample seven thousand for no particular reason.
OK, so let's think about what the shape of this data will be since we index to an array of size and
by TBD at some row, the result will be of shape TBD.
So this is a single multivariate time series of length T with dimensions.
OK, so this is a plot of all the details series at once, nothing special, but it gives you a sense
of what the data looks like.
OK, so the next step is to build our model, we'll begin by creating two empty lists to store inputs
and features.
The features are just the output of each mini neural network that will process different dimensions
of the Time series.
The next step is to enter a loop that goes these times inside the loop.
We declare an input object with the shape T. So this is what we have for processing a univariate time
series.
Basically, we're just processing a univariate time series D Times.
The next step is to create some dense layers.
Note that I've chosen the number of hidden units in the number of hidden layers arbitrarily, so please
feel free to try different things.
At this point.
The final X variable refers to the output of this many neural network.
So we append AI to the list of inputs and we appendix to the list of features.
OK, so once that loop is done, the features list will contain the outputs of all the mini neural networks.
The next step is to create the concatenate layer, which joins other features together.
So since you have nine feature vectors each of size 16, the result will be a big feature vector of
length, nine times 16.
Once we've done our concatenation, we have one final dense layer with K outputs as usual.
The last step is to create our model objects passing in the list of inputs and the output x.
OK, so when you have a complicated model like this, it's useful to create a picture of your model,
the tensor flow plot model function can be used to do this.
So as you can see, our model is basically nine parallel mini neural networks, all taking in a different
part of the multivariate time series.
After the data goes through these many neural networks, they are combined together into a single feature
vector.
That single feature vector then goes through one final dense layer.
OK, so this is a multi tailed neural network.
The next step is to create a checkpoint.
This will be used to save the best model according to some validation metric during the training process.
So we have three arguments.
The first argument is the file name to save the best model to the second argument is which metric to
monitor.
You could choose the laws here, but since we are paying attention to accuracy, that's what I've chosen.
The third argument is used so that it doesn't save every model, but only the best model.
OK, so the next step is to call the compile function, this is similar to what we've seen before,
except for two things for the loss.
Instead of specifying a string, we specify an object of type, sparse, categorical cross entropy.
We're going to set from sequel to true since our model outputs Logits.
If we use the default value of false, you would have to end your neural network with a softmax.
You don't have to worry about why we do this, but the reason is for numerical stability.
As always, you can check extra reading text for more information.
The next thing to notice is that we've passed in accuracy as the metric to track.
So when we're doing forecasting, this is not really needed.
Since the mean squared error is both the loss and the metric we to track for this problem is different
because the loss and accuracy are two separate things.
The next step is to convert our data into a format that will work with our neural network.
As you recall, our model is defined to take in a list of inputs.
It does not accept an array of size N by T by D directly.
That would be the case for CNN's and Arnon's, but it's not the case for a special, multi tailored
model.
So the function we built earlier is more general in that IT future proves us for the later sections
on CNN's in Arnon's.
So basically we're going to live through each dimension of our Time series and separate each dimension
so that we get a list of univariate time series.
The next step is to call our fifth function, so the arguments to this are the same as we've seen before,
except we are now passing in a list of inputs where X would normally go.
And we have our checkpoint's.
The next step is to plot our lansberry prepack.
OK, so here is our Lansberry Park.
Notice that the train loss is much better than the loss.
And this is our accuracy report.
So we see the same pattern as the Los.
The next step is to load back our best model by calling the load model function, notice that we pass
in the filename that matches with our checkpoint.
The next step is to call the Predix function to get our test predictions.
The next step is to check the accuracy of our model.
OK, so what's happening here?
So as you recall, test is an endemic matrix of probabilities.
In order to convert these to labels, we need to take the Amax on each row.
The results of this will be a one degree of predictive labels.
OK, so once we have the predicted labels, we can use the equals equals operator to compare them to
Y test.
So this does an element wise comparison and the result will be an array of booleans either true if we
are correct and false.
Otherwise, as you recall in Python, a true is treated like one and false is treated like zero.
Therefore, when we take the mean of this array, we get the accuracy, which is ninety two point six
percent.
OK, so this seems like a pretty decent result.
But then again, we don't have any point of comparison.