Scale By The Bay 2018: Rahul Chitturi, Motivating Probabilistic Programming
Recording: Scale By The Bay 2018: Rahul Chitturi, Motivating Probabilistic Programming
okay my name is aho and I work for a hedge fund called co2 management so this is really my side project this is not what I work on on a daily basis so since I work for a hedge fund I can't really talk away about my work so whatever what I do every year is like I think some random project and spend a couple of months and like create like a brand new library or do something cool with it so that's what I did with probability programming as well so my goal is just to teach you like what probabilistic programming is so there have been like three talks where people talked about like what they're doing with their libraries so I want to concentrate a little bit on like what the probabilistic programming is and then I'll talk about the library that I wrote so before we start let's do a small thought experiment hopefully this will give you enough intuition on what probabilistic programming is so let's do that so how many of you have seen this movie called limitless please raise your hands don't be shy okay so for the people who haven't seen this so there is this drug called NZT and if you take this drug two things can happen so you can either become the smartest person in the world and you can do like all crazy things or you can have like really bad side effects and you might even die from it so you want to be really sure before you want to take such a decision right so the pharmaceutical company that made this drug they collected like lot of data points around this so the blue points that you see here are the ones like people who took the drug and survived and hopefully became smarter people and the red ones are the ones where like people who had like really bad side effects or they even probably died from it so what are we going to do with this data now so obviously we are going to train a model and these days everything is deep learning so we are going to tell you in our deep learning model and as google says you don't even need a PhD to do that and let's see what the accuracy would be with such a model yay we got hundred percent accuracy so what this is showing is anything that is about the curve it is purely safe so and whatever is below the curve is is a dangerous zone and you don't want to take the drug if you are in this zone so this is too good to be true right this is like a perfect model so you have like a deep net that essentially classified and it actually bent itself and classified perfectly so now you get excited with this model and say hey pharmaceutical company can you also please try this drug this is like I want to be a really smart person and can you help me with that and then they say hey yeah okay so I'll take your blood work and they put your they take your features from your blood work and they place you there at this black point so my question to you is like if you are this black point like would you take this drug so this is well above the curve right so how many would you how many would take it just to come on guys not a B okay so it seems like a no-brainer to me because like it is well about the car and the model says it is a hundred percent accurate that accurately classified that this is a blue thing and let's see what happened to that guy who took the drug boom he died whoever took the drug they died because like we can't ever be sure like about the data that we haven't seen but yet like the models that we do today they give you like hundred percent confidence about like something said that we haven't seen before so that is the problem we are trying to address here so let's do another another round so you already fixed your model you have 100% accuracy now if you are this black black point would you take it again No you guys are getting spotted now you don't even even need a pill okay so as you expect so that's not that that's not the right model so this turns out to be the right model and we were bad twice and this could be very costly if your life is depending on it so that's exactly what we are trying to solve here with probability programming so so what happened here so if you look at the models that we had before they're like hundred wasn't accurate and they totally separated the blue dots from the red dots right so we have one model here second model in the third model and every time like we said like hundred percent accurate but that order to be not true so essentially what we want to do is like we want to make a confident decision and so if if ever like if you're if you ever have to take a decision where your life depends on it you want to be really sure so that's what probabilistic programming will help you with so how do we solve this problem so one one idea that came to my mind is so like we saw three models and one idea is like instead of one model one deep learning model like can we have like three three models or a bunch of mortals so that's what we see here so now instead of one model we got three models and let's say like we are trying to predict like we want to know the confidence of these three points so for the p1 so the confidence of the probability of p1 being red is 2 out of 3 because there are three models here and two of the models say hey it's a p1 should be red so out of three models two are saying it's red so that's why the probability of p1 being red is two out of three and the same thing with p2 so one of the models the pink one is saying hey it should be in the red zone so that's why one of the so that's what the probability of p2 being red is one out of three and all the three models are agreeing on p3 and it is this highly confident about p3 and then so if you are to take a pill being a p3 or p1 you would have like more confidence with p3 than p1 right because the probability of p3 being red is almost close to 0 and the next idea is like why stop at like three models or few models why not generate all the models but there are as you can imagine there are like infinite curves that can fit this but there is a solution to it so essentially if you model it as like probabilistic with continuous probability distributions you essentially get a surface so in so earlier what we saw was like we saw a few curves but rather than that like in if you model your program probably slickly then you can then you'll get a probability surface and this surface is saying that hey like wherever it is dark it is like highly uncertain and the areas where it is light it is very certain about it so essentially or you have like infinite ways to do it so let's what we are proposing here all right so so this is where we started before before this talk so this is however like all our models are working as of today so we have like one Unser it at any uncertainty curve and we also have like one answer for prediction so it could be safe or deadly and and from here we want to go here so instead of prediction being like one answer you want to get a range of answers so that's what the prediction is saying and for the uncertainty curve instead of getting one curve you want to get a probability space is that clear so far now let's look at models with and without the probability so I got these slides from Eric MA he gave a awesome presentation on like how their models look like with done without the probability so you should check it out and eric moore wherever you are a big shout out for you okay so let's look at our linear regression model so the idea of linear regression is you have a bunch of points and you want to fit a line through those points right so essentially what we are computing is that red point and the sign point the red one is the slope the slope of the line and cyan one is the y-intercept okay so this is like plane modeling 101 and essentially what you get is like given a point you get an answer back which is the Y and all these are points right so however like as we discussed before you don't want one answer we want a distribution of answers right so my question to is like what do you do with this weight so that you get a distribution back at the end can anyone take a guess no guesses how do you know the answer so essentially instead of taking these looking at these as points you have to make them probability distributions and when you multiply a point with the probability distribution we get a probability summation at the end so let's take a look at that so all probability modeling does is instead of having like points as your answers like as your parameters you have probable probability distributions as your parameters so that's essentially the crux of probabilistic programming and let's see like how a deep learning model would look like with and without the probability so so deep learning there is nothing deep about it there's a bunch of function compositions with a bunch of matrix operations it's essentially a deep learning model like that we saw before like this so it has a bunch of parameters and bunch of function compositions and if you were to make this deep learning model a probabilistic deep learning model you just have to make these parameters probability distributions so essentially what we are doing is from this state we are going to this state that's essentially probably c programming is that clear so far yeah right yeah yeah yeah so how do we address that maybe we can talk of that yeah okay so why there is so much hype today so I feel like so we are already doing good with the deep learning models but as you saw before like we are like so confident in areas where we shouldn't be so confident right so like the current research is saying that hey like instead of being like so much sure like add uncertainty to your models so that's what we are trying to fix here with probabilistic programming and as you can see here there are like many popular repos uber has pyro and PIME c3 is very popular in the Python community and tensorflow is also adding a bunch of commits to probability they just started with it so however like there is one common thing that is bad with all these reports not bad but where we can do better can you guys take a guess exactly [Music] so obviously we want to do this in Scala and if we were not doing in this color I'm like I wouldn't be talking here so I created this library called prom Allah so if you are excited to work on probabilistic programming you guys check this out and so let's do some so let's see how we can do this in Scala so let me talk a little bit about my library so here primal is a probabilistic machine learning librarian that is written in Scala and what I say here it's purely functional so what is purely functional so any probabilistic machine in learning library it has it has to support distributions and samplers so this is like the very coating of any probabilistic machine learning library and I made sure that like there is there are no side effects in these two things so they all take a input of type a and give our output of type B and there are no side effects so this is the pure functional piece and I say it is back in diagnostic what is the back end so if you have to do some machine learning modeling you have to choose your back end which is like is that tensorflow or torch or MX net or whatever right so in whatever code that we are writing it should be back in diagnostic because like the models that we are writing they are not tied to like one particular back-end so they essentially so how do we solve this problem right so we simply abstract this away with a type less so this is what we learnt from like scholars E and cats right essentially we create like type classes to solve things that other libraries don't support so what we do here so we create a type plus call back end and you just create type instances for the back ends that you have so let's say so I in my case I created a back end for breeze and one for tensor flow and all you need to do if you have to change your model to switch from one back into another back end it's just one line of change isn't this amazing so PI mc3 is a popular Python library and they were tied to Theano and T I know like they've solved their support and they're spending like almost a year to move away from like Thea now to tensorflow and we don't want to let that happen with our library so you can simply abstract that away a type class so another type class that I provide here is the matrix hop types of matrix operations this is similar to what tonfa is working on yeah so I'm using a library called tensorflow Scala so it is a wrapper on top of that so yesterday we saw this top from tongue kitchen where he talks about types of matrix operations but we have some libraries written in Scala today that does linear algebra like breeze so which has no idea of like what the dimensions are right so if you were to create like a matrix all it knows is is a dense matrix of some data type it could be in boolean or whatever and the same thing same thing with nd for s as well so and if you were to do like some kind of matrix operations like well while working on your deep learning models you have no idea like when it would be type safe or not like you can totally do some mistakes or typos as here so for example like you have a matrix that is of n by 2 which is your X here and you're doing a dot product with W 1 if you made a typo you want to get some nice error that says hey like that is not possible that's what you get you get with this library as well and hopefully like going forward I can probably work with don fehr and get some concepts from him and include this in the library so let's take a look at a few machine learning models so let's go with linear regression so the idea of so this is a model right so let me show you the data that I have or one thing that I also support in my library is the node works so if you go to my library like we have like some nice notebooks for a bunch of examples in there and I think that is very crucial for our scholar community this is like one piece that we are missing in Scala where we don't really care about UI and if you were to give your library to some data scientists and if you want him to do some like machine learning or whatever he's not going to use it because like there is no way he can play with it without even seeing all these models so you need to support some kind of UI otherwise like there is no point writing your library and I think Rainier did a good job as well like got some data points from them so they use see plot I'm I'm actually using something similar it's call evil plot beaker I see I see yeah probably can look at that after this okay so having said that let's look at a linear model so essentially what I did was like I took us a line and added some noise and plotted it here so the actual y-intercept and slope are here so the real slope is 2 and the intercept is -2 okay and a yeah generate a bunch of points and the idea is to try not probably sigmoidal on top of this so let's look at like how the probabilistic model works so essentially probably modeling is nothing but Bayesian inference and the way we do it is like so essentially we want to get the posterior so P of parameters given data and this is nothing but likelihood times prior by P of data and what is P of data this is some nasty thing that we don't want to come to it it's like really hard that we can't even compute right so essentially I'm saying so then we use this technique called MCMC Monte Carlo Markov chain that says that like hey like if you do some random walks in this probabilistic space and collect some data points along the way you don't so this P of X gets cancelled and I can talk about it for like almost our it gets deeper than this but essentially just believe me that this is what it is so this P of X get cancelled and you get a problem you get samples from your probability distributions along the way so let's take a look at what MCMC is so if you go look for MC MC there is like you get like so many definitions and like one of the things that wikipedia says is this big paragraph which I don't clearly understand so my version of MC MC is you start at a point in your probability space right and you take some random box and these random box are biased towards the high probability areas okay and what you do is like while you're doing this random box you collect some data points along the way and that's it that's what I'm saying C is and let's take a look at some animation which will make it clear so let's say you're at this point right and you are jumping like randomly but this jump is liked by us towards the high probability area so this is a high probability area and you're jumping towards that and it also does walk randomly so you also get to these points where it's not like really high probable so the number of points that you will collect from this area will be smaller and if you do this like Million number of times you get a probability distribution of your like slope and y-intercept and this is your like samples that you find here so this is that this is the crux of MCMC so right now my library only supports metropolis Hastings and there are like some huge breakthroughs in the in this space so I think the latest one is a DVI sampler and I'm planning to add that sometime soon okay let's back so I thought like hey like I have like a real basic version of MCMC and if I can do like the most complex one or the best one that's all anything so yeah since I just started working on this like a month ago so I just came up with this metropolises things and this metropolis Hastings algorithm is like three lines of course you can check it out in my library okay so let's get to the model now so all we are saying is so we have this likelihood and we have this prior we are trying to get rid of this P of data and if you look at the model it's a trade all you have to do is like implement these four methods what are these four methods one is proposal so the proposal is nothing but so this is MCMC piece where we are at a point on you are jumping somewhere right so it will give you the proposal of like way it should you jump so that's your proposal and law prior is P of theta and log so the likelihood is computed in the model and you are starting at some point so that is your like prior assumption so in this case we started at like some so let's see where we started at so in this example that I'm giving I started at like slope being hundred instead of two and minus n instead of minus two so it could be totally random so that's your priority emption and you have this model that says hey y equals to MX plus C kind of thing right so that's that's your models a single line of model that's it and so given this mortal you're providing this sampler that I talked about before the metropolis Hastings and hear you say hey like generate like 20,000 samples and just born the 15,000 it's probably more clear if you work on it so essentially you like throw the first few thousand one so that like you don't get like bad data from me and now let's look at like what it computed so in a in a model without the probability distributions you get a single answer which is exactly what we predicted like two and minus two here but here with the probabilistic machine learning model you are getting a distribution which is concentrated towards the like the real values but it will also give you the uncertainty of what what the things could be so let's do the same thing with Bayesian deep learning so before we get to that so let me concentrate a little bit so in the case of linear regression all we computed is two variables right nothing much but you can easily extend this to like a Bayesian deep learning model so here every edge you see here is a weight and as you can see here there are like Rolly like 30 or 40 bits here so essentially I'm doing the same example that we did before in my slides so I want to separate out the green ones from the red dots so which I do here in this case with the probability with a deep net with probabilistic models and as you can see here I have all these matrices like that are completely typesafe so this is the tie plus that I was talking about before and all it takes is like either like some kind of dense matrix it could be either tense tensor flow matrix or it could be breeze matrix it doesn't matter so that is abstracted away and now this is your neural network model essentially we are doing the same thing so you have you have a proposal function and you have the log priors which is P of theta and you are starting with some prior assumptions the same thing as the linear model and all you are doing different is just this line and this is your deep learning model just one line so so let me show you have it looks like if I made a type of here for some reason I can't make it bigger anyways I'll probably short here I think it's much better so anyways so if so this is your input right so this is of dimensions n by 2 and you're doing a dot product with w1 which is of type 2 by 5 and for some reason like you made a typo here right instead of w1 you had w-2 this will throw an error so you will get an error in IntelliJ and say hey this is you're doing something stupid and that's what we want to get from our compiler and the same thing so if you wanted a if you instead of breathe if you wanted to have like a tensor flow all you do is like one line of change and that's it so you get like a tensor flow model instead of a brief small and that's what you get from all these things so we are almost done here so closing points thanks to Erik MA for your slides and if you find this interesting come work for koto so we do have like lot of back-end and front-end positions and I'm happy to chat with you more if you are interested and please start this repo so it's not about you and me I think this will help build the ecosystem I think that is more important yeah yeah thank you so much hopefully we can be on the top [Applause]