Hey, everyone, and welcome back to this class.
This lecture is about how to write code independently when you're doing machine learning.
You always hear me say that the first thing you should try to do when you learn an algorithm is to go
and write it up and code by yourself before looking at someone else's code.
For some students who are new to machine learning, it's not quite clear how to do that.
So this lecture is going to explain the thought process you should go through in hopes that it encourages
more people to try to code by themselves.
I like to encapsulate this in the phrase, When it's time to code, you must code.
So remember this whenever you see some code that demonstrates the concepts we learned in this course,
even if you don't fully understand what's going on, coding and copying examples from others helps you
build muscle memory.
Sometimes people act like they are going to learn tennis by reading a book about tennis.
This is, of course, obviously not possible.
You need to learn some things about tennis, which are pure fact and reside in your brain as conscious
facts.
But then you have to go out and practice those concepts on the tennis court.
Once you've exhausted and mastered everything you know so far, then you can learn new techniques.
So you have this continuous cycle of learning new techniques and then practicing them.
Why is this?
Because learning the technique as pure fact, does it mean you truly understand it?
Practicing the technique helps you think about the technique from a different perspective.
Practicing again and again leads to new understanding, to learning in the subconscious, which is muscle
memory.
You must go through the cycle.
But some students naively think they are going to read an entire book about tennis and then be a tennis
master.
The first time they try playing this is very common, especially when all you really need to do is sit
there and watch a video and no one can force you to type.
A lot of people don't even think to try.
So just remember this.
Whenever you see code, when it's time to code, you must code.
First, we are going to talk a little bit about why you want to code by yourself a lot of the time what
an algorithm does or how it works isn't quite clear the first time you learn about it.
Every individual will have their own unique background.
So they might be familiar with the patterns and know exactly what to do.
While some individuals might have questions that no one else has.
Sometimes you gloss over details because you assume you know what's going on when really there are important
things you're not considering.
So this is how trying to code by yourself can help you.
Coding by yourself.
Forces you to think about each and every detail.
It forces you to think line by line.
You have to be familiar with the data types and the shapes of all your variables, and they all have
to fit together properly.
Kind of like Lego blocks.
As an example, you know that in order to do element wise matrix additions, both matrices have to have
the exact same shape.
So if you find that they are not the same shape, then one of your previous assumptions was incorrect.
So you should go back and correct it.
Lego blocks have to follow a specific set of rules in order to fit together properly.
If you make incorrect assumptions about how Lego blocks fit together and you try to join them, it's
not going to work without trying to build things by yourself.
You will never discover these details.
Let's now talk about how to code by yourself.
Consider this supervised machine learning scenario.
We know that we're going to have some inputs and some targets, and we want to try to make predictions
from the inputs that are very close to the targets.
We typically call the inputs X and the targets Y.
Sometimes we call the targets T.
But for the purpose of this lecture, we'll call the targets Y.
Now, this next point is a key point.
It doesn't actually matter what X is and what why is it does it matter if you're looking at an E commerce
dataset and the columns of X might be time on site, time of day?
How many pages the user looked at and so on.
You could just as well be looking at a data set of x ray images where each column is the pixel intensity
of an image.
This is the key point.
We say all data is the same.
When we do linear regression, we don't have different kinds of linear regression for e commerce and
x ray images.
Linear regression is the same algorithm no matter what your dataset is.
So we say all data is the same.
Theoretically, I could give you a data set of X's and Y's and you could train a classification algorithm
on it without me even telling you what the Xs and Ys mean.
You should get very comfortable with this idea.
Sometimes people say, well, this isn't practical because I want to do concrete examples, but that's
because they're not thinking intelligently.
In fact, this is the most practical thing we can do because it means everything we learn.
We can apply it to any dataset that exists.
It means that I can train a model on an e-commerce dataset, but I can also train a model on an online
advertising dataset without learning anything new at all.
It's truly the greatest form of lazy programming.
Learn something once and apply it to every industry.
One great consequence of all data is the same is that there is an unlimited amount of practice opportunity
for you.
You can download data sets from Kaggle, from Google, from Wikipedia, from Amazon or from anywhere
else and try the algorithms you learned.
What this means is let's say you have a data set.
You care about more than something we do in Class C and class.
We have to use data sets that everybody can understand.
For example, text and images.
Everybody should know what text and images are.
Text and images are just fundamental data types on the web.
You probably don't even think about the fact that texts and images are data because it's so trivial.
In any case, we work with text and images a lot because everybody understands them.
But let's say you're a biologist and you know about DNA.
You find DNA and genomics really interesting.
So you want to use the algorithms.
We learn on that.
Well, that's great.
And you should totally do it.
Remember, all data is the same.
So all you need to do is convert your data into the appropriate Xs in Ys to plug into our machine learning
algorithms.
The downside is we most likely won't talk about DNA in class unless it's a very simple example because
most computer scientists are not biologists, too.
So they don't know what DNA is.
They don't understand the specifics.
So an example wouldn't make much sense to them.
This is opposed to text and images, which makes sense to everybody.
Same goes with any other specialized field like finance, computer networking, cosmology and so on.
So now we can start from the right place to talk about supervised learning.
We have our exes and we have our wise.
What do we do with them in supervised learning?
We know that there are two main things we want to do.
We want to do training and we want to do prediction and psyche to learn.
All the models have the same API.
It doesn't matter if you're doing logistic regression or decision trees or random forest, all supervised
models in Saikat learn to have the same two functions fit and predict.
Fitting a model is just a synonym for training, a model.
If you don't believe me, you can go look at these Saikat documentation yourself.
When you're learning about supervised machine learning, what you're really learning is what goes inside
these two functions.
What are the parameters and how are those parameters learned?
That's really what I'll supervised.
Machine learning is filling in.
These two functions, taking this perspective is going to give you a code structure and it's going to
make things much easier to visualize in your head.
Let's now do a simple example to solidify this idea and this example, I'm going to give you an algorithm
and you will implement it in code.
There are two key points I want to make before I give you the algorithm.
First, I'm not going to give you much intuition about how it works.
Second, I'm not going to derive the theory behind why it works.
The reason I want to mention these two key points is that you should realize you don't need these two
pieces of information in order to translate pseudocode into code.
A lot of time, these three concepts, intuition, theory and implementation work to reinforce each
other.
The reason I'm focusing on implementation right now is because it's the one people most often miss or
think they don't need at all, when really it's an extremely important part of the learning process.
OK.
So the pseudocode is as follows.
In my predict function, as you already know.
I'm going to take in some input data X my predictions will be Y hat equals X times W.
You might already recognize this as linear regression.
However, forget what you know about linear regression and pretend that this is just some formula I
gave you.
What I can tell you is that we are creating some kind of regression model.
It should be clear from this equation that the parameters of the model are contained in this vector
of weights.
W I would probably also tell you that W is a vector that's the same size as the number of feature vectors
in X.
Of course this must be true in order for the Matrix multiplied to be valid.
This axis kind of a sanity check.
So you can make sure everything is making sense.
In my fit function, I want to perform this loop some number of times, since we know from the previous
slide that the parameters of the model are contained and w then it should be clear that inside the fit
function, what we want to do is update w.
Not surprisingly, that's what's happening here.
Another good sanity check.
Now, because you're already familiar with linear regression, you probably already recognize this as
gradient descent.
Again, I want you to pretend that you don't know that, but what do you know?
You know that iterative algorithms are common in machine learning and that gradient descent is among
the most common.
You could probably infer that this is some form of gradient descent, but you don't need to know what
it is in order to write it in code.
What else do we know?
You know what a for loop is.
And you know how to write one in Python.
You know that X is an end by the Matrix containing the input data.
And that Y is an end size vector containing the targets.
You know that why hat is an inside vector containing the predictions.
You know that W is a disease vector containing the model parameters.
And you know how to add, subtract and multiply these objects so we can write this algorithm without
even knowing how it works.
Of course, a lot about how it works can just be inferred from this formula.
Here is what you don't know.
You don't know T- the number of times the loop is supposed to run.
You don't know, Ayda, the learning rate.
This is completely fine.
You can't let this lack of knowledge stop you in your tracks.
The truth of the matter is there are going to be situations where you're not told the exact numbers
to plug in.
In many cases, the answer is going to be it depends on the problem.
The important thing is you have to get used to the idea of trial and error and you have to make educated
guesses based on what you already know.
So what can you do when you know that the learning rate is supposed to be a small number?
How small?
It depends on the problem.
Typically, it's a number less than one like zero point one.
If that doesn't work, you might try lowering it.
Typically, we lower it by factors of 10.
So, for example, the next learning rate we would try is ten to the minus two.
Ten to the minus three and so on.
Typically in machine learning, whether you're looking at a supervised or unsupervised algorithm, there
is an associated cost or objective function that you're trying to minimize.
Let's suppose I tell you it's square, which is typically what we would use for regression.
Well, now you have a way to choose the number of iterations of the loop and the learning rate.
How do we do this?
So inside your fit function, you would plot the cost as a function of iteration?
I always recommend doing this no matter what algorithm you're looking at.
In general, you always want the cost to converge.
The pattern you typically see is that there is a steep drop at the beginning and it slowly flattens
out as the number of iterations increases to choose the number of iterations you want to stop when the
curve is sufficiently flat.
You might recognize this as a diminishing returns scenario.
The returns diminish because at the beginning, we reduce the costs by a lot in just a few iterations.
At the end we can do a lot of iterations, but we reduce the cost by almost nothing.
An alternative is you could use a separate validation set and stop when the validation cost increases.
But that is not the focus of this lecture.
Unlike the training cost, the validation cost is not guaranteed to decrease at every round because
that is not what we are minimizing.
With respect to.
You can also use the same plot to help improve your learning rate if the cost explodes or becomes not
a number, your learning rate is probably too high.
If your cause converges too slowly, you can try increasing your learning rate.
So what does your final code look like?
Well, we know we want to create a class with at least that predict and fit functions.
You don't have to use a class, but I find that it encapsulates the code nicely and provides useful
structure.
Notice that once you have the algorithm in math, there isn't much work to convert it into no code.
You know how to do matrix multiplication.
You know how to add.
You know how to subtract.
These are basic arithmetic operations that I'm sure you know already.
If you don't know how to do these and num pi, then I have a completely free number, of course, on
the number I stack that you can take.
Here is the second part of the code where we actually create an instance of the class and then use it.
I've also included the squared error cost function just for completion sake.
What's cool about this template is that it doesn't really change from one algorithm to the next.
The structure is basically always going to be this way, at least for supervised learning and unsupervised
learning.
So as we discussed before, it is a matter what algorithm you're implementing, whether it be linear
regression, logistic regression, a neural network and so on.
They always have the predicts function and the fit function and learning what goes inside these functions
is equivalent to learning the algorithm.
So, in fact, one way to begin coding is to start with just this boilerplate code and then fill in
the blanks.