9.6 Recommender Systems implementation detail|TensorFlow implementation of collaborative filtering
 In this video, we'll take a look at how you can use TensorFlow to implement the collaborative filtering algorithm. You might be used to thinking of TensorFlow as a tool for building neural networks, and it is. It's a great tool for building neural networks. And it turns out that TensorFlow can also be very helpful for building other types of learning algorithms as well, like the collaborative filtering algorithm. One of the reasons I like using TensorFlow for tasks like these is that for many applications, in order to implement gradient descent, say, you need to find the derivatives of the cost function. But TensorFlow can automatically figure out for you what are the derivatives of a cost function. All you have to do is implement the cost function. And without needing to know any calculus, without needing to take derivatives yourself, you can get TensorFlow with just a few lines of code to compute that derivative term that can then be used to optimize the cost function. Let's take a look at how all this works. You might remember this diagram here on the right from course one. This is exactly the diagram that we had looked at when we talked about optimizing W when we were working through our first linear regression example. And at that time, we had set B equals to zero. And so the model was just predicting f of x equals W dot x. And we wanted to find the value of W that minimizes the cost function J. So the way we were doing that was via a gradient descent update, which looks like this, where W gets repeatedly updated as W minus the learning rate alpha times the derivative term. If you were updating B as well, this is the expression you would use. But if you set B equals zero, you just forego the second update. And you keep on performing this gradient descent update until convergence. Sometimes computing this derivative or partial derivative term can be difficult. And it turns out that TensorFlow can help with that. Let's see how. I'm going to use a very simple cost function J equals W x minus one squared. So W x is our simplified f W of x. And y is equal to one. And so this would be the cost function. If we had f of x equals W x, y equals one for the one training example that we have. And if we were not optimizing this with respect to B. So the gradient descent algorithm would repeat until convergence this update over here. It turns out that if you implement the cost function J over here, TensorFlow can automatically compute for you this derivative term and thereby get gradient descent to work. I'll give you a high level overview of what this code does. If you equals tf.variable three takes the parameter W and initializes it to the value of three telling TensorFlow that W is a variable is how we tell it that W is a parameter that we want to optimize. I'm going to set x equals one y equals one and the learning rate alpha to be equal to zero point zero one. And let's run gradient descent for 30 iterations. So in this code will do for either arrange iterations. So for 30 iterations. And this is a syntax to get TensorFlow to automatically compute derivatives for you. TensorFlow has a feature called a gradient tape. And if you write this with TF by gradient tape as tape F, this is compute f of x as W times x and compute J as f of x minus y squared. Then by telling TensorFlow how to compute the cost J and by doing it with a gradient tape syntax as follows, TensorFlow will automatically record the sequence of steps, the sequence of operations needed to compute the cost J. And this is needed to enable automatic differentiation. Next, TensorFlow will have saved the sequence of operations in tape in the gradient tape. And with this syntax, TensorFlow will automatically compute this derivative term, which I'm going to call DJ DW. And TensorFlow knows you want to take the derivative with respect to W that W is a parameter you want to optimize because you had told it so up here and because we're also specifying it down here. So now that you've computed derivatives, finally, you can carry out this update by taking W and subtracting from it the learning rate alpha times that derivative term that we just got from up above. TensorFlow variables, tier variables, require special handling, which is why instead of setting W to be W minus alpha times the derivative in the usual way, we use this assign add function. But when you get to the practice lab, don't worry about it, we'll give you all the syntax you need in order to implement a collaborative filtering algorithm correctly. So notice that with the gradient tape feature of TensorFlow, the main work you need to do is to tell it how to compute the cost function J. And the rest of the syntax causes TensorFlow to automatically figure out for you what is that derivative. And with this, TensorFlow will start with finding the slope of this at three shown by this dashed line, take a gradient step and update W and compute the derivative again and update W over and over until eventually it gets to the optimal value of W, which is at W equals one. So this procedure allows you to implement gradient descent without ever having to figure out yourself how to compute this derivative term. This is a very powerful feature of TensorFlow called auto diff. And some other machine learning packages like PyTorch also support auto diff. Sometimes you hear people call this auto grad. The technically correct term is auto diff and auto grad is actually the name of a specific software package for doing automatic differentiation, for taking derivatives automatically. And sometimes if you hear someone refer to auto grad, they're just referring to this same concept of automatically taking derivatives. So let's take this and look at how you can implement the collaborative filtering algorithm using auto diff. And in fact, once you can compute derivatives automatically, you're not limited to just gradient descent. You can also use a more powerful optimization algorithm like the Adam optimization algorithm. In order to implement the collaborative filtering algorithm in TensorFlow, this is the syntax you can use. Let's start with specifying that the optimizer is Keras optimizers, Adam with learning rate specified here. And then for say 200 iterations, here's the syntax as before with TF gradient tape as tape. You need to provide code to compute the value of the cost function J. So recall that in collaborative filtering, the cost function J takes this input parameters X, W and B as well as the ratings mean normalized. So that's why I'm writing Y norm, R ij specifying which values have a rating, number of users or N U in a notation, number of movies or N M in a notation just now, as well as the regularization parameter lambda. And if you can implement this cost function J, then this syntax will cause TensorFlow to figure out the derivatives for you. Then this syntax will cause TensorFlow to record the sequence of operations used to compute the cost. And then by asking it to give you grads equals tape dot gradient, this will give you the derivative of the cost function with respect to X, W and B. And finally, with the optimizer that we had specified up on top as the Adam optimizer, you can use the optimizer with the gradients that we just computed. And does it function in Python is just a function that rearranges the numbers into an appropriate ordering for the applied gradients function. If you are using gradient descent for collaborative filtering, recall that the cost function J would be a function of W, B as well as X. And if you're applying gradient descent, you take the partial derivative with respect to W and then update W as follows. And you'd also take the partial derivative of this with respect to B and update B as follows and similarly update the features X as follows and you repeat until conversions. But as I mentioned earlier, with TensorFlow and auto diff, you're not limited to just gradient descent. You can also use a more powerful optimization algorithm like the Adam optimizer. The data set you use in the practice lab is a real data set comprising actual movies rated by actual people. This is the movie lens data set and is due to Harper and constant. And I hope you enjoy running this algorithm on a real data set of movies and ratings and see for yourself the results that this algorithm can get. So that's it. That's how you can implement the cloud filtering algorithm in TensorFlow. If you're wondering, why do we have to do it this way? Why couldn't we use a dense layer and then model compile and model fit? The reason we couldn't use that old recipe is the collaborative filtering algorithm and cost function, it doesn't neatly fit into the dense layer or the other standard neural network layer types of TensorFlow. That's why we had to implement it this other way, where we would implement the cost function ourselves, but then use TensorFlow's tools for automatic differentiation, also called auto diff and use TensorFlow's implementation of the Adam optimization algorithm to let it do a lot of the work for us of optimizing the cost function. If the model you have is a sequence of dense neural network layers or other types of layers supported by TensorFlow, then the old implementation recipe of model compile model fit works. But even when it isn't, these tools in TensorFlow give you a very effective way to implement other learning algorithms as well. And so I hope you enjoy playing more with the collaborative filtering exercise in this week's practice lab, and if it looks like there's a lot of code and lots of syntax, don't worry about it. We'll make sure you have what you need to complete that exercise successfully. And in the next video, I'd like to also move on to discuss more of the nuances of collaborative filtering and specifically the question of how do you find related items given one movie or the other movies similar to this one. Let's go on to the next video.
