We'd like to know you better so we can create more relevant courses. What do you do for work?
Course Syllabus
You've achieved today's streak!
Complete one lesson every day to keep the streak going.
Su
Mo
Tu
We
Th
Fr
Sa
You earned a Free Pass!
Free Passes help protect your daily streak. Complete more lessons to earn up to 3 Free Passes.
Elevate Your Career with Full Learning Experience
Unlock Plus AI learning and gain exclusive insights from industry leaders
Access exclusive features like graded notebooks and quizzes
Earn unlimited certificates to enhance your resume
Starting at $1 USD/mo after a free trial – cancel anytime
So you've seen a bunch of TensorFlow code by now, learned about how to build a layer in TensorFlow, how to do forward prop through a single layer in TensorFlow, and also learned about data in TensorFlow. Let's put it all together and talk about how to build a neural network in TensorFlow. This is also the last video on TensorFlow for this week, and in this video, you'll also learn about a different way of building a neural network that will be even a little bit simpler than what you've seen so far. So let's dive in. What you saw previously was, if you want to do forward prop, you initialize the data X, create layer 1 like so, then compute A1, then create layer 2, and compute A2. So this was an explicit way of carrying out forward prop one layer of computation at a time. It turns out that TensorFlow has a different way of implementing forward prop as well as learning. Let me show you a different way of building a neural network in TensorFlow, which is that, same as before, you're going to create layer 1 and create layer 2, but now, instead of you manually taking the data and passing it to layer 1 and then taking the activations from layer 1 and passing it to layer 2, we can instead tell TensorFlow that we would like it to take layer 1 and layer 2 and string them together to form a neural network. That's what the sequential function in TensorFlow does, which is it says, Dear TensorFlow, please create a neural network for me by sequentially stringing together these two layers that I just created. It turns out that with the sequential framework, TensorFlow can do a lot of work for you. Let's say you have a training set like this on the left. This is for the coffee example. You can then take the training data's inputs X and put them into a NumPy array. This here is a 4 by 2 matrix, and the target labels Y can then be written as follows. This is just a one-dimensional array of length 4. Y, this set of targets, can then be stored as a 1D array like this, 1 0 0 1, corresponding to the four training examples. It turns out that given the data X and Y stored in this matrix X and this array Y, if you want to train this neural network, all you need to do is call two functions. You need to call model.compile with some parameters. We'll talk more about this next week, so don't worry about it for now. Then you need to call model.fitXY, which tells TensorFlow to take this neural network that it created by sequentially string together layers 1 and 2, and to train it on the data X and Y. But we'll learn the details of how to do this next week. Then finally, how do you do inference on this neural network? How do you do forward prop? If you have a new example, say X new, which is NP array with these two features, then to carry out forward prop, instead of having to do it one layer at a time yourself, you just have to call model.predict on X new, and this will output the corresponding value of A2 for you, given this input value of X. Model.predict carries out forward propagation, or carries out inference for you using this neural network that you compiled using the sequential function. Now I want to take these three lines of code on top and just simplify it a little bit further, which is when coding TensorFlow, by convention, we don't explicitly assign the two layers to two variables, layer 1 and layer 2, as follows. But by convention, I would usually just write the code like this. What we say the model is a sequential model of a few layers strung together sequentially, where the first layer, layer 1, is a dense layer with three units and activation of sigmoid, and the second layer is a dense layer with one unit and again a sigmoid activation function. So if you look at others' TensorFlow code, you often see it look more like this, rather than having an explicit assignment to these layer 1 and layer 2 variables. And so that's it. This is pretty much the code you need in order to train, as well as do inference on, a neural network in TensorFlow, where again, we'll talk more about the training bits of this, the compile and the fit function, next week. Let's redo this for the digit classification example, as well. So previously, we had x is this input, layer 1 is the layer, a1 equals layer 1 applied to x, and so on, through layer 2 and layer 3, in order to try to classify a digit. With this new coding convention, with using TensorFlow's sequential function, you can instead specify what are layer 1, layer 2, layer 3, and tell TensorFlow to string the layers together for you into a neural network. And same as before, you can then store the data in a matrix and run the compile function and fit the model as follows. Again, more on this next week. And finally, to do inference or to make predictions, you can use model predict on x new. And similar to what you saw before, with the coffee classification network, by convention, instead of assigning layer 1, layer 2, layer 3, explicitly like this, we would more commonly just take these layers and put them directly into the sequential function, so you end up with this more compact code, where you just tell TensorFlow, create a model for me that sequentially strings together these three layers, and then the rest of the code works same as before. So that's how you would build a neural network in TensorFlow. Now I know that when you're learning about these techniques, sometimes someone may ask you to, hey, implement these five lines of code, and then you type five lines of code, and then someone says, congratulations, with just five lines of code, you've built this crazy complicated, state-of-the-art neural network. And sometimes that makes you wonder, what exactly did I do with just these five lines of code? One thing I want you to take away from the machine learning specialization is the ability to use cutting-edge libraries like TensorFlow to do your work efficiently. But I don't really want you to just call five lines of code and not really also know what the code is actually doing underneath the hood. So in the next video, I'd like to go back and share with you how you can implement from scratch by yourself, forward propagation in Python, so that you can understand the whole thing for yourself. In practice, most machine learning engineers don't actually implement forward propagation in Python that often. We just use libraries like TensorFlow and PyTorch. But because I want you to understand how these algorithms work yourself, so that if something goes wrong, you can think through for yourself what you might need to change, what's likely to work, what's less likely to work. Let's also go through what it would take for you to implement forward propagation from scratch. Because that way, even when you're calling a library and having it run efficiently and do great things in your application, I want you in the back of your mind to also have that deeper understanding of what your code is actually doing. So with that, let's go on to the next video.