In this lecture we are going to look at extending our MLP example so that we can use a trained model
to make predictions.
A lot of people ask this question and at a high level you just need to follow the same steps that we
did earlier to pre process the text and call the model predict function.
However some students get overwhelmed with the amount of code and so hopefully this lecture will clear
up any confusion you may have had.
Before we start first recognize that we are in a separate VIP notebook which you can find in the VIP
notebooks document.
So to start we're going to do a few things differently in this notebook to kind of extend what we discussed
earlier.
So if we scroll down to after the model is trained you'll see that I also do a check for how many of
our observations are actually spam.
As you can see it's not that many.
Only about 13 percent.
This is an example of a data set with imbalanced classes.
So when we see accuracy in the high 90s we might be concerned that our model is just predicting the
majority class all the time.
Next we're going to look at the confusion matrix to see if our model is doing well on one class but
not on the other.
So I've pasted the same confusion matrix code from earlier in the course in this block if we scroll
down to the results we can see that we actually do well on both classes.
So this goes for the tests.
In with the train said
so it seems that even though the classes were imbalanced this did not pose a problem for the model
next.
Let's go onto you making a prediction on a new test sentence.
There are two ways and one to show you how to do this.
The first way is closer to what we did earlier in the script which I think is easier to understand.
That is we want to start with a C S V file containing our data.
So first I have a snippet of code here that shows you how to sample from the data frame but only for
the rows with a label that spam
in the next block.
I assign this to a variable called small sample and then I write this to a C as V file called sample
test starts yes.
Note that the only thing I'm writing to this file is the sentences themselves not any of the other columns.
So in the first line I say open a sample test DRC is V in write mode in the next line.
I write the hetero which is just the string data.
Then I do a loop through the small sample data frame using it rose function inside the loop I select
the data column from the row and then write back to the file along with the new line
next we use the cat command to confirm that our CSB has been written properly.
As you can see it contains only the data column with the corresponding spam sentences.
The next step is to create a tabular data set from our CSP.
This is almost the same as the code from earlier.
But notice that in the fields argument I only have one field which is data.
I also pass in the text field object that we created earlier.
Since this contains all of the information about tokenization and the vocabulary and so forth from the
train set
next I create an iterator object from the data set object which is similar to what we did earlier when
we called iterator dot splits.
As you can see it takes in almost the same arguments a dataset a batch size a sort key and a device
but you'll notice that before the splits function it took in multiple datasets whereas this takes in
only a single dataset.
Now you might wonder how did I figure out such an object existed.
Well guys you have to read the documentation all the information is there
in the next block.
We loop through our sample iterator so this is the moment of truth where we get to find out whether
this will work or not.
As you can see it's slightly different from what we had earlier in the previous loops.
We got back the data directly as tenses but it seems that in this loop the data is returned as a batch
object.
We can access the data by calling inputs that data.
Unfortunately the API for the iterator object versus iterator that splits is inconsistent.
Well you just have to observe what's going on and make the necessary changes.
Nobody said you wouldn't have to put in some effort as you can see our predictions come out just fine.
Now your next question might be what if I have just a single sentence and I don't want to write it to
ICAC.
In this case we can still make use of the torture tax documentation to figure out what to do so we'll
start with a sentence from the spam set and I'll assign this to a variable called single sentence the
next step is to use our text field object and call the pre process function on this single sentence.
Here I'm just printing out the result to confirm that it does the right thing.
As you can see this only does the tokenization part but it does not map the tokens to integers
the next step is to call the numerical I's function which converts the above token I sentence into a
sequence of integers.
Unfortunately the API is a bit inconsistent here again since this accepts a list of token sentences
rather than just a single token a sentence.
Finally the last step is just to put these all together and then use our model to get a prediction.
So in the last block of code we call text pre process to convert our sentence into tokens.
Next we call numeric allies to convert our tokens into a sequence of word indexes.
Lastly we call our model and we pass in the sequence of word indexes.
After moving the input tensor to the Jeep you
as a final sign for this lecture you may want to check out another notebook I created which uses the
Cara's API to pre process the text.
It might surprise you that this is possible but in fact since the carries API just gives you back a
num pi array.
There is no mixture of Kerry's code with PI torch code.
I found that this was advantageous in a few ways namely that it achieves better accuracy perhaps due
to smarter default tokenization and the API is easier to work with and more consistent than torch text.
You can find this notebook along with the rest of the VIP notebooks in the extra section.