[{"start": 0.0, "end": 7.24, "text": " In this video, we'll take a look at how you can use TensorFlow to implement the collaborative"}, {"start": 7.24, "end": 9.44, "text": " filtering algorithm."}, {"start": 9.44, "end": 14.08, "text": " You might be used to thinking of TensorFlow as a tool for building neural networks, and"}, {"start": 14.08, "end": 15.08, "text": " it is."}, {"start": 15.08, "end": 17.56, "text": " It's a great tool for building neural networks."}, {"start": 17.56, "end": 22.32, "text": " And it turns out that TensorFlow can also be very helpful for building other types of"}, {"start": 22.32, "end": 27.240000000000002, "text": " learning algorithms as well, like the collaborative filtering algorithm."}, {"start": 27.24, "end": 33.879999999999995, "text": " One of the reasons I like using TensorFlow for tasks like these is that for many applications,"}, {"start": 33.879999999999995, "end": 38.68, "text": " in order to implement gradient descent, say, you need to find the derivatives of the cost"}, {"start": 38.68, "end": 39.68, "text": " function."}, {"start": 39.68, "end": 46.44, "text": " But TensorFlow can automatically figure out for you what are the derivatives of a cost"}, {"start": 46.44, "end": 47.519999999999996, "text": " function."}, {"start": 47.519999999999996, "end": 50.72, "text": " All you have to do is implement the cost function."}, {"start": 50.72, "end": 55.28, "text": " And without needing to know any calculus, without needing to take derivatives yourself,"}, {"start": 55.28, "end": 60.120000000000005, "text": " you can get TensorFlow with just a few lines of code to compute that derivative term that"}, {"start": 60.120000000000005, "end": 63.28, "text": " can then be used to optimize the cost function."}, {"start": 63.28, "end": 65.94, "text": " Let's take a look at how all this works."}, {"start": 65.94, "end": 70.92, "text": " You might remember this diagram here on the right from course one."}, {"start": 70.92, "end": 76.88, "text": " This is exactly the diagram that we had looked at when we talked about optimizing W when"}, {"start": 76.88, "end": 81.68, "text": " we were working through our first linear regression example."}, {"start": 81.68, "end": 85.02000000000001, "text": " And at that time, we had set B equals to zero."}, {"start": 85.02, "end": 89.47999999999999, "text": " And so the model was just predicting f of x equals W dot x."}, {"start": 89.47999999999999, "end": 94.75999999999999, "text": " And we wanted to find the value of W that minimizes the cost function J."}, {"start": 94.75999999999999, "end": 101.36, "text": " So the way we were doing that was via a gradient descent update, which looks like this, where"}, {"start": 101.36, "end": 107.8, "text": " W gets repeatedly updated as W minus the learning rate alpha times the derivative term."}, {"start": 107.8, "end": 112.03999999999999, "text": " If you were updating B as well, this is the expression you would use."}, {"start": 112.04, "end": 116.80000000000001, "text": " But if you set B equals zero, you just forego the second update."}, {"start": 116.80000000000001, "end": 122.16000000000001, "text": " And you keep on performing this gradient descent update until convergence."}, {"start": 122.16000000000001, "end": 128.68, "text": " Sometimes computing this derivative or partial derivative term can be difficult."}, {"start": 128.68, "end": 132.68, "text": " And it turns out that TensorFlow can help with that."}, {"start": 132.68, "end": 133.76, "text": " Let's see how."}, {"start": 133.76, "end": 143.0, "text": " I'm going to use a very simple cost function J equals W x minus one squared."}, {"start": 143.0, "end": 148.6, "text": " So W x is our simplified f W of x."}, {"start": 148.6, "end": 150.68, "text": " And y is equal to one."}, {"start": 150.68, "end": 153.85999999999999, "text": " And so this would be the cost function."}, {"start": 153.85999999999999, "end": 162.2, "text": " If we had f of x equals W x, y equals one for the one training example that we have."}, {"start": 162.2, "end": 165.48, "text": " And if we were not optimizing this with respect to B."}, {"start": 165.48, "end": 171.2, "text": " So the gradient descent algorithm would repeat until convergence this update over here."}, {"start": 171.2, "end": 177.39999999999998, "text": " It turns out that if you implement the cost function J over here, TensorFlow can automatically"}, {"start": 177.39999999999998, "end": 182.72, "text": " compute for you this derivative term and thereby get gradient descent to work."}, {"start": 182.72, "end": 187.67999999999998, "text": " I'll give you a high level overview of what this code does."}, {"start": 187.68, "end": 195.20000000000002, "text": " If you equals tf.variable three takes the parameter W and initializes it to the value"}, {"start": 195.20000000000002, "end": 203.20000000000002, "text": " of three telling TensorFlow that W is a variable is how we tell it that W is a parameter that"}, {"start": 203.20000000000002, "end": 204.68, "text": " we want to optimize."}, {"start": 204.68, "end": 209.64000000000001, "text": " I'm going to set x equals one y equals one and the learning rate alpha to be equal to"}, {"start": 209.64000000000001, "end": 211.76000000000002, "text": " zero point zero one."}, {"start": 211.76000000000002, "end": 215.38, "text": " And let's run gradient descent for 30 iterations."}, {"start": 215.38, "end": 218.79999999999998, "text": " So in this code will do for either arrange iterations."}, {"start": 218.79999999999998, "end": 220.84, "text": " So for 30 iterations."}, {"start": 220.84, "end": 226.24, "text": " And this is a syntax to get TensorFlow to automatically compute derivatives for you."}, {"start": 226.24, "end": 229.88, "text": " TensorFlow has a feature called a gradient tape."}, {"start": 229.88, "end": 238.16, "text": " And if you write this with TF by gradient tape as tape F, this is compute f of x as"}, {"start": 238.16, "end": 245.44, "text": " W times x and compute J as f of x minus y squared."}, {"start": 245.44, "end": 251.92, "text": " Then by telling TensorFlow how to compute the cost J and by doing it with a gradient"}, {"start": 251.92, "end": 257.64, "text": " tape syntax as follows, TensorFlow will automatically record the sequence of steps, the sequence"}, {"start": 257.64, "end": 265.36, "text": " of operations needed to compute the cost J. And this is needed to enable automatic differentiation."}, {"start": 265.36, "end": 273.08000000000004, "text": " Next, TensorFlow will have saved the sequence of operations in tape in the gradient tape."}, {"start": 273.08000000000004, "end": 279.68, "text": " And with this syntax, TensorFlow will automatically compute this derivative term, which I'm going"}, {"start": 279.68, "end": 283.32, "text": " to call DJ DW."}, {"start": 283.32, "end": 289.04, "text": " And TensorFlow knows you want to take the derivative with respect to W that W is a parameter"}, {"start": 289.04, "end": 294.12, "text": " you want to optimize because you had told it so up here and because we're also specifying"}, {"start": 294.12, "end": 295.84000000000003, "text": " it down here."}, {"start": 295.84000000000003, "end": 302.08, "text": " So now that you've computed derivatives, finally, you can carry out this update by"}, {"start": 302.08, "end": 308.28000000000003, "text": " taking W and subtracting from it the learning rate alpha times that derivative term that"}, {"start": 308.28000000000003, "end": 311.08, "text": " we just got from up above."}, {"start": 311.08, "end": 316.24, "text": " TensorFlow variables, tier variables, require special handling, which is why instead of"}, {"start": 316.24, "end": 323.48, "text": " setting W to be W minus alpha times the derivative in the usual way, we use this assign add function."}, {"start": 323.48, "end": 327.48, "text": " But when you get to the practice lab, don't worry about it, we'll give you all the syntax"}, {"start": 327.48, "end": 331.36, "text": " you need in order to implement a collaborative filtering algorithm correctly."}, {"start": 331.36, "end": 338.16, "text": " So notice that with the gradient tape feature of TensorFlow, the main work you need to do"}, {"start": 338.16, "end": 344.64000000000004, "text": " is to tell it how to compute the cost function J. And the rest of the syntax causes TensorFlow"}, {"start": 344.64000000000004, "end": 350.56, "text": " to automatically figure out for you what is that derivative."}, {"start": 350.56, "end": 355.24, "text": " And with this, TensorFlow will start with finding the slope of this at three shown by"}, {"start": 355.24, "end": 364.0, "text": " this dashed line, take a gradient step and update W and compute the derivative again"}, {"start": 364.0, "end": 370.72, "text": " and update W over and over until eventually it gets to the optimal value of W, which is"}, {"start": 370.72, "end": 373.06, "text": " at W equals one."}, {"start": 373.06, "end": 378.56, "text": " So this procedure allows you to implement gradient descent without ever having to figure"}, {"start": 378.56, "end": 382.72, "text": " out yourself how to compute this derivative term."}, {"start": 382.72, "end": 388.0, "text": " This is a very powerful feature of TensorFlow called auto diff."}, {"start": 388.0, "end": 394.16, "text": " And some other machine learning packages like PyTorch also support auto diff."}, {"start": 394.16, "end": 397.2, "text": " Sometimes you hear people call this auto grad."}, {"start": 397.2, "end": 402.4, "text": " The technically correct term is auto diff and auto grad is actually the name of a specific"}, {"start": 402.4, "end": 407.56, "text": " software package for doing automatic differentiation, for taking derivatives automatically."}, {"start": 407.56, "end": 411.4, "text": " And sometimes if you hear someone refer to auto grad, they're just referring to this"}, {"start": 411.4, "end": 414.68, "text": " same concept of automatically taking derivatives."}, {"start": 414.68, "end": 419.32, "text": " So let's take this and look at how you can implement the collaborative filtering algorithm"}, {"start": 419.32, "end": 421.68, "text": " using auto diff."}, {"start": 421.68, "end": 426.52, "text": " And in fact, once you can compute derivatives automatically, you're not limited to just"}, {"start": 426.52, "end": 427.52, "text": " gradient descent."}, {"start": 427.52, "end": 434.12, "text": " You can also use a more powerful optimization algorithm like the Adam optimization algorithm."}, {"start": 434.12, "end": 439.04, "text": " In order to implement the collaborative filtering algorithm in TensorFlow, this is the syntax"}, {"start": 439.04, "end": 440.04, "text": " you can use."}, {"start": 440.04, "end": 448.0, "text": " Let's start with specifying that the optimizer is Keras optimizers, Adam with learning rate"}, {"start": 448.0, "end": 449.92, "text": " specified here."}, {"start": 449.92, "end": 457.36, "text": " And then for say 200 iterations, here's the syntax as before with TF gradient tape as"}, {"start": 457.36, "end": 458.36, "text": " tape."}, {"start": 458.36, "end": 463.32, "text": " You need to provide code to compute the value of the cost function J."}, {"start": 463.32, "end": 470.08, "text": " So recall that in collaborative filtering, the cost function J takes this input parameters"}, {"start": 470.08, "end": 475.52, "text": " X, W and B as well as the ratings mean normalized."}, {"start": 475.52, "end": 482.88, "text": " So that's why I'm writing Y norm, R ij specifying which values have a rating, number of users"}, {"start": 482.88, "end": 488.32, "text": " or N U in a notation, number of movies or N M in a notation just now, as well as the"}, {"start": 488.32, "end": 490.68, "text": " regularization parameter lambda."}, {"start": 490.68, "end": 495.72, "text": " And if you can implement this cost function J, then this syntax will cause TensorFlow"}, {"start": 495.72, "end": 498.32, "text": " to figure out the derivatives for you."}, {"start": 498.32, "end": 502.16, "text": " Then this syntax will cause TensorFlow to record the sequence of operations used to"}, {"start": 502.16, "end": 503.66, "text": " compute the cost."}, {"start": 503.66, "end": 509.6, "text": " And then by asking it to give you grads equals tape dot gradient, this will give you the"}, {"start": 509.6, "end": 516.92, "text": " derivative of the cost function with respect to X, W and B."}, {"start": 516.92, "end": 522.8399999999999, "text": " And finally, with the optimizer that we had specified up on top as the Adam optimizer,"}, {"start": 522.8399999999999, "end": 528.4, "text": " you can use the optimizer with the gradients that we just computed."}, {"start": 528.4, "end": 533.16, "text": " And does it function in Python is just a function that rearranges the numbers into an appropriate"}, {"start": 533.16, "end": 535.76, "text": " ordering for the applied gradients function."}, {"start": 535.76, "end": 541.04, "text": " If you are using gradient descent for collaborative filtering, recall that the cost function J"}, {"start": 541.04, "end": 547.56, "text": " would be a function of W, B as well as X. And if you're applying gradient descent, you"}, {"start": 547.56, "end": 553.06, "text": " take the partial derivative with respect to W and then update W as follows."}, {"start": 553.06, "end": 557.64, "text": " And you'd also take the partial derivative of this with respect to B and update B as"}, {"start": 557.64, "end": 565.0799999999999, "text": " follows and similarly update the features X as follows and you repeat until conversions."}, {"start": 565.08, "end": 571.24, "text": " But as I mentioned earlier, with TensorFlow and auto diff, you're not limited to just"}, {"start": 571.24, "end": 572.5200000000001, "text": " gradient descent."}, {"start": 572.5200000000001, "end": 577.8000000000001, "text": " You can also use a more powerful optimization algorithm like the Adam optimizer."}, {"start": 577.8000000000001, "end": 583.36, "text": " The data set you use in the practice lab is a real data set comprising actual movies rated"}, {"start": 583.36, "end": 585.58, "text": " by actual people."}, {"start": 585.58, "end": 590.36, "text": " This is the movie lens data set and is due to Harper and constant."}, {"start": 590.36, "end": 595.8000000000001, "text": " And I hope you enjoy running this algorithm on a real data set of movies and ratings and"}, {"start": 595.8000000000001, "end": 599.08, "text": " see for yourself the results that this algorithm can get."}, {"start": 599.08, "end": 600.08, "text": " So that's it."}, {"start": 600.08, "end": 604.44, "text": " That's how you can implement the cloud filtering algorithm in TensorFlow."}, {"start": 604.44, "end": 606.6800000000001, "text": " If you're wondering, why do we have to do it this way?"}, {"start": 606.6800000000001, "end": 611.88, "text": " Why couldn't we use a dense layer and then model compile and model fit?"}, {"start": 611.88, "end": 616.6, "text": " The reason we couldn't use that old recipe is the collaborative filtering algorithm and"}, {"start": 616.6, "end": 622.08, "text": " cost function, it doesn't neatly fit into the dense layer or the other standard neural"}, {"start": 622.08, "end": 624.76, "text": " network layer types of TensorFlow."}, {"start": 624.76, "end": 628.84, "text": " That's why we had to implement it this other way, where we would implement the cost function"}, {"start": 628.84, "end": 634.08, "text": " ourselves, but then use TensorFlow's tools for automatic differentiation, also called"}, {"start": 634.08, "end": 639.52, "text": " auto diff and use TensorFlow's implementation of the Adam optimization algorithm to let"}, {"start": 639.52, "end": 644.38, "text": " it do a lot of the work for us of optimizing the cost function."}, {"start": 644.38, "end": 650.84, "text": " If the model you have is a sequence of dense neural network layers or other types of layers"}, {"start": 650.84, "end": 657.72, "text": " supported by TensorFlow, then the old implementation recipe of model compile model fit works."}, {"start": 657.72, "end": 663.04, "text": " But even when it isn't, these tools in TensorFlow give you a very effective way to implement"}, {"start": 663.04, "end": 665.58, "text": " other learning algorithms as well."}, {"start": 665.58, "end": 670.56, "text": " And so I hope you enjoy playing more with the collaborative filtering exercise in this"}, {"start": 670.56, "end": 674.5999999999999, "text": " week's practice lab, and if it looks like there's a lot of code and lots of syntax,"}, {"start": 674.5999999999999, "end": 675.5999999999999, "text": " don't worry about it."}, {"start": 675.5999999999999, "end": 680.8399999999999, "text": " We'll make sure you have what you need to complete that exercise successfully."}, {"start": 680.8399999999999, "end": 687.64, "text": " And in the next video, I'd like to also move on to discuss more of the nuances of collaborative"}, {"start": 687.64, "end": 693.9599999999999, "text": " filtering and specifically the question of how do you find related items given one movie"}, {"start": 693.9599999999999, "end": 696.4399999999999, "text": " or the other movies similar to this one."}, {"start": 696.44, "end": 703.44, "text": " Let's go on to the next video."}]