2.3 Linear Regression with Multiple Variables | Vectorization part 2 --[Machine Learning |Andrew Ng]
 I remember when I first learned about vectorization, I spent many hours on my computer taking an un-vectorized version of an algorithm, running it, see how long it ran, and then running a vectorized version of the code and seeing how much faster that ran. And I just spent hours playing with that. And it frankly blew my mind that the same algorithm, vectorized, would run so much faster. It felt almost like a magic trick to me. In this video, let's figure out how this magic trick really works. Let's take a deeper look at how a vectorized implementation may work on your computer behind the scenes. Let's look at this for loop. A for loop like this runs without vectorization. So if j ranges from 0 to, say, 15, this piece of code performs operations one after another. On the first time step, which I'm going to write as time 0 or t0, it first operates on the values at index 0. At the next time step, it calculates values corresponding to index 1 and so on until the fifth theme step, where it computes that. In other words, it calculates these computations one step at a time, one step after another. In contrast, this function in NumPy is implemented in the computer hardware with vectorization. So the computer can get all values of the vectors w and x, and in a single step, it multiplies each pair of w and x with each other all at the same time in parallel. Then after that, the computer takes these 16 numbers and uses specialized hardware to add them all together very efficiently, rather than needing to carry out distinct additions one after another to add up these 16 numbers. This means that code with vectorization can perform calculations in much less time than code without vectorization. And this matters more when you're running learning algorithms on large data sets or trying to train large models, which is often the case with machine learning. So that's why being able to write vectorized implementations of learning algorithms has been a key step to getting learning algorithms to run efficiently and therefore scale well to the large data sets that many modern machine learning algorithms now have to operate on. Now let's take a look at a concrete example of how this helps with implementing multiple linear regression, that is linear regression with multiple input features. Say you have a problem with 16 features and 16 parameters w1 through w16 in addition to the parameter b. You calculated 16 derivative terms for these 16 weights and in code, maybe you stored the values of w and d in two non-pyrotries with d storing the values of the derivatives. For this example, I'm just going to ignore the parameter b. Now you want to compute and update for each of these 16 parameters. So wj is updated to wj minus the learning rate, say 0.1 times dj for j from 1 through 16. In code, without vectorization, you would be doing something like this, update w1 to be w1 minus the learning rate 0.1 times d1, next update w2 similarly and so on through w16, updated as w16 minus 0.1 times d16. In code without vectorization, you could use a full loop like this for j in range 0, 16, that again goes from 0 to 15, set wj equals wj minus 0.1 times dj. In contrast, with vectorization, you can imagine the computer's parallel processing hardware like this. It takes all 16 values in the vector w and subtracts in parallel 0.1 times all 16 values in the vector d and assign all 16 calculations back to w all at the same time and all in one step. In code, you can implement this as follows. W is assigned to w minus 0.1 times d. Behind the scenes, the computer takes these numpy arrays w and d and uses parallel processing hardware to carry out all 16 computations efficiently. So using a vectorized implementation, you should get a much more efficient implementation of linear regression. Maybe the speed difference won't be huge if you have 16 features, but if you have thousands of features and perhaps very large training sets, this type of vectorized implementation will make a huge difference in the running time of your learning algorithm. It could be the difference between code finishing in one or two minutes versus taking many, many hours to do the same thing. In the optional lab that follows this video, you see an introduction to one of the most used Python libraries in machine learning, which we've already touched on in this video called numpy. You see how they create vectors in code, and these vectors or lists of numbers are called numpy arrays. And you also see how to take the dot product of two vectors using a numpy function called dot. And you also get to see how vectorized code, such as using the dot function, can run much faster than a for loop. In fact, you get to time this code yourself and hopefully see it run much faster. This optional lab introduces a fair amount of new numpy syntax. So when you read through the optional lab, please don't feel like you have to understand all the code right away. But you can save this notebook and use it as a reference to look at when you're working with data stored in numpy arrays. So congrats on finishing this video on vectorization. You've learned one of the most important and useful techniques in implementing machine learning algorithms. In the next video, we'll put the math of multiple linear regression together with vectorization so that you really implement gradient descent for multiple linear regression with vectorization. Let's go on to the next video.
