5.1 Neural Network Training | TensorFlow implementation --[Machine Learning | Andrew Ng]
 Welcome back to the second week of this course on advanced learning algorithms. Last week you learned how to carry out inference in a neural network. This week we're going to go over training of a neural network. I think being able to take your own data and train your own neural network on it is really fun. This week we'll look at how you could do that. Let's dive in. Let's continue with our running example of handwritten digit recognition, recognizing this image as 0 or a 1. And here we're using the neural network architecture that you saw last week, where you have an input x that is the image, and then the first hidden layer with 25 units, second hidden layer with 15 units, and then one output unit. If you are given a training set of examples comprising images x as well as the ground truth label y, how would you train the parameters of this neural network? Let me go ahead and show you the code that you can use in TensorFlow to train this network. And then in the next few videos after this, we'll dive into details to explain what the code is actually doing. So this is the code you would write. This first part may look familiar from the previous week, where you are asking TensorFlow to sequentially string together these three layers of a neural network, the first hidden layer with 25 units and sig 1 activation, the second hidden layer, and then finally the output layer. So nothing new here relative to what you saw last week. Second step is you have to ask TensorFlow to compile the model. And the key step in asking TensorFlow to compile the model is to specify what is the loss function you want to use. In this case, we'll use something that goes by the arcane name of sparse categorical cross entropy. We'll say more in the next video what this really is. And then having specified the loss function, the third step is to call the fit function, which tells TensorFlow to fit the model that you specified in step one. Using the loss of the cost function that you specified in step two to the data set x, y. And back in the first course, when we talked about gradient descent, we had to decide how many steps to run gradient descent or how long to run gradient descent. So epochs is a technical term for how many steps of learning algorithm like gradient descent you may want to run. And that's it. Step one is to specify the model, which tells TensorFlow how to compute for the inference. Step two compiles the model using a specific loss function. And step three is to train the model. So that's how you can train a neural network in TensorFlow. As usual, I hope that you'll be able to not just call these lines of code to train the model, but that you also understand what's actually going on behind these lines of code. So you don't just call it without really understanding what's going on. And I think this is important because when you're running a learning algorithm, if it doesn't work initially, having that conceptual mental framework of what's really going on will help you debug whenever things don't work the way you expect. So with that, let's go on to the next video, where we'll dive more deeply into what these steps in the TensorFlow implementation are actually doing. I'll see you in the next video.
