Devreal

Real Time Machine Learning Visualization...

Event: Data by the Bay

data.bythebay.io: Chester Chen, Real Time Machine Learning Visualization with Spark

Recording: data.bythebay.io: Chester Chen, Real Time Machine Learning Visualization with Spark

so um today I'm going to talk about realtime machine learning visualizations uh with spark so uh the first you know he briefly introduced I'm the director of engineering at Al and data so uh I'm also the founder organizers of this Meetup uh called San Francisco big Analytics currently we have close to 42 uh 4200 members uh previously I have working from big and small companies from You Know Places um I've been you know poking around the spark since spark 0.6 so um but mainly most of the work is really uh at Alpine which where we did a lot of Integrations with the spark in from window one window 3 1.5 currently W 1.5.2 so uh and Alpine RS on different Hado distributions and so for example different version of clera H works and the map parts and we also support pival and all that and big insights and all that stuff so uh just one slid on Alpine so um Alpine is a a predict Predictive Analytics uh platform for Enterprise so uh we've been working uh we have this visual UI allow you to drag and drops to do analytics on it and uh we have a management piece alligned to managing the relationships uh between data science and uh tracking the workflows uh so in particular related to spark and we are the first one uh being certified on the advanced analytics on spark so I don't need to talk about what it Sparks so okay so so um before I jump into the real-time visualization for the machine learning so I want to quickly just go through very simple machine learning algorithms and I know this is probably is just uh for many uh many of you already know this I just want to have one simple thing so we are on the same page so this is in this case we're talk about cing so uh in this diagrams we see a cing with three clusters and I'm using this uh public available data sets called IRS data sets so um so here we have a three clusters cluster zero one and two and the Big Dot in the in the middle of the cluster called centroid so um so the king really just try to you know uh find the the optimum distance which the distance is measur become centroid uh to the any points of the data sets so what exact came means and if you look at the wikipedias essentially in find the distance and try to minimize the distance uh find the centroid and and then try to minimize the distance uh to to the Clusters to each point and and there's a mathematic equations you can find out and then try to minimize that so given that so we for each calculations of a machine learning optimizations essentially there's a cost function goes with it the cost you know here's the plot the cost versus the iterations so if you see the cost essentially after certain points mostly flat toad even though they gradually uh decreasing um but pretty much if you're experimenting with the machine machine learning you probably want to say okay after 10 iterations I think that that's good enough so I can want to stop so you you want to see how the cost function gradually comes down so this is the work we talk about so um so the real time machine learning visualization so why we want to do that we we need a way to visualize the machine learning training process uh you know including convergence clustering you know residual plots or uh if you see the uh the cost has pretty much a flat toe you want to stop the iterations just gather the model then and then you know maybe try some other variations as other models uh or you want to say okay this is good enough I have seen enough of this just disable the visualization you know don't cause taking more time just let it finish so so there's a there's a way things you want to do so with that I want to quickly do a demo so this is a a quick time of video I pre-recorded uh because I don't know internet connections and all that stuff so uh give me a second put this here so uh here's what the aline the uh workflow engine looks like and I I directly put this work in the Aline product so so I can quickly uh show this uh so basically you can drag say for example a file or a tables or you know whatever Hive tables database tables in this you know or Hado tables uh Hado files so uh we dra there and we usually you do U some variable cing and some variable substitutions and then eventually do some transformation and then you connect to a machine learning algorithms in this case I just directly connect to cing uh and and so because I wanted to do a quick demo uh so let's quickly run it and uh I want to show so here let me stop a second so here you can see the data set you know I don't know you can see it it's pretty small uh the file size about 10 gig and I just double click the the properties so you can see that I set the the max iteration is about 20 this is too small so just believe me so uh and I'll let it run so let it run fast so I make it really so uh so we you know you notice that there's the tracking URLs you can click and then we can bring either the log or the spark UI um so here I put a two views side by side so um yeah maybe I run little a little bit faster things will happen after so because initially the spark started to to launch the jobs and then do so the machine learning haven't really hit it yet so uh it's well I think it maybe another let me make it a little little faster okay all right so uh so let's make it normal speed so um I probably running too fast but essentially at certain points we see that uh the data start to come in and then and and then the the cause functions stting U flat to and we have a two views um against the two different plots and you know uh I think the first one is uh this is the airline data so basically it's a uh departure time versus arrival times so the other one is distance versus arrival times so you can plot different uh clusters and then the central will you know see that but in this case because it's pretty much flat to after certain points and you can click stop there and then that will stop the iteration just to gather The Gather the um uh gather the the stuff here you notice here you know when I click stop the the log basically you know print out and say hey stop two and so iterations uh in the message being passed to the to the spark will come back uh so this kind of uh uh let me just uh quickly let it go to the end so so we also have a a sliding bars so essentially allow you to uh slide back and forth you can if you want to replay the uh uh replay the uh see how how the training how the Sid actually removed you want to see it again and and allow you to do that because uh once this flight to the croid is pretty much not moving it's only at the beginning you know the centroid moves so that's kind of the demo so uh with that let me close this go back to our presentation okay so how do we do that so essentially how do we enable this uh real time visualization so um because that's just a demo so if you're say hey if I want to myself in in our system not in Alpine so what's the necessary steps you you you can make this happen so essentially you need a four Parts uh first one is a call callback interface in the spark machine learning algorithms to send messages and uh the algorithm will decide what to send in the K me's case where you send you know s CH it you may send in the uh sample datas uh but he doesn't know how actually how the message got delivered he don't care and so he say here's the message send it over the second part is a test channel to handle message deliveries in this case is a from St driver to the spark client in this case the test Channel his only job is basically deliver the message he doesn't care what the messages is so and once the message is delivered to the spark clients and basically the who sending submitting the jobs we basically send it the message to the browser and in this case we're using a push to send a browsers and and we'll discuss you know pull and pull uh push and pull later but basically right now is push to the browser and the browser and then using visualization Frameworks to display them for example in this case we're using plotly in D3 so we're diing each different parts so okay so um first in the spark job in in in our case we basically run the young cluster mode so we um so that's the case for because different clients running this uh running the spark jobs we don't want to crash them one one person to crash it bring the other one down so basically it's on the cluster mode so in the cluster mode the spark job you have a spark contacts and the spark driver is inside the container so the spark clients is really our application code sending the job over and this these uh the the request can come in from command line or RPI or sered applications like a tomcat and Jetty in that case it's a jetty applications and send it over so uh we create appliation context there's additional Contex we creates because we control the how the spark job is invoked so we create application cont context and there we set up the communications oh 10 minutes okay all right I I'll speak faster so um we uh the that we send the uh the communication channels and the loggers and then the machine learning algorithms you know directly called The Machine learning uh listener interface and send a message over so and the message is send back to the uh the uh the the application server through AA messages and eventually we using this chunk response uh send it over all right so so let's get into the uh The Listener part so it's actually quite simple interface for example we Define a interface called machine learning listener so has simple signature called on message and message is passing over and the other interface is listen to support basically when you send a me call send message you pass whatever messages if the listener is enabled we basically just call on message on for each listener for Kings we essentially modify the cing code basically mixing that uh listener support interface we mentioned early and uh and for a concrete message we send in this case we Define uh essential a case class with essential iterations the centroid and the cost in the cost values and there we simply modify this wrong algorithms if you do the king's uh uh code essentially essentially come down to this method called run algorithms this is all the spark code and the only part which is bold is I I modified so essentially if it's not stopping and basically we send in the messages and the messages is really just the case class we mentioned early so for so the remember the uh ml listener is just an interface so for the caming specific and here we essentially um this is a little bit more code i u probably than you expected but essentially the the idea here is at the beginning we sample we uh we take the rdd and then take a sample and up the data and from that sample because and since at this point we already know the Cent we got the message for the centroid and then we're basically using that sample to predict the uh in the model to predict what the cluster is and then send this information back to the browsers so how do we set it up up so there's a you know the only difference is if you call the the caming is instead of call new caming you call new cing extensions you add the listeners and then we also notice here we also add a something called a test Observer I would describe later and that's the as that's the uh that's the uh that's the uh the the interface receiving commands so uh uh the task observers unlike the listeners who basically Sending message out here basically receiving messages and in this case we basically want to receive message like for example you want to stop the visualization or sto the iterations and so you can collect in information so and the test Observer is you know again very simple um uh interface which have only one method they notify and then you can Implement different ones so uh I'm going to skip logistic regressions uh so um so we're going to talk about task channels so okay so so we describe the first part the message has been you know sent it out from machine learning to uh somewhere and and it's who's delivered them so in this case we have this uh task channels so when the spark application starts we we mention we create this application context in this case we're using AA so we create a new AA systems the AA system creating uh two two actors one is for messaging actor the other one is TX Channel actor the one is a re you know Sending message out the other one is a receiving messages and then we also create a spark context through the novel spark jobs okay so here's a is a the picture so the K means for example sending messages through ml listeners and then sending it to over to the logger essentially that's the messaging actor who sending the ARA message back and then uh if you sending message over to the say face stop it'll go to the test channels and then go to the m listener in turn and change the flag and then the machine learning will startop stop so okay so that described the the second part the third part is to say how do we push the stuff to uh browser so again we sending ACA messages here so we're using this uh you know HTM HTML 5 server send a server send event ssse uh you know uh is a oneway messaging so uh you can register this uh events tours uh through the URL on the JavaScript site and then there's a call back and say message you pass whatever functions the data format specific for SS is a you know you have the data column and uh you can just put a a Jon string there directly and on the on the server side uh we since we're using AA okay uh using using AA we're using spray based uh rest API server so the spray spray is AA based server so you can has this a chunk response uh uh chunk response support So in this case each time we got a message essentially we start a chunk response and just keeping sending and then until we got everything finished so uh on the okay I guess we can describe this I can skip this so uh the the last one is uh the visualization so here we just using uh plotly and and and using plotly to to to plot the visualization is actually quite simple so uh you defines your your cost vectors and a data you know data vectors eventually you specify the X and Y and just just and then just plug in into um you know uh JavaScript call and poly is open source everybody can use it so um so quick in summary since I only had like a minute or two left uh a training machine learning visualization involves a lot of experiments so we need a way to visualize the process and uh for spark especially spark young cluster mode essentially a batch job you have no clue what's going on until the job finishes so so uh with our approach this gave you a way to essentially do a callback to your system so allow you to actually see what's going on and this the approach I described is not a really you know in is not complex at all so so pretty much everybody can implement this so um I guess um that thank you and here's the if you want to later on look at the you know the the presentation the demos and uh Linkin page just has all the informations thank you very [Applause] much still have time for questions yeah plenty of time questions okay all right so any questions sure imp machine learning in your company or your custom uh this is actually a new feature we put with customer haven't really used this so um we um for for many our data scien they they basically want to see you know what you know if I running a job is pretty long what's going on so that will be first things I want to see so we Implement uh three algorithms essentially for Kings logistic regression and linear regressions and I didn't I I skipped through the the linear and logistic regression part and uh essentially uh they basically say you know is you know if I gave a set of you know iterations or a set of you know is this already converged if this converg you know why bother waiting for that and I can because I want to try even the different models are different parameters so so this will give you a quick way to to do that all right thank you very [Applause] much a