9.9 Content-based Filtering | Deep learning for content-based filtering-- [ML |Andrew Ng]
 A good way to develop a content-based filtering algorithm is to use deep learning. The approach you see in this video is the way that many important commercial, state-of-the-art content-based filtering algorithms are built today. Let's take a look. Recall that in our approach, given a feature vector describing a user, such as the age and gender and country and so on, we have to compute a vector v u. And similarly, given a vector describing a movie, such as its year release, the stars in the movie and so on, we have to compute a vector v m. In order to do the former, we're going to use a neural network. And the first neural network will be what we'll call the user network. Here's an example of a user network. It takes as input the list of features of the user, x u, so the age, the gender, the country of the user, and so on. And then using a few layers, say dense neural network layers, it will output this vector v u that describes the user. Notice that in this neural network, the output layer has 32 units, and so v u is actually a list of 32 numbers. Unlike most of the neural networks that we're using earlier, the final layer is not a layer with one unit. There's a layer with 32 units. Similarly, to compute v m for a movie, we can have a movie network as follows that takes as input features of the movie and through a few layers of a neural network as an outputting v m, that vector that describes the movie. Finally, we'll predict the rating of this user on that movie as v u dot product with v m. Notice that the user network and movie network can hypothetically have different numbers of hidden layers and different numbers of units per hidden layer. Only the output layer needs to have the same size or the same dimension. In the description you've seen so far, we were predicting the 1 to 5 or 0.25 star movie rating. If you had binary labels, if y was to the user like or favorite in item, then you can also modify this algorithm to output instead of v u dot v m, you can apply the sigmoid function to that and use this to predict the probability that y i j is 1. To flesh out this notation, we can also add superscripts i and j here. If you want to emphasize that this is the prediction by user j on movie i. I've drawn here the user network and the movie network as two separate neural networks, but it turns out that we can actually draw them together in a single diagram as if it was a single neural network. This is what it looks like. On the upper portion of this diagram, we have the user network which inputs x u and ends up computing v u. On the lower portion of this diagram, we have what was the movie network that inputs x m and ends up computing v m. And these two vectors are then dot producted together. This dot here represents dot product and this gives us our prediction. Now this model has a lot of parameters. Each of these layers of a neural network has a usual set of parameters of the neural network. So how do you train all the parameters of both the user network and the movie network? What we're going to do is construct a cost function j which is going to be very similar to the cost function that you saw in collaborative filtering, which is assuming that you do have some data of some users having rated some movies. We're going to sum over all pairs i and j of where you have labels where i j equals one of the difference between the prediction so that that would be v u j dot product with v m i minus y i j squared. And the way we would train this model is depending on the parameters of the neural network, you end up with different vectors here for the users and for the movies. And so what we like to do is train the parameters of the neural network so that you end up with vectors for the users and for the movies that results in small squared error in the predictions you get out here. And so to be clear, there's no separate training procedure for the user and movie networks. This expression down here, this is the cost function used to train all the parameters of the user and the movie networks. We're going to judge the two networks according to how well v u and v m predict y i j. And with this cost function, we're going to use gradient descent or some other optimization algorithm to tune the parameters of the neural network to cause the cost function j to be as small as possible. Oh, and if you want to regularize this model, we can also add the usual neural network regularization term to encourage the neural networks to keep the values of their parameters small. It turns out after you've trained this model, you can also use this to find similar items. This is akin to what we have seen with collaborative filtering features, helping you find similar items as well. Let's take a look. So v u j is a vector of length 32 that describes a user j that had features x u j. And similarly, v i m is a vector of length 32 that describes a movie with these features over here. So given a specific movie, what if you want to find other movies similar to it? Well, this vector v i m describes the movie i. So if you want to find other movies similar to it, you can then look for other movies k so that the distance between the vector describing movie k and the vector describing movie i that that distance or square distance is small. And this expression plays a role similar to what we had previously with collaborative filtering, where we talked about finding a movie with features x k that was similar to the features x i. And thus with this approach, you can also find items similar to a given item. One final note, this can be pre computed ahead of time. And by that, I mean, you can run a compute server overnight to go through the list of all your movies and for every movie find a similar movies to it. So that tomorrow, if a user comes to the website and they're browsing a specific movie, you can already have pre computed the 10 or 20 most similar movies to show to the user at that time. The fact that you can pre compute ahead of time what's similar to a given movie will turn out to be important later when we talk about scaling up this approach to a very large catalog of movies. So that's how you can use deep learning to build a content based filtering algorithm. You might remember when we were talking about decision trees and the pros and cons of decision trees versus neural networks. I've mentioned that one of the benefits of neural networks is that it's easier to take multiple neural networks and put them together to make them work in concert to build a larger system. And what you just saw was actually an example of that, where we could take a user network and the movie network and put them together and then take the inner product of the output and this ability to put two neural networks together. This how we've managed to come up with a more complex architecture that turns out to be quite powerful. One note, if you're implementing these algorithms in practice, I find that developers often end up spending a lot of time carefully designing the features needed to feed into these content based filtering algorithms. So if you end up building one of these systems commercially, it may be worth spending some time engineering good features for this application as well. And in terms of these applications, one limitation of the algorithm as we've described it is it can be computational, very expensive to run if you have a large catalog of a lot of different movies you may want to recommend. So in the next video, let's take a look at some of the practical issues and how you can modify this algorithm to make a skill to working on even very large item catalogs. Let's go see that in the next video.
