Hello guys, This blog will also be somewhat theoretical. Understand guys this theory is also important to let you understand where and in what field ML can be used. In this blog we are going to discuss about “Online ML”.
Have you heard some company promotions? Some of companies promotes in such a way saying that the more the customers use their product, the greater the performance of the product becomes. Right, that means the performance of the product depend on usability. If you have heard any of such kind of promotions then basically they are talking about online ML. That means they have develop their product in such a way that anyhow dynamically the performance of the model improves.
Online learning unlike batch learning is done incrementally that means the training of the model is done incrementally and the data is given in small batch also called as Mini Batch. After every batch the model goes on improving. Since this data that is given to the model is actually small chunk of data so the training can also be done on server as in production and that is why this type of learning is called online learning.
Let me show you with an example. You have some data and you give that to a ML model. Then the model will get trained with the data. You then test the model if it is working good. Then deploy it over server. Now what happens is that you are getting continuous flow of the data from different users around the country. Now at this point the model is doing two thing simultaneously. First, it is making a prediction and Second, it is also learning from the new data. This is the core idea behind online ML.
Now you must have identified the difference between batch and online ML. Let’s discuss the real life examples of online machine learning.
Chatbot: The chatbots of huge companies like google, Alexa, Siri use online ML. They predict as in chat with us and they also learn from this data.
Swift-Keyboard: Use must have used the swift-keyboard in your mobile. As far as I have observed the keyboard learn the most frequent word typed and then when we swift the spelling it is typed automatically.
YouTube: If you must have observed if you click on a specific video watch that and come back again to the original feed, your feed changes a little according to the video you watched recently.
I guess Now you got what is Online ML. Today also most of the companies use Batch Learning, but yes it has been seen that there is a shift towards Online ML.
Now the question is when to use? See the first thing we have to watch in the problem statement is the concept drift. Now what is this Concept Drift. You pick a question and try to solve that but what happens is the nature of the problem changes drastically or is volatile. The scenarios where the nature of the problem statement changes is where you can use online ML. The scenarios maybe stock-exchange where the buying and selling of stocks happens very drastically or ecommerce platform where sometimes sales happens and the rates changes according to the seasons. In such cases where the concept itself changes you would probably prefer Online ML.
Secondly, where you want to reduce the cost. Online ML is cost effective. If you are working on a large scale of data then batch learning will actually cost you a lot to train. Because in online learning, you train your data in small chunks of data, cost is reduced.
Lastly, it is fast. No doubt, training small chunks of data reduces time and yes prediction doesn’t take time. So overall if you need to make your system fast then Online Learning is the solution.
There are many ways to implement. The simple way to implement the same is, you use online learning. We have very famous SkLearn module. SkLearn provides us various algorithms to train our data sequentially or let’s say incrementally. Like LinearRegression( you will learn in future) we have SGDRegressor(variant of LinearRegression). This works the exact same way as Linear Regression but just the way is different. And Just because the way is different it can be used for Online Learning. If you have used SkLearn you know that there is a method call “fit”. But in SGDRegressor we don’t only have “fit” but we also have “partial_fit” method. In “partial_fit” what happens is that you partially train your data by providing partial data. And again provide some partial data to train the rest and so on.. This way you can continue.
Here, in the above code you can see I have imported SkLearn module. Then at line number 3, I have taken just one row of data(just to show you as an example). Then I have imported SGDRegressor from linear_model. Then I trained the data using partial_fit and then I calculated the time taken to train a single data point. You can see the time taken was 0.004430294036865234 sec.
And then I again generated some new data ( above image — Line 1 and 2) and started training the data from the same point from where I had left the previous time using partial_fit. In this way I can train my model whenever any new training point comes for user gives a query. So can you this was like using SkLearn or I can use some dedicated libraries for the same.
The libraries like River. This Library is basically used for training on live data. There is another library called Vowpal Wabbit. This library is dedicated to train on Re-enforcement Learning but this library has the feature to train models for Online ML also.
There is a concept called Learning Rate in Online ML. Learning Rate says how frequently you train your model. No doubt, you are going to get data very consistently but it not not advised to train your data at each and every data-point. If you train your data at every data-point then your model will will change very fast and it will start forgetting the starting data points. You will not want your data to change very fast and you would also not want your data to change very slow. So this is called Learning Rate for the model to train. You have to maintain the Rate so that your model do not forget the previous data and also should learn with the new data. Setting the correct Learning Rate is the biggest challenge for business case in Online ML.
Now that we have discussed what is Online Learning and How to implement it, so Online Learning have any disadvantages. A big Yes, there are actually two big disadvantages.
- Tricky to Use: Whoever has used Online Learning knows and whatever I am learning right now am realizing that this is tricky. Tricky in the sense, training the model is not the big thing to do but expecting the same results what you want from the model is very difficult to achieve. It Maybe because of the learning rate, or the data you are processing on the server, or the speed of the data you are receiving. There maybe multiple reasons. Because your model is learning on the fly over the server, it is a difficult task. Especially when you are working for a huge company with huge data in real time there making this work as you are expecting is an art. There is no specific skills but just the tools and these libraries so there is no assurance for the enterprise business.
- Risky: Just imagine, you are training your data Online with the new data points you are receiving. Now if any how you are receiving wrong data like any spammer is trying to spam with some wrong data or your server gets hacked and you picked the data unknowingly. Then your model will become biased over wrong data. So then you will have to redeploy your model over the server again. So this is no good for you.
The only solution towards this is that you are actively designing a monitoring system to monitor such data- point maybe anomaly detection algorithm.
There is one more topic to be discussed. The difference between Batch and Online Learning. I will not discuss but just paste a diagram to you to understand.
In Online ML we learnt how we train our data using mini batches. Then we learnt how to implement the model using various libraries like SkLearn, River and Vowpal Wabble and River. Then we discussed that how much it is important it is to maintain a learning rate to ensure your model is trained very perfectly. At the very end we discussed that Online Learning also has some serious issues that has to be taken care of while expecting the results from the model. And finally concluding with the difference between Batch and Online Learning.
Important Links: River – https://riverml.xyz/dev/
Vowpal Wabble – https://vowpalwabbit.org/