scale.bythebay.io: James Ward, Introduction to Machine Learning
Recording: scale.bythebay.io: James Ward, Introduction to Machine Learning
alright we'll go ahead and get started welcome everyone I'm James ward I'm the open source ambassador at Salesforce so thanks for coming this will be an introduction to machine learning and hopefully if you haven't done anything with machine learning or have done maybe a little bit hopefully this will help help you take the next step with it so I'm a developer been a developer for a long time and I got into machine learning pretty recently because sales force started acquiring all these different companies around AI technologies and machine learning and I thought it'd be fun to start to learn some of them so so that's that's how I dove in started playing with prediction IO and working with our Einstein team on some fun stuff so I'm a developer new to machine learning or fairly needed machine learning so hopefully that'll help you connect with with the subject so first let's talk about how machines learn so that's my daughter Josie she's 2 and it's pretty fun to watch how she learns so if you're a parent you've probably experienced children who will be sitting in the highchair and they take their food and they drop it on the ground what do we do we pick it up off the ground put it back on their tray and so then they're like oh this is a fun game let me drop it again and then the cycle repeats right and what they're doing is they're observing the world then they're comparing it to their expectations so of course Josie is she when she drops at food she wants to see what happens what's the reaction and then she analyzes the differences how did this how is this different from what I expected and then refines her model of how the world works and then continues this cycle and that's how she's building her model of of the world and learning so what about machines how do machines learn and it's pretty similar cycle so what we do is we take data and we turn that into a model then we evaluate that model and take feedback signals so things that are giving us feedback about the the correctness of our model and then we refine our model and do it all again and that's the cycle that we're doing with machine learning so we're gonna dive into some details on what that actually looks like but so what does I think machine learning kind of started very simply with with all right we have some data and we're going to to just do like a bunch of if-then statements and nest them together and have a some sort of decision tree to help us make a make a decision but that didn't really work very well you didn't get us very far and so now what a lot of our machine learning systems do is they take data they apply an algorithm to the data create a model and then we can use that model for predictions categorizations other things so they Paul snidely once said that the all machine learning is compression and essentially why we need to create this model is because the data is usually too big to actually make a reasonable decision from just in its raw form and so what we need to do is distill that data down into something smaller or something that that we can easily and quickly ask for predictions and categorizations the the field of machine learning is massive there is tons and tons going on around all sorts of different types of machine learning in their supervised learning and many branches of that and reinforcement learning and unsupervised learning and all these different branches of in the AI universe so so it is massive that was one of the things that as a developer getting in this world I'm like oh my god like this is really overwhelming so I'm gonna try to break it down a little bit so there's first if we start with our use cases we kind of divide this up into layers we've got use cases will drill into those so use cases could be like rate leads in terms of how likely they are to close so at Salesforce this is something that we've built on our iron stein platform is the ability to rate lead it's pretty common thing to apply machine learning to categorize this text in some way is it offensive or not offensive is it positive or negative sentiment those sorts of things that's categorization recommend products right that's a good use case for machine learning detect anomalies so those are just some of the use cases but the way that we can actually accomplish those use cases is by using some core methods so there's really four core methods that we can use in machine learning to help our machines make a decision for us and if what you're trying to do doesn't fit with one of these methods then what you're trying to do probably isn't a good fit for machine learning so I talked to salespeople a lot who are like give me some crazy problem and say oh can I Stein do this and I'm like well you probably don't have enough data you know it's like can I'm Stein predict what the next number and a quantum random generator will be it's like no probably not like it would be awesome if it could but but maybe in the future we don't know but so if what you're trying to do doesn't fit into compression classification regression or reinforcement then maybe machine learning isn't what you should be using there so I'm gonna be talking mostly about a type of machine learning a piece of machine learning called product recommendation also called collaborative filtering so that's one of the things that as I got into this this area I realized that there's all these terms and words that like made no sense to me and it turned out they were usually pretty easily explainable so as I go through I'll try to identify some of the terms and it's how I would say it and then how like the machine learning community says it so product recommendation they call collaborative filtering so what we're doing in collaborative filtering is we're taking some likes so we've got some users they're liking items products whatever and we're going to use some some fancy heavy math to generate a model and then from that model we can make predictions to tell you here's some other things you might be interested in so let me show you a demo of what this could actually look like so and this is actually like real it's using prediction I owe and I'll talk a little bit about the technologies in a minute but this is a demo application called dream house and it's something we've we've built all sorts of stuff around but what we can do in dream house is go see a bunch of properties so we can go see okay here's this beautiful property and we can go in and favorite that property right and then we can go back and go to our favorites and see there's our list of favorites right and so other users have done this in the system and so then because I've favorited things and other people have favorited things now we can use that that product recommender to actually go get some recommendations now it's going out and saying okay based on what you favored it and what others have favorited here's some other properties that you would also be interested in so I'll show you some of the code for this in a little bit but all the code is available on Dream House app dahye oh okay so that's our little demo that we're going to talk about how how that actually works and how I used machine learning to build that so a piece of the puzzle for that particular demo prediction I know is my open-source machine learning server run it on Heroku using Heroku Postgres and Heroku connect and pachi spark and spark MLS the spark ml is really the thing that's actually doing the machine learning and that's the piece that we're gonna dive into in a lot more detail okay so let's talk about how we do product recommendation and my hope for this is that you'll see how one algorithm works and that'll help you understand how other algorithms work as well so we're gonna dive into the product recommendation how that actually works so with product recommendation let's start with how humans would do product recommendation so what I've done is create a matrix of properties and users so product user one likes property one user one likes property two user two likes property two so on and so forth so what we can infer just as humans from this is that user one would probably like property three because there's a correlation between what user 1 and user two like so because they have something in common they both like the same sorts of things that we can infer that because user two also likes property 3 that user one should like it too and then we could go further right down the the matrix the user two should also like property 1 and they should like property 4 due to the correlation with user 3 so on and so forth so that's kind of the direct correlations but then there's another set of coral that are like the second-tier correlations so user one should probably like property 4 because user 1 had something in common with user to add something common with user 3 who liked property 4 right and so just as humans we can we can begin to put together this model and figure out what things we should recommend to these different users so when we're looking at product recommendations we especially is in like those real estate example what we can imagine is that the every property has some traits or some features is what the machine learning folks like to call these some features and so if we knew what the features were for all the properties like property one was near a park and had a it was by a good school property two was for bedroom so on and so forth and then if we also knew what the user features were so what the users wanted when they were looking for something to like then we we could actually have a much better idea of what we should recommend them actually we would know exactly what we should recommend them right so by being able to to have all these features if you had these features in your system then you wouldn't actually need any machine learning you if you have this information you essentially could just do a search of this data but where machine learning comes in is when we don't actually have those features so that's we're going to talk about is how how we get there so we've got likes that's the piece of data that we have users have liked properties and let's say we did have the property features so let's say we did have the property one was near a park right so we could actually take the like data and take the property features and compute what the user features are so we can go from likes and property features to our user features so for example if user one likes property one and property one is near a park then we can determine that user one likes things that are near right so that's that's just just just the way the tent stuff works and then likewise if we have likes and we have user features so we know what the user is like then we can determine the property features so we can go either way with those right but remember we don't have the user features or the product features in this case because we're doing machine learning okay there's another cool thing here is that if we have the product the property features and the user features then we can get to the recommendations so and this is again it's just just math so if we know that property one is near a park and we know that user one likes things that are near a park then we can recommend to them to user one property one so we know that there are these correlations there and can map these mathematically this it ends up just being a simple matrix map okay so we we can make the we can develop these mathematical models that will do this for us so then if we have likes remember that's all we actually have in this system how do we get from likes to our property features and our user features and that's exactly what our machine learning system is going to do for us is give us a way to create those property features and user features so how does it work well in this case I'm using an algorithm called the alternating least squares so alternating least squares is a mathematical algorithm that was developed many many times I don't know who initially came up with it but it's the way that we actually can develop those those two feature matrices that we need so the way that it works is what we're gonna do is just actually generate some random data into a matrix so this is this is we're starting with our user features matrix so we're generating some data into this matrix so we're just randomly saying okay user one likes to be near a park right we don't actually know that but we're going to say that and then what do is take remember we have our likes so we're gonna take our likes that's that green chart in the middle so we're gonna take our likes and remember we can do that computation from our user features and our likes to get to our product features okay so we've just done the math to make that transition from random data with likes to property features now this is at this point just random property features right these these these are not real so what we do is we go through a cycle where we compare we compare this data well actually it's not entirely random because we did base it on user likes part of that equation was based on on actual reality so we generated the property features and then what we do is we go through this again but in the other direction so we actually take the property features that we generated which were kind of based on randomness and kind of based on reality and we take that with our likes again and then get a new matrix for our user features so we've generated random user features we've used that with our likes to get our product features and then we've taken that product features that we that we calculated and we apply our likes again and get a new user features and then we do this again so we repeat and what we eventually do is converge to a model that fits our training data set and our training data set is the the likes that we gave to the system so our model is converging over time over iterations through the cycle to something that fits that data that we gave it initially but now here's the the cool thing is that we've got our we've got our features our user features and our product features and remember we can use both of those to generate recommendations so this is our model this is our model is our user features and our product features so up to this point I've been describing these these features in terms of near a park has a nice view so on and so forth but it turns out here's the magic image learning is we don't have to actually know what those features are we can just say that these features exist and our model will we'll fill them with data to fit the training data that we gave it okay so the features don't have to have names we don't even have to know what they are we don't have they don't have to exist in our training data set and that's what the machine learning part of this is doing is it's creating these features filling them in and so that we could have one feature we could have hundreds of features doesn't matter but this is where we get into model tuning is that this how many features we want can affect how accurate and how quickly we can arrive at a model that accurate accurately fits our training data okay so that was alternating these squares which I tried to distill down into something simple but is certainly a bit complex so any questions about alternating least squares before I go on we're gonna see it in code in a minute and so yeah oh so what you do is you tell it how many iterations you're willing to spend on it and then what you can do once your model is generated you can test your model to see how far off it is from the training data or some other data set that you gave it and so that's definitely a big part of machine learning is generating models and then testing models to see how correct they are against the training data that you have yeah yeah so you definitely have to specify max iterations and you don't want it to run forever yeah yes that's a great question so in this type of machine learning what we're doing is we're taking a data set we are applying this algorithm to get to a model and then we use that model so we like to play that model into production and we use that model but what you're pointing out is that the problem with that is that our data is actually changing over time and so what typically is done to address this is we have some batch process that's regenerating a new model based on the new data that's in the system but it's but it's batch mode and I think that this is certainly one of the challenges that that the Einstein team has been working on at Salesforce is to get away from this this like okay we're gonna nightly generate a model that may be out of date an hour after we deploy it right and so so yeah so there certainly are some challenges but this is the the traditional kind of easy model for for how we do it yeah good question Thanks ok any other questions before we go on see some code and stuff ok so just to recap we took our likes that was our training data set and we applied the alternating least squares algorithm which gave us back our our property features and our user features matrices and then from those those matrices we can calculate recommendations so there's another part of this which is ok so I've got these two matrices the property and user features now how do i how do I actually get a recommendation out of that so the way we do that is we do dot product math on these and so we take let's say property 1 and user 1 and we go through every feature and we just essentially add up the the values in that chart and that gives us essentially the probability that this user matches with that given property and so if we actually do that process for every single every single slot in our major then we can calculate what's called the matrix factorization model for this and this can actually be a pretty heavy computation because we're going through every row and every column for like the whole data set and so if it's a large data set oftentimes it doesn't actually make sense to to generate this model previous versions of SPARC ml would just give you this model out of the box and I think they stopped doing that because it was it got pretty heavy with large data sets so now the only thing that they give you by default is the the to the user features and the product features and if you do want the the matrix factorization model then you have to go build that yourself essentially okay so one of the nice things remember I said earlier that Paul Snively said that all machine learning is compression so here's where we can see some of that coming into play is that if we took our user features and our property features when we calculated that that matrix factorization model then just if we had 10 by 10 that would be a hundred right hundred different things we have to store but if we only store our property features and our user features then we can actually save a lot of space in terms of the the matrix that we're actually storing so I don't know I think I reduced that to three 3mc anyways it's a whole lot better a whole lot more efficient to not calculate that matrix factorization model and just save your your future features and users so usually you would save these into a big data store Hadoop whatever and both of these charts and then be able to use them whenever you want okay so let's dive into some code to see how that actually works in SPARC so I have a demo project here that is the one that I use for the Dreamhouse application and I'm going to walk through a bunch of the code for how this actually works and then how I actually test it so let's see so first thing I have is a train method so my train method is going to take a sequence that is a tuple here of user ID and then then the the property ID so these are our likes this is this user like this thing and I'm just using an essentially a binary rating here that is alike but you could certainly instead use have a third parameter which would be like stars so if you have you know one to five stars on something then you could have a third parameter that would be that rating but in my case my rating is either offer on so I just have the the user ID and the property ID then I have some parameters to my to the ALS alternating least squares method so there's reg pram which is how much weight do we want to place on extreme values then there's the rank and rank is the number of latent features so that's how many features do we want in those feature matrices so I default that to three so that means that that for each user and we want to know the features that they like then we're gonna have three we're gonna say that there's three different things that distinguish what they like so as that number goes up your model can be more accurate usually then here's our max iteration so I default that to ten so that means by default I'm going to run through that alternate least squares to ten times okay and then what this outputs is an ALS model so that is really just a holder for those two matrices the the product features and the user features so then what I'm gonna do is because this is SPARC I'm going to convert my likes to a data frame and my data frame has two items two columns initially a user and an item item is the default name that spark wants we could rename that if we want but those are my two initial columns but then I need a rating column because remember I didn't specify rating it's either on or off and because it's a like we know it's on so what I'm gonna do is just add a column that is rating and its value is going to be 1 so I'm just leaving out all the data that's these you know anyone that didn't like anything we don't care about that okay so then what I'm gonna do is initialize the alternating least squares stuff from sparks specify those parameters and then here's the magic lfit ratings so I'm giving it these ratings to the LS fit method and then that's what actually is going to go and do all that computation the going around and around for ten iterations by default and calculate a model which is the the product features and the user features that fit the training data which is the ratings so it's going to generate that okay so there's a whole lot of math that happens when I run that ALS dot fit in the case of my small data set not so much but but a whole lot of math there's there's some pretty serious equations that go into how that actually works and how it converges and all that so that's my train method let's go flip over to my tests here and I've written some tasks I've learned that machine learning world they don't often like right like automated unit tests and stuff for her for things but I wanted to do that so that I could actually test that this thing worked as expected so I want to show you the the test of it that are here okay so for my test what I've done is I've put together a simple matrix of users and properties and what in my in my training data set I've said that user one like property one user to Lex property two so on and so forth so then I've just charted out here in ASCII what I think should be the result from the from the predictions in terms of what they should like so so user two should should like property one and so on and so forth right you can see all that there okay so then I need to actually it can't take ASCII and give it to this thing so there's my actual matrix of all right user one likes property 1 and user 2 likes property 1 sorry property 2 is liked by user 1 or something okay so there's my matrix so then what I'm gonna do is actually test this thing so I'm going to run the train method on from my dream house recommendations and then I'm gonna test a few things just to make sure that the training actually worked as expected so the first thing is that the user factor Rose so this is not the columns this is the rows in the user factors should be five and that's because I have five unique users there right and same with item factors should have five rows because I have five properties so you'll see that the API calls this item factors to make it to make it work with with whatever the whatever you want to recommend but but this is my property features and you'll also see that they call it factors it's another word for features in this space okay so then my my next test is to go to my user feature user features get the first row and what I get out of that is a sequence of long which is my features and here's where I'm actually getting out the the features from this thing from my training from what my model is my user model and I'm verifying that the number of features equals 3 which should correlate with this 3 here which is the default number of features that I've specified right so that's what I want to test first and let's go actually just run this thing so that we can verify that this all works so it's going to take just a little bit to run this because it has to spin up spark and then do all the the matrix math on that so but then what we should see is that it's gonna pass but well that's well that's doing that there we go it passed I want to show you how we can actually look into this stuff to see what it actually looks like so there's some stuff in spark where we can say modeled user factors show and I'm gonna tell it not to like truncate the the output and let's also do the item factors and then let's just rerun that one and now we'll actually be able to see what the what those models look like alright so take a second to you start up spark and then run that but see our model here so this what we're gonna see is our actual model this is the thing that spark has generated using alternating least-squares and then we'll be able to use for predictions okay so there we go there's our features and what you'll see is that so this is the the first one is the user features what we'll see is that user one has these three features so remember we just made up these features you know this could be the view this could be the number of bedrooms whatever we don't know at this point we don't know so we don't have that data but what we've done is we've generated values and these values go from 0 to 1 so so essentially it's the same user one really likes whatever this feature 2 is so again we don't know what that feature actually is it doesn't matter but user 2 also likes that feature so so does use your 3 use a fork kind of likes it right and user 5 not at all right so we can see these values they really mean nothing to us this is just just this generated model and then the same thing for the user features I guess those don't go 0 to 1 they go a little above one sometimes maybe 2 - I don't know ok so so there we go we see our user features - okay so that's our training and I'll remove that so that all worked as expected but then we have our model and now we want to actually make predictions against it so let's go down to our next test where we're actually going to use the predict method and I'll go into the predict method so you can see how how this one works so for this one what we're gonna do is we're gonna pass in our model and then we're gonna pass in the user that we want to query so this particular predict method only takes a single user but you could certainly you know cut this data up however you want to you could do multiple users you could say give me the top top users give me the top products all that kind of stuff can be done but we're just gonna say for a given user give me the items that they will like and then number results that we want to get and what this returns is a map from into float so that is the property ID and then the likelihood the value of the likelihood that the prediction predictive value so how likely they are to actually like that property alright okay so now what we're gonna do is go to our model and there's this convenience method here recommend for all users which is going to do that matrix factorization model for us on on those two the item and the property features models and it's going to get us number of results that we're looking for so it's getting us the top by default the top products and then we're telling it to filter out because we really only want to see the user that we're making predictions for and so we're telling SPARC to filter on the column user and and then match on on the user that recording so what we get back from that is this data set of row and what we want to do is then convert convert this thing to to what we actually to date this data in the form though that we want which is this map from in to float so I have to do some some math here so we can actually get the way that the data looks when it comes back is that it's a single row which has the user ID and then it has an array of arrays which each array is essentially a tuple of the property in the prediction so essentially what I'm doing is just parsing that data out to get me my my map into float okay so that's our predictive method so let's go take a look at a test for this one so here's just a basic prediction we're calling the predict method we're saying here's your model that we generated and then your query user is three so user three up here we can see user three should be interested in should be kind of interested in in this way actually user three should be kind of interested in property two and they actually liked property three and property forest so they should definitely be interested in those right but we should get a good hi number for property - okay so my test down here what it's doing and the number of results is ten okay so I'm checking to see that my size of my results is five and the reason why I should be five because they only have five properties so even though I said number results ten it shouldn't be able to give me more than five property recommendations because I only have five and then we're gonna go through and test this to make sure that it fulfills our expectation which is basically just checking to see does does it look like what we see on this user three column right and let's go actually view what our results look like and I'm gonna print those out result okay and print Lynn I guess print works also but blue print Lynn and then let's run this one so that's gonna go off retrain the data and then and then call the predict method on it so that's gonna take fifteen seconds or so to run for some reason I was running these an SPT before there's any like SPT experts or SPARC EPS experts I need some help for some reason an SPT when I ran it would take like 20 times longer to run an SPT for some reason so thanks to Russ somewhere over here who helped me he's like why he's running IntelliJ and I did and I ran a lot faster so there we go okay so there's our prediction which is just showing us all right prediction for property five is an prediction for property one is pretty is pretty good prediction for property two is really good prediction for property three is basically like yep absolutely and same with four and if we look back at our model for user three we should see that that basically fits the the data that we're seeing up there so you see that the that it's it has created this model that is then converged to something that fits that the training dataset okay then there's a more test down here I'll just walk through these real quick so there's some what I want to do is test to make sure that the parameters that I can specify when I create this model that they actually are having the effect that I want and so the first one is red prams and reg prams I don't really know what that does in the algorithm but I do know that it the model should be more accurate for lower reg pram numbers and so I'm just testing creating two different models and then there's just really cool stuff in spark ml where I can check to see alright how close did this this model that was generated fit two to the the matrix that I expect and the matrix that I expect I actually fill out with all the likes and all the the not likes essentially and so I'm checking to see does the the model that's generated to fit that and the way that we do that is with this regression evaluator where we're doing a root mean squared error analysis on that and this gets into some math that I definitely don't understand but essentially what we're doing is seen how far does the the generated model deviate from what we expected and what we can then do is compare so we do the root mean squared error for both of these and we check to see in this case the lower reg parameter should be less than the higher Reggiano right should be lower air than the then the one with the higher number and I'll just run all this you can see that sure enough all all of my tests pass like a good developer but it works and then I'll go on to the next one so so the next one is higher ranks so the ranks are the number of features that we want in our model and so I first do one and then I do five and then I do my root mean squared error and say that yep when I did I called it ten but it should actually be five down here that my five should have less air than my one with one because I should get more accurate with more features okay and then then finally the last one is that I should be more accurate with more iterations and that in fact is true as well so in machine learning one of the challenges is that we you know if we had infinite resources compute and storage we could create a model that was just absolutely perfect for our data but oftentimes we don't have and we don't have infinite compute resources and so what we need to do is figure out how to balance the amount of time that we spend generating models so that's doing what's called model tuning and figuring out okay how can we kind of minimize the the resources that we're putting into this but maximize the the correctness of the model that's being generated so that's that's where that machine learning gets into a lot more data science of figuring out how to tweak all these different knobs to optimize things best but um so that's that's what it's called model tuning and a part of that is model selection being able to select the best model for a given case so there's all sorts of caveats to to this whole world like sometimes you can get a model that essentially is over fit so that's that's when you know if if I basically was recommending all products to everyone then because my model actually was just like hey you know everyone likes everything then that would be overfitting and so that's a challenge that you have to look out for as well okay so there we go all my tests pass and let me show you where you can go get all that code so so if you go to dream house app dot IO well and I don't know I'm searching for that but there we go and then go to the patchy prediction I know one down here you can see a fun demo and then get the the code down here at this repository so that's the the code that I actually used for the dream house out there to do the the recommendation for dream house properties okay so that's all the code for that one okay so that's all I got questions yeah how does the LS algorithm actually do the math oh that's way beyond me I'm sure that there's some some of our I'm Stein team in here that can answer that for you but but I actually don't know the math what I've wanted to understand the math but it's some pretty a pretty heavy statistical analysis stuff usually and I'm I don't know that part of math very well and so what someone actually recommended to me to if I wanted to understand the algorithm so as I said go look at the Python implementation of these algorithms and the Python implementations are usually pretty easy to read and understand how it's actually working and it's often a lot easier to understand the algorithm from the Python implementation than actually the the mathematical models that are underneath all that so so that was the recommendation I received on if you really do want to dive in deeper to the mathematical models under that yeah okay other questions all right all right well thanks everyone I hope that's useful and I'll be around later if you have other questions Thanks