[{"start": 0.0, "end": 6.8, "text": " A good way to develop a content-based filtering algorithm is to use deep learning."}, {"start": 6.8, "end": 13.56, "text": " The approach you see in this video is the way that many important commercial, state-of-the-art"}, {"start": 13.56, "end": 15.56, "text": " content-based filtering algorithms are built today."}, {"start": 15.56, "end": 17.2, "text": " Let's take a look."}, {"start": 17.2, "end": 25.240000000000002, "text": " Recall that in our approach, given a feature vector describing a user, such as the age"}, {"start": 25.24, "end": 31.68, "text": " and gender and country and so on, we have to compute a vector v u."}, {"start": 31.68, "end": 37.96, "text": " And similarly, given a vector describing a movie, such as its year release, the stars"}, {"start": 37.96, "end": 42.86, "text": " in the movie and so on, we have to compute a vector v m."}, {"start": 42.86, "end": 48.239999999999995, "text": " In order to do the former, we're going to use a neural network."}, {"start": 48.239999999999995, "end": 53.4, "text": " And the first neural network will be what we'll call the user network."}, {"start": 53.4, "end": 57.12, "text": " Here's an example of a user network."}, {"start": 57.12, "end": 63.519999999999996, "text": " It takes as input the list of features of the user, x u, so the age, the gender, the"}, {"start": 63.519999999999996, "end": 66.28, "text": " country of the user, and so on."}, {"start": 66.28, "end": 74.28, "text": " And then using a few layers, say dense neural network layers, it will output this vector"}, {"start": 74.28, "end": 78.28, "text": " v u that describes the user."}, {"start": 78.28, "end": 87.08, "text": " Notice that in this neural network, the output layer has 32 units, and so v u is actually"}, {"start": 87.08, "end": 89.56, "text": " a list of 32 numbers."}, {"start": 89.56, "end": 95.08, "text": " Unlike most of the neural networks that we're using earlier, the final layer is not a layer"}, {"start": 95.08, "end": 96.08, "text": " with one unit."}, {"start": 96.08, "end": 99.24000000000001, "text": " There's a layer with 32 units."}, {"start": 99.24, "end": 108.36, "text": " Similarly, to compute v m for a movie, we can have a movie network as follows that takes"}, {"start": 108.36, "end": 115.56, "text": " as input features of the movie and through a few layers of a neural network as an outputting"}, {"start": 115.56, "end": 119.72, "text": " v m, that vector that describes the movie."}, {"start": 119.72, "end": 129.76, "text": " Finally, we'll predict the rating of this user on that movie as v u dot product with"}, {"start": 129.76, "end": 131.24, "text": " v m."}, {"start": 131.24, "end": 136.8, "text": " Notice that the user network and movie network can hypothetically have different numbers"}, {"start": 136.8, "end": 140.32, "text": " of hidden layers and different numbers of units per hidden layer."}, {"start": 140.32, "end": 145.56, "text": " Only the output layer needs to have the same size or the same dimension."}, {"start": 145.56, "end": 152.76, "text": " In the description you've seen so far, we were predicting the 1 to 5 or 0.25 star movie"}, {"start": 152.76, "end": 153.76, "text": " rating."}, {"start": 153.76, "end": 160.46, "text": " If you had binary labels, if y was to the user like or favorite in item, then you can"}, {"start": 160.46, "end": 168.64000000000001, "text": " also modify this algorithm to output instead of v u dot v m, you can apply the sigmoid"}, {"start": 168.64, "end": 177.07999999999998, "text": " function to that and use this to predict the probability that y i j is 1."}, {"start": 177.07999999999998, "end": 182.11999999999998, "text": " To flesh out this notation, we can also add superscripts i and j here."}, {"start": 182.11999999999998, "end": 188.6, "text": " If you want to emphasize that this is the prediction by user j on movie i."}, {"start": 188.6, "end": 193.64, "text": " I've drawn here the user network and the movie network as two separate neural networks, but"}, {"start": 193.64, "end": 198.64, "text": " it turns out that we can actually draw them together in a single diagram as if it was"}, {"start": 198.64, "end": 201.23999999999998, "text": " a single neural network."}, {"start": 201.23999999999998, "end": 203.23999999999998, "text": " This is what it looks like."}, {"start": 203.23999999999998, "end": 210.23999999999998, "text": " On the upper portion of this diagram, we have the user network which inputs x u and ends"}, {"start": 210.23999999999998, "end": 212.23999999999998, "text": " up computing v u."}, {"start": 212.23999999999998, "end": 217.92, "text": " On the lower portion of this diagram, we have what was the movie network that inputs x m"}, {"start": 217.92, "end": 221.16, "text": " and ends up computing v m."}, {"start": 221.16, "end": 225.56, "text": " And these two vectors are then dot producted together."}, {"start": 225.56, "end": 232.28, "text": " This dot here represents dot product and this gives us our prediction."}, {"start": 232.28, "end": 236.44, "text": " Now this model has a lot of parameters."}, {"start": 236.44, "end": 241.51999999999998, "text": " Each of these layers of a neural network has a usual set of parameters of the neural network."}, {"start": 241.51999999999998, "end": 249.28, "text": " So how do you train all the parameters of both the user network and the movie network?"}, {"start": 249.28, "end": 255.24, "text": " What we're going to do is construct a cost function j which is going to be very similar"}, {"start": 255.24, "end": 260.98, "text": " to the cost function that you saw in collaborative filtering, which is assuming that you do have"}, {"start": 260.98, "end": 265.36, "text": " some data of some users having rated some movies."}, {"start": 265.36, "end": 272.0, "text": " We're going to sum over all pairs i and j of where you have labels where i j equals"}, {"start": 272.0, "end": 280.08, "text": " one of the difference between the prediction so that that would be v u j dot product with"}, {"start": 280.08, "end": 286.88, "text": " v m i minus y i j squared."}, {"start": 286.88, "end": 293.32, "text": " And the way we would train this model is depending on the parameters of the neural network, you"}, {"start": 293.32, "end": 299.28, "text": " end up with different vectors here for the users and for the movies."}, {"start": 299.28, "end": 304.79999999999995, "text": " And so what we like to do is train the parameters of the neural network so that you end up with"}, {"start": 304.79999999999995, "end": 311.79999999999995, "text": " vectors for the users and for the movies that results in small squared error in the predictions"}, {"start": 311.79999999999995, "end": 313.59999999999997, "text": " you get out here."}, {"start": 313.59999999999997, "end": 320.71999999999997, "text": " And so to be clear, there's no separate training procedure for the user and movie networks."}, {"start": 320.71999999999997, "end": 326.23999999999995, "text": " This expression down here, this is the cost function used to train all the parameters"}, {"start": 326.24, "end": 329.6, "text": " of the user and the movie networks."}, {"start": 329.6, "end": 336.84000000000003, "text": " We're going to judge the two networks according to how well v u and v m predict y i j."}, {"start": 336.84000000000003, "end": 341.64, "text": " And with this cost function, we're going to use gradient descent or some other optimization"}, {"start": 341.64, "end": 346.76, "text": " algorithm to tune the parameters of the neural network to cause the cost function j to be"}, {"start": 346.76, "end": 348.40000000000003, "text": " as small as possible."}, {"start": 348.40000000000003, "end": 355.56, "text": " Oh, and if you want to regularize this model, we can also add the usual neural network regularization"}, {"start": 355.56, "end": 362.04, "text": " term to encourage the neural networks to keep the values of their parameters small."}, {"start": 362.04, "end": 368.2, "text": " It turns out after you've trained this model, you can also use this to find similar items."}, {"start": 368.2, "end": 373.56, "text": " This is akin to what we have seen with collaborative filtering features, helping you find similar"}, {"start": 373.56, "end": 374.56, "text": " items as well."}, {"start": 374.56, "end": 376.56, "text": " Let's take a look."}, {"start": 376.56, "end": 385.64, "text": " So v u j is a vector of length 32 that describes a user j that had features x u j."}, {"start": 385.64, "end": 393.68, "text": " And similarly, v i m is a vector of length 32 that describes a movie with these features"}, {"start": 393.68, "end": 395.2, "text": " over here."}, {"start": 395.2, "end": 401.84000000000003, "text": " So given a specific movie, what if you want to find other movies similar to it?"}, {"start": 401.84, "end": 408.41999999999996, "text": " Well, this vector v i m describes the movie i."}, {"start": 408.41999999999996, "end": 414.12, "text": " So if you want to find other movies similar to it, you can then look for other movies"}, {"start": 414.12, "end": 421.7, "text": " k so that the distance between the vector describing movie k and the vector describing"}, {"start": 421.7, "end": 426.35999999999996, "text": " movie i that that distance or square distance is small."}, {"start": 426.36, "end": 431.92, "text": " And this expression plays a role similar to what we had previously with collaborative"}, {"start": 431.92, "end": 439.08000000000004, "text": " filtering, where we talked about finding a movie with features x k that was similar to"}, {"start": 439.08000000000004, "end": 441.52000000000004, "text": " the features x i."}, {"start": 441.52000000000004, "end": 447.72, "text": " And thus with this approach, you can also find items similar to a given item."}, {"start": 447.72, "end": 451.84000000000003, "text": " One final note, this can be pre computed ahead of time."}, {"start": 451.84, "end": 458.02, "text": " And by that, I mean, you can run a compute server overnight to go through the list of"}, {"start": 458.02, "end": 462.61999999999995, "text": " all your movies and for every movie find a similar movies to it."}, {"start": 462.61999999999995, "end": 468.64, "text": " So that tomorrow, if a user comes to the website and they're browsing a specific movie, you"}, {"start": 468.64, "end": 474.0, "text": " can already have pre computed the 10 or 20 most similar movies to show to the user at"}, {"start": 474.0, "end": 475.0, "text": " that time."}, {"start": 475.0, "end": 480.09999999999997, "text": " The fact that you can pre compute ahead of time what's similar to a given movie will"}, {"start": 480.1, "end": 485.92, "text": " turn out to be important later when we talk about scaling up this approach to a very large"}, {"start": 485.92, "end": 487.92, "text": " catalog of movies."}, {"start": 487.92, "end": 494.64000000000004, "text": " So that's how you can use deep learning to build a content based filtering algorithm."}, {"start": 494.64000000000004, "end": 499.28000000000003, "text": " You might remember when we were talking about decision trees and the pros and cons of decision"}, {"start": 499.28000000000003, "end": 501.24, "text": " trees versus neural networks."}, {"start": 501.24, "end": 506.28000000000003, "text": " I've mentioned that one of the benefits of neural networks is that it's easier to take"}, {"start": 506.28, "end": 511.11999999999995, "text": " multiple neural networks and put them together to make them work in concert to build a larger"}, {"start": 511.11999999999995, "end": 512.12, "text": " system."}, {"start": 512.12, "end": 517.8, "text": " And what you just saw was actually an example of that, where we could take a user network"}, {"start": 517.8, "end": 522.74, "text": " and the movie network and put them together and then take the inner product of the output"}, {"start": 522.74, "end": 527.16, "text": " and this ability to put two neural networks together."}, {"start": 527.16, "end": 531.92, "text": " This how we've managed to come up with a more complex architecture that turns out to be"}, {"start": 531.92, "end": 534.16, "text": " quite powerful."}, {"start": 534.16, "end": 538.9599999999999, "text": " One note, if you're implementing these algorithms in practice, I find that developers often"}, {"start": 538.9599999999999, "end": 544.9599999999999, "text": " end up spending a lot of time carefully designing the features needed to feed into these content"}, {"start": 544.9599999999999, "end": 545.9599999999999, "text": " based filtering algorithms."}, {"start": 545.9599999999999, "end": 550.42, "text": " So if you end up building one of these systems commercially, it may be worth spending some"}, {"start": 550.42, "end": 555.76, "text": " time engineering good features for this application as well."}, {"start": 555.76, "end": 562.1999999999999, "text": " And in terms of these applications, one limitation of the algorithm as we've described it is"}, {"start": 562.2, "end": 567.32, "text": " it can be computational, very expensive to run if you have a large catalog of a lot of"}, {"start": 567.32, "end": 570.1400000000001, "text": " different movies you may want to recommend."}, {"start": 570.1400000000001, "end": 573.96, "text": " So in the next video, let's take a look at some of the practical issues and how you can"}, {"start": 573.96, "end": 580.5600000000001, "text": " modify this algorithm to make a skill to working on even very large item catalogs."}, {"start": 580.56, "end": 593.3599999999999, "text": " Let's go see that in the next video."}]