[{"start": 0.0, "end": 8.08, "text": " I remember when I first learned about vectorization, I spent many hours on my computer taking an"}, {"start": 8.08, "end": 12.9, "text": " un-vectorized version of an algorithm, running it, see how long it ran, and then running"}, {"start": 12.9, "end": 16.2, "text": " a vectorized version of the code and seeing how much faster that ran."}, {"start": 16.2, "end": 18.64, "text": " And I just spent hours playing with that."}, {"start": 18.64, "end": 24.240000000000002, "text": " And it frankly blew my mind that the same algorithm, vectorized, would run so much faster."}, {"start": 24.240000000000002, "end": 27.16, "text": " It felt almost like a magic trick to me."}, {"start": 27.16, "end": 31.44, "text": " In this video, let's figure out how this magic trick really works."}, {"start": 31.44, "end": 36.08, "text": " Let's take a deeper look at how a vectorized implementation may work on your computer behind"}, {"start": 36.08, "end": 37.32, "text": " the scenes."}, {"start": 37.32, "end": 39.44, "text": " Let's look at this for loop."}, {"start": 39.44, "end": 43.16, "text": " A for loop like this runs without vectorization."}, {"start": 43.16, "end": 52.66, "text": " So if j ranges from 0 to, say, 15, this piece of code performs operations one after another."}, {"start": 52.66, "end": 59.31999999999999, "text": " On the first time step, which I'm going to write as time 0 or t0, it first operates on"}, {"start": 59.31999999999999, "end": 62.199999999999996, "text": " the values at index 0."}, {"start": 62.199999999999996, "end": 68.72, "text": " At the next time step, it calculates values corresponding to index 1 and so on until the"}, {"start": 68.72, "end": 73.56, "text": " fifth theme step, where it computes that."}, {"start": 73.56, "end": 81.08, "text": " In other words, it calculates these computations one step at a time, one step after another."}, {"start": 81.08, "end": 88.67999999999999, "text": " In contrast, this function in NumPy is implemented in the computer hardware with vectorization."}, {"start": 88.67999999999999, "end": 95.67999999999999, "text": " So the computer can get all values of the vectors w and x, and in a single step, it"}, {"start": 95.67999999999999, "end": 102.52, "text": " multiplies each pair of w and x with each other all at the same time in parallel."}, {"start": 102.52, "end": 107.92, "text": " Then after that, the computer takes these 16 numbers and uses specialized hardware to"}, {"start": 107.92, "end": 114.08, "text": " add them all together very efficiently, rather than needing to carry out distinct additions"}, {"start": 114.08, "end": 118.4, "text": " one after another to add up these 16 numbers."}, {"start": 118.4, "end": 123.84, "text": " This means that code with vectorization can perform calculations in much less time than"}, {"start": 123.84, "end": 126.56, "text": " code without vectorization."}, {"start": 126.56, "end": 131.1, "text": " And this matters more when you're running learning algorithms on large data sets or"}, {"start": 131.1, "end": 136.4, "text": " trying to train large models, which is often the case with machine learning."}, {"start": 136.4, "end": 140.68, "text": " So that's why being able to write vectorized implementations of learning algorithms has"}, {"start": 140.68, "end": 146.64000000000001, "text": " been a key step to getting learning algorithms to run efficiently and therefore scale well"}, {"start": 146.64000000000001, "end": 152.48000000000002, "text": " to the large data sets that many modern machine learning algorithms now have to operate on."}, {"start": 152.48000000000002, "end": 158.06, "text": " Now let's take a look at a concrete example of how this helps with implementing multiple"}, {"start": 158.06, "end": 163.82, "text": " linear regression, that is linear regression with multiple input features."}, {"start": 163.82, "end": 173.07999999999998, "text": " Say you have a problem with 16 features and 16 parameters w1 through w16 in addition to"}, {"start": 173.07999999999998, "end": 175.6, "text": " the parameter b."}, {"start": 175.6, "end": 182.2, "text": " You calculated 16 derivative terms for these 16 weights and in code, maybe you stored the"}, {"start": 182.2, "end": 190.76, "text": " values of w and d in two non-pyrotries with d storing the values of the derivatives."}, {"start": 190.76, "end": 194.92, "text": " For this example, I'm just going to ignore the parameter b."}, {"start": 194.92, "end": 200.48, "text": " Now you want to compute and update for each of these 16 parameters."}, {"start": 200.48, "end": 213.2, "text": " So wj is updated to wj minus the learning rate, say 0.1 times dj for j from 1 through"}, {"start": 213.2, "end": 214.2, "text": " 16."}, {"start": 214.2, "end": 221.6, "text": " In code, without vectorization, you would be doing something like this, update w1 to"}, {"start": 221.6, "end": 230.79999999999998, "text": " be w1 minus the learning rate 0.1 times d1, next update w2 similarly and so on through"}, {"start": 230.79999999999998, "end": 237.83999999999997, "text": " w16, updated as w16 minus 0.1 times d16."}, {"start": 237.84, "end": 246.16, "text": " In code without vectorization, you could use a full loop like this for j in range 0, 16,"}, {"start": 246.16, "end": 254.4, "text": " that again goes from 0 to 15, set wj equals wj minus 0.1 times dj."}, {"start": 254.4, "end": 260.92, "text": " In contrast, with vectorization, you can imagine the computer's parallel processing hardware"}, {"start": 260.92, "end": 262.04, "text": " like this."}, {"start": 262.04, "end": 271.40000000000003, "text": " It takes all 16 values in the vector w and subtracts in parallel 0.1 times all 16 values"}, {"start": 271.40000000000003, "end": 279.16, "text": " in the vector d and assign all 16 calculations back to w all at the same time and all in"}, {"start": 279.16, "end": 280.48, "text": " one step."}, {"start": 280.48, "end": 285.24, "text": " In code, you can implement this as follows."}, {"start": 285.24, "end": 290.24, "text": " W is assigned to w minus 0.1 times d."}, {"start": 290.24, "end": 296.32, "text": " Behind the scenes, the computer takes these numpy arrays w and d and uses parallel processing"}, {"start": 296.32, "end": 301.0, "text": " hardware to carry out all 16 computations efficiently."}, {"start": 301.0, "end": 305.6, "text": " So using a vectorized implementation, you should get a much more efficient implementation"}, {"start": 305.6, "end": 307.8, "text": " of linear regression."}, {"start": 307.8, "end": 313.64, "text": " Maybe the speed difference won't be huge if you have 16 features, but if you have thousands"}, {"start": 313.64, "end": 318.24, "text": " of features and perhaps very large training sets, this type of vectorized implementation"}, {"start": 318.24, "end": 322.04, "text": " will make a huge difference in the running time of your learning algorithm."}, {"start": 322.04, "end": 326.88, "text": " It could be the difference between code finishing in one or two minutes versus taking many,"}, {"start": 326.88, "end": 329.68, "text": " many hours to do the same thing."}, {"start": 329.68, "end": 334.76, "text": " In the optional lab that follows this video, you see an introduction to one of the most"}, {"start": 334.76, "end": 339.16, "text": " used Python libraries in machine learning, which we've already touched on in this video"}, {"start": 339.16, "end": 341.40000000000003, "text": " called numpy."}, {"start": 341.40000000000003, "end": 346.62, "text": " You see how they create vectors in code, and these vectors or lists of numbers are called"}, {"start": 346.62, "end": 348.72, "text": " numpy arrays."}, {"start": 348.72, "end": 354.68, "text": " And you also see how to take the dot product of two vectors using a numpy function called"}, {"start": 354.68, "end": 356.76, "text": " dot."}, {"start": 356.76, "end": 362.16, "text": " And you also get to see how vectorized code, such as using the dot function, can run much"}, {"start": 362.16, "end": 364.36, "text": " faster than a for loop."}, {"start": 364.36, "end": 369.84000000000003, "text": " In fact, you get to time this code yourself and hopefully see it run much faster."}, {"start": 369.84000000000003, "end": 374.36, "text": " This optional lab introduces a fair amount of new numpy syntax."}, {"start": 374.36, "end": 379.04, "text": " So when you read through the optional lab, please don't feel like you have to understand"}, {"start": 379.04, "end": 380.92, "text": " all the code right away."}, {"start": 380.92, "end": 385.28000000000003, "text": " But you can save this notebook and use it as a reference to look at when you're working"}, {"start": 385.28000000000003, "end": 388.0, "text": " with data stored in numpy arrays."}, {"start": 388.0, "end": 391.56, "text": " So congrats on finishing this video on vectorization."}, {"start": 391.56, "end": 395.48, "text": " You've learned one of the most important and useful techniques in implementing machine"}, {"start": 395.48, "end": 397.12, "text": " learning algorithms."}, {"start": 397.12, "end": 403.7, "text": " In the next video, we'll put the math of multiple linear regression together with vectorization"}, {"start": 403.7, "end": 409.84, "text": " so that you really implement gradient descent for multiple linear regression with vectorization."}, {"start": 409.84, "end": 436.84, "text": " Let's go on to the next video."}]