[{"start": 0.0, "end": 7.640000000000001, "text": " Welcome back to the second week of this course on advanced learning algorithms."}, {"start": 7.640000000000001, "end": 11.76, "text": " Last week you learned how to carry out inference in a neural network."}, {"start": 11.76, "end": 15.76, "text": " This week we're going to go over training of a neural network."}, {"start": 15.76, "end": 20.8, "text": " I think being able to take your own data and train your own neural network on it is really"}, {"start": 20.8, "end": 21.8, "text": " fun."}, {"start": 21.8, "end": 24.240000000000002, "text": " This week we'll look at how you could do that."}, {"start": 24.240000000000002, "end": 25.240000000000002, "text": " Let's dive in."}, {"start": 25.24, "end": 31.64, "text": " Let's continue with our running example of handwritten digit recognition, recognizing"}, {"start": 31.64, "end": 34.76, "text": " this image as 0 or a 1."}, {"start": 34.76, "end": 40.04, "text": " And here we're using the neural network architecture that you saw last week, where you have an"}, {"start": 40.04, "end": 46.519999999999996, "text": " input x that is the image, and then the first hidden layer with 25 units, second hidden"}, {"start": 46.519999999999996, "end": 50.94, "text": " layer with 15 units, and then one output unit."}, {"start": 50.94, "end": 56.72, "text": " If you are given a training set of examples comprising images x as well as the ground"}, {"start": 56.72, "end": 62.12, "text": " truth label y, how would you train the parameters of this neural network?"}, {"start": 62.12, "end": 67.52, "text": " Let me go ahead and show you the code that you can use in TensorFlow to train this network."}, {"start": 67.52, "end": 71.32, "text": " And then in the next few videos after this, we'll dive into details to explain what the"}, {"start": 71.32, "end": 73.68, "text": " code is actually doing."}, {"start": 73.68, "end": 75.56, "text": " So this is the code you would write."}, {"start": 75.56, "end": 80.92, "text": " This first part may look familiar from the previous week, where you are asking TensorFlow"}, {"start": 80.92, "end": 86.68, "text": " to sequentially string together these three layers of a neural network, the first hidden"}, {"start": 86.68, "end": 92.6, "text": " layer with 25 units and sig 1 activation, the second hidden layer, and then finally"}, {"start": 92.6, "end": 94.16, "text": " the output layer."}, {"start": 94.16, "end": 98.36, "text": " So nothing new here relative to what you saw last week."}, {"start": 98.36, "end": 102.80000000000001, "text": " Second step is you have to ask TensorFlow to compile the model."}, {"start": 102.8, "end": 108.84, "text": " And the key step in asking TensorFlow to compile the model is to specify what is the loss function"}, {"start": 108.84, "end": 110.2, "text": " you want to use."}, {"start": 110.2, "end": 115.88, "text": " In this case, we'll use something that goes by the arcane name of sparse categorical cross"}, {"start": 115.88, "end": 116.88, "text": " entropy."}, {"start": 116.88, "end": 120.75999999999999, "text": " We'll say more in the next video what this really is."}, {"start": 120.75999999999999, "end": 127.12, "text": " And then having specified the loss function, the third step is to call the fit function,"}, {"start": 127.12, "end": 131.72, "text": " which tells TensorFlow to fit the model that you specified in step one."}, {"start": 131.72, "end": 138.4, "text": " Using the loss of the cost function that you specified in step two to the data set x, y."}, {"start": 138.4, "end": 143.56, "text": " And back in the first course, when we talked about gradient descent, we had to decide how"}, {"start": 143.56, "end": 147.2, "text": " many steps to run gradient descent or how long to run gradient descent."}, {"start": 147.2, "end": 151.92, "text": " So epochs is a technical term for how many steps of learning algorithm like gradient"}, {"start": 151.92, "end": 155.36, "text": " descent you may want to run."}, {"start": 155.36, "end": 156.44, "text": " And that's it."}, {"start": 156.44, "end": 163.68, "text": " Step one is to specify the model, which tells TensorFlow how to compute for the inference."}, {"start": 163.68, "end": 168.07999999999998, "text": " Step two compiles the model using a specific loss function."}, {"start": 168.07999999999998, "end": 170.96, "text": " And step three is to train the model."}, {"start": 170.96, "end": 174.8, "text": " So that's how you can train a neural network in TensorFlow."}, {"start": 174.8, "end": 179.48, "text": " As usual, I hope that you'll be able to not just call these lines of code to train the"}, {"start": 179.48, "end": 185.14, "text": " model, but that you also understand what's actually going on behind these lines of code."}, {"start": 185.14, "end": 189.67999999999998, "text": " So you don't just call it without really understanding what's going on."}, {"start": 189.67999999999998, "end": 194.79999999999998, "text": " And I think this is important because when you're running a learning algorithm, if it"}, {"start": 194.79999999999998, "end": 200.88, "text": " doesn't work initially, having that conceptual mental framework of what's really going on"}, {"start": 200.88, "end": 206.06, "text": " will help you debug whenever things don't work the way you expect."}, {"start": 206.06, "end": 211.04, "text": " So with that, let's go on to the next video, where we'll dive more deeply into what these"}, {"start": 211.04, "end": 214.92, "text": " steps in the TensorFlow implementation are actually doing."}, {"start": 214.92, "end": 216.2, "text": " I'll see you in the next video."}]