 All right, so I'm an active open source contributor on GitHub to training AI projects like TensorFlow, Keras, some projects by Huggingface, IBM, the sort. I'm a writer on AI and machine learning data science on medium.com, and I've been working in the machine learning and AI field for the past five years at research institutions, local startups, doing my own side projects. So this is, I guess, what I've been doing for the past while. And I picked up TensorFlow ever since it's beta, when it's beta was still in development. I started learning TensorFlow and using it since then. So this is just a rundown of what we're gonna do today. I'll start out with some prerequisites, what exactly is needed, I guess, understand at a high level what I'll be talking about today. I'll give a quick refresher to machine learning in general in terms of the terminology I'll be using, some of the concepts that this talk will cover. And from then on, once we're comfortable with that, we'll move on into pure TensorFlow, what it is, who's using it, what can you do with it, what TensorFlow provides for the average developer slash researcher, and after that, we'll be doing a bit of live coding. So if you have your laptops at hand, you can follow along, we'll be covering two, I guess, exercises that kind of get you up and running with TensorFlow, the API, the syntax used in TensorFlow, and how you can build data pipelines all the way from, I guess, loading and pre-processing your data, all the way to building your models, training and testing it, and checking its performance metrics. And yeah, let's get started. So at a high level, to understand TensorFlow, you first need to understand what machine learning is. And to understand what machine learning is, you need to have a basic math background, linear algebra, calculus. So even to understand this talk, you need to have some bit of background in these two topics. And then basic understanding of machine learning concepts, neurons, layers, activation, neural networks in general, how they work. You probably also may need to know, like a fair bit on what a neural network is and what makes it work, or the underlying architecture, or the mechanism inside the neural network that makes it do what it's supposed to do. And of course, to even understand the TensorFlow syntax or the TensorFlow API, you need to be proficient in Python in terms of creating functions, working with classes, working with different variables and using packages, especially for numerical computing and visualization. So now I'll give a quick recap on machine learning in general. So I think we can spend a bit of time touching that before we deep dive into TensorFlow. So machine learning, it's a subset of artificial intelligence. It's where you give data to a computing system. It finds patterns in that data and it's able to predict off of unseen data. So if I have a stock market model, I just train it or train that model on historical stock data, stock price data. And given a few variables, I can get it to predict tomorrow's stock price data with a certain X amount of confidence or accuracy. So machine learning, the two major categories, supervised and unsupervised. Supervised means that your training dataset, it consists of both the feature space and the labels. So the features are something that describe the object. For example, if it's an apple, well, we can look at its color, its redness in terms of RGB values. They can be a feature. It's weight. It could be a feature. It's diameter. Anything that uniquely describes that object is considered a feature. And the label, which means like, yes, it's an apple. Yep, that's the label. So this could also work for other objects like an orange. Same thing. You'd want to take its color, its mass, its diameter with the class label of orange. So that's what supervised learning is. It's where you give all the data to the machine learning algorithm to find patterns in it. Semi-supervised, back then it was called unsupervised learning. So now semi-supervised learning is you just give the algorithm just the features. So the model or the algorithm finds its own patterns in the data either through clustering or some mechanism that it enables it to understand what's going on in the data, its distribution and things to describe that distribution. It figures all of it out by itself. And given data, given new data that it hasn't seen before, you can use this algorithm to predict off of it seeing which cluster it most probably belongs to. So that's semi-supervised. So neurons, you know, neurons, it's a terminology. It's, I guess, inspired by the human brain itself, the neurons in the human brain. So when you have a neuron, it takes in information from one side and it spits out something after doing something to its input. Similarly, a neuron in machine learning terms, it's a mathematical representation of what the biological neuron does. So it computes the equation of a line. So, you know, in high school, we learned that y equals mx plus c is the equation of a line, m being the gradient and c being the y-intercept. In machine learning, the gradient m is called the weight w, and the y-intercept c is called the bias b. And x, as you can see in the last point, it is the input. So what a neuron does is it takes the input x, it multiplies it, or it does a dot product. With its respective weight and adds a bias to it. And then it adds this thing called the non-linearity to it, which is the activation function. So when you have, well, a line which is linear in nature and you have complex data, you wanna learn some non-linear mapping between x and y. X is the input, y is the output. So you want some non-linear representation mapping the two. So when you add non-linearity as an activation function, it enables better learning. It allows the model to find better patterns in the data. And usually non-linear models, they're more accurate and they're much more versatile when you apply them to real life data. All right, so if you've, I guess if you're familiar with machine learning in general, you would have seen some of these layers. You see the fully connected layer, which is just a stack of neurons, one on top of each other. Convolutional layers, they take in an input image and they perform a convolution function on it. If you have an image, you can think of the convolutional kernel as this flashlight. I'm scanning the image, parts of the image one by one, row by row. And based on that, after convolving, I have, I guess a matrix that I can figure out the features from that matrix and infer what that object could be or what that image could be after performing a series of convolutional functions on it. Maximum pooling, it usually accompanies the convolutional layers. Max pooling, it lowers the dimensionality, makes it easier for the model to learn. So it, so when you have a convolutional layer applied to some input data and you have a max pool layer after that, it lowers the number of parameters in the network and makes it much more lighter. So parameters here refers to the weights and biases in the individual neurons. So if you apply max pooling to immediately after a convolution, it lowers the dimensionality, thus bringing down the parameter count, making the model faster if you want to put it into production. So flattened, it's a simple concept. If you have an n dimensional matrix, for example, I have a matrix, two by three matrix. And if I were to apply a flattened layer on it, it would give me a six by one vector because two times three is six. And it would just give me a single column or single row after you flatten it. So you can think of the flattened layer as, you know, I bring in, I bring in bread, put it in, and when it goes through the flattened layer, you just basically get like a disk at the end. So you can imagine it like that. So it returns a one by M or M by one vector. So when you take these different layers in the previous slide and you put them together in different combinations, you get this thing called the architecture, the neural network architecture. So the architecture, individual layers, they have the parameters, the weights and biases in the individual neurons. So an architecture is just a huge collection of all of them together. So in an architecture or a model, you take in inputs X and you map them through that entire neural network to the output Y. So when you, through the process of training, when you fine tune the values of W weights, W and biases, B in the network, when you train the parameters, the network is becoming, I guess, more adept at creating that representation between X and Y. So when you have, after fully training the network, when you have that final configuration of parameters, W and B in all the layers, all the neurons, you can call it a frozen model. And this is the final configuration. You can put that final model into production and use it to predict unseen data. So whatever I mentioned, neurons, layers, architecture, this slide kind of summarizes everything, brings it all together. So all the yellow dots, green dots and the purple dots, they're all the individual neurons. And here it's just a fully connected layer. All the four layers, they're fully connected, which means they're just a stack of neurons. It takes in inputs, you know, X one, two, three, four, or how many ever inputs you have, and it maps it to the output Y. So for example, if I were to take an image, supposing the layer one, L one, takes in an image and the network is supposed to predict which class it belongs to out of four. You know, is it Y one, two, three and four? You would build such a network that takes in that image, it does something in the middle and it spits out the classified value pertaining to that image. So if I gave it a picture of a car, it would tell me, okay, out of, for example, apple, orange, car and bus, it would fire that specific neuron saying, yes, this is a bus, sorry, yes, this is a car. So yeah, X to Y mapping, that's ultimately what a neural network is. So TensorFlow is a library built by Google AI. If I'm not wrong, it's beta version released somewhere back in 2015 and ever since then, it's one of the largest growing machine learning frameworks on the planet today. So you use TensorFlow in any one of these languages you're comfortable with, it has lots of support, so you can use it in any one of these languages to build neural networks. And you can train these neural networks in their specific languages. TensorFlow is even compatible with different kinds of hardware like GPUs, TPUs, and I'll cover that later. So TensorFlow, when you break it down, you get tensor and flow. Tenser, you can think of it as an n dimensional array and flow just means the movement or the flow of these n dimensional arrays through the neural network to get that final mapping or representation when you train it. So TensorFlow, it's widely used. It's used by a lot of major corporations, it's used by all of these. Even the major players in most industries, they use TensorFlow for different projects, different products. Research institutions, even ASTAR, were heavy on TensorFlow for performing some simulations when you're quickly crunching some numbers. TensorFlow is our go-to framework. And practitioners, hobbyists, even now, research students in even high school, university, educators, they're all using TensorFlow for their classes. And now, hopefully by the end of this, even you're able to use TensorFlow to a fair extent. So TensorFlow, while it has its theoretical usages, you can also use TensorFlow for all these sorts of things. So you have Twitter, Facebook, wait, no, Facebook uses PyTorch, the different thing. So you have Twitter, you have all these companies like Airbnb, they're recommending products to you. Singapore Airlines, they recommend flights to you. So they're all building recommendation systems using TensorFlow. Even social media, like LinkedIn, they are heavy on TensorFlow when bringing people together or connecting them or recommending who you should connect with next. Fraud and fault detection when you have a camera and you're focusing on something and you detect a crack in, for example, a piece of metal. You can use TensorFlow for that. Now Twitter is even using, it has a new fake news detection algorithm. So whenever you come across a piece of external news, it'll actually tell you this may be misleading or this news, like this news piece may have been manipulated. So please verify. So open source software, we have companies like Huggingface, all of them using TensorFlow to build their open source projects, putting it into the hands of developers around the world. And again, research applications, companies like OpenAI, DeepMind, Google's DeepMind, they're using it for self-driving car research, computer vision and natural language processing research. So I guess just a humor all of you. A South Korean farmer, his son was a software engineer. He used TensorFlow to create a Cucumber classification model. So in like the farm, whenever Cucumbers from the farm, they were bought into the warehouse. The classifier would predict what class of Cucumbers it would belong to. And there would be like the sweeper thing that kind of sweeps it into its specific bucket. So yeah, even farms, AgriTech, TensorFlow is big in that. So yeah, TensorFlow, it's usage is, it's really just constrained by your imagination, what you want to use it for. So TensorFlow, while it has its merits, we, why specifically do companies use TensorFlow? Why does a single developer use TensorFlow? TensorFlow is kind of like this huge ecosystem of many different products that enable you to do different things. So on one hand, it lets you build simple data pipelines. It lets you ingest data, load, pre-process, all of them. It lets you build and reuse neural network models. It lets you take those train models when you freeze their weights and you kind of put them into production. You can use TensorFlow for that. And you can even visualize your training process using some dashboard, using a dashboard that TensorFlow provides. It's fully open source and you can do all of this without paying a single dollar. And TensorFlow even allows you to train on specialized hardware that Google built themselves. So the entire ecosystem and the infrastructure is provided by the Google TensorFlow team so that you can do anything you want with it. So if you're here for the talk or you're tuning in online, I'm guessing that you are here for one of these purposes. You want to learn TensorFlow for projects. You want to apply it at work or you just want to learn something new and exciting that's highly in demand now it is. Or you're a student educator wanting to pick up this for your school projects. So by the end of this, I want to assure you that this workshop will be covering a bit of basic TensorFlow and that should get you up and running for whatever you want to do beyond this. All right, so the TensorFlow API, it's really simple to learn. For me, it took a few weeks to get started with it. There are some highly complicated machine learning frameworks out there that require even more time and expertise to get used to the syntax. But TensorFlow, it's really Pythonic. So if you're familiar with Python, you can easily get started with TensorFlow and you can hit the ground running. It's as simple as that. And neural networks, all that math and that complexity, underlying complexity, TensorFlow abstracts all of that away such that you can build your entire data pipeline, loading your data set, training your model, checking performance metrics and deploying that well within 20 to 30 lines of code. And when you compare that to the number of lines of code that organizations or corporations, 20 to 30 lines is nothing. So TensorFlow allows you to do all that really easily. And TensorFlow, it's fully customizable while they also provide some basic functions for, I guess, beginners to use. You can also customize it for your own personal use case. You can create custom layers. You can create custom input pipelines. You can do all of that for both beginners and advanced users with TensorFlow. So what TensorFlow does is when you're creating that neural network, when you're specifying each layer one by one, what it's doing in, I guess, the background at a low level is it's creating this thing called the computation graph. The computation graph is simply an organized view of all the processes occurring in your entire pipeline. So the computation graph, it consists of nodes and edges. Each node, well, it pertains to a specific operation or data value that is being given to it. And an edge is basically that flow of data or that operation being performed on that variable. So for example, say we have this multivariate function fxy equals y plus 2 plus x square y. So if we were to break this down or we give this to something like TensorFlow, it constructs a computation graph with this equation. So to help you originalize this, let's imagine that same fxy function. And we want to build the computation graph for it. So we take y and the constant 2. And we take x. And we get x square by multiplying x with itself. The yellow circle that you see there, it represents the multiplication operation. And the squares that you see, it represents the data value that's going in, either a variable or a constant, like 2. So now that we have all the basic building blocks, the variables x square, y, and 2, we want to all bring them together to form that fxy equation. So now when you have x square, y, and 2, you can multiply x square and y to get x square y. At the same time, you can take the same value of y and add it to 2 to get y plus 2. And finally, the last operation in this computation graph, to top it all off, it adds both these things together. The y plus 2, the y plus 2 part of it and the x square y part of it, it adds both of them together to form that final equation, fxy, or that expression. So at a high level, this is what a computation graph is. We have a really complicated, I guess, equation. We break it up into its individual steps to simplify the problem, while also ensuring that we finally get to that equation at the end or that process. We finish it in a very organized method. So TensorFlow, back when it was in its beta development, there's an engineer, his name is François Chalet. Yeah, my bad, I forgot. Yeah, François Chalet, follow him on Twitter. You should do, he's really active and he gives lots of good advice when it comes to machine learning. So he created this thing called Keras. Keras is, so if you think high, TensorFlow is at this level of abstraction at taking away all the ugly math underneath, Keras was in like a layer above that. So it was even more, I guess, easier to use Keras than it was to use TensorFlow back then. So in TensorFlow beta, when you were, I guess, initialize or create a neural network, you had to also initialize the individual weights and biases or the parameters manually. So you actually had to write W equals, B equals for all the layers that you had in your network. But Keras, and naturally this took 50 plus lines of code. And for anyone who didn't have that kind of time or for beginners trying to, I guess, quickly prototype, this was, I guess, still too complicated for them. So to remove all that, to push all that aside, François, he created Keras. So you could do this same thing. You no longer had to initialize your weights, biases, parameters, hyperparameters. You no longer had to initialize any of that. All you had to do was call a specific function and it would do all of that. And your entire pipeline, whatever took you that 50 lines of code in TensorFlow, you could do the same thing in less than 20 lines of code using Keras. So since then, as the Keras community, as the Keras project and the TensorFlow project, as they've grown the couple of years, Keras is now part of TensorFlow. So using the TF or TensorFlow.Keras package, you can get access to those functions available in Keras that abstract away everything. So now almost all the models that you see on GitHub, open source, all the tutorials you use, even TensorFlow version 2.0, it's fully based on the Keras spec, meaning you no longer have to do these low level operations to get your job done. You can use the high level TF Keras library and do the same thing. So TensorFlow Keras, it has this thing called the layers API. So what the layers API does is it provides a bunch of different functions or different classes that represent all these different layers. So earlier in the slides, we mentioned the fully connected layer, the convolutional layer, max pooling layer. So to actually implement that in raw NumPy, if you've heard of that, the numerical computing library in Python, if you were to use pure NumPy to write all of that, it would be a mess. But in TF Keras dot layers, all you have to call a fully connected layer, you just need to write the word dense. So dense is just another name for fully connected. So all you need to do is type in dense and pass in a bit of parameters and you're good to go. You no longer have to initialize the weights, the parameters or the hyperparameters. Or if you wanted to initialize a convolutional layer, all you have to do is type in conf2d and it gets the job done. So the layers API, so all these different layers, if you were to bring them together to create the neural network architecture, you have to use this thing called a sequential API. So all of this is part of TF Keras. So sequential, you can think of it as like a bucket and all your layers are, I guess, the individual objects that you put in your bucket in an organized way. So now that I have my container, I put in all my layer objects one on top of each other, I stack them up to create my neural network. And with less than seven lines of code, if you're creating a simple neural network, you have an architecture that you can train or do whatever you want with it. And each of these layers, data automatically flows between them, so you no longer have to specify connections between the layers, like you had to do in the original version of TensorFlow. So yeah, TensorFlow ever since its debut in early 2015, all the way till now, the ecosystem has grown so much. It has a lot of, I guess, subsystems or submodules that the ecosystem has provided to make all your machine learning needs much easier to acquire or achieve. So now that we've done like a quick recap of the past, I guess, yeah, four to five years of TensorFlow and the contribution it has made to the machine learning community, I guess it's time to finally do a bit of live coding. So if you have your laptops, please ensure that it's connected to the Wi-Fi. If not, are you connected to the Wi-Fi? Wi-Fi? Okay, yeah. So yeah, we'll be doing a bit of live coding. So who's heard of Google Colab? Raise your hand if you've heard of Colab. Okay, yeah. So Colab, those of you who haven't heard of it, it's an online notebook. It's similar to Drupter, but it's hosted on the web. So in the case that you wanna quickly experiment with things, you can quickly hop onto a Google Colab notebook and immediately get started with coding. So the advantages of using Colab compared to having an IDE or having your specialized editor or setup environment is that Colab, it comes pre-packaged with all the major data science and machine learning packages. It comes with TensorFlow, TensorBoard, NumPy, SciPy, Matplotlib. It has support for all of them, so you no longer have to install all of them because there are some issues that people have faced that they're trying to install a package for doing machine learning projects and it just fails, the installation fails. So to do away with all of that, you get this containerized notebook in your hand that you can do the same thing with. So Colab, an additional benefit is if you do not want to spend thousands of dollars on a custom machine learning rig, you can use the hosted virtual machines given by Colab. You have your basic CPUs, you have the Tesla GPUs and you even have access to the Google Brain Teams custom hardware called the Tensor Processing Unit or the TPU. You can get access to all of that free of cost for 12 hours using Colab. But if you want, I guess, opt in for better virtual machines or you want more training time, up to like a day, 24 hours, you can sign up for the pro version, it's $10 per month and you have even more support for all your machine learning projects. And creating a Google Colab notebook is as simple as creating a Google Doc. So for that, we will be visiting Google Drive. So if all of you can visit drive.google.com on your browsers right now. Hold on, let me zoom in. Yeah, and yeah. So once you're in Google Drive, you can log in with your Gmail account or your Google account and you can click the new button and when you see more, you can, in the dropdown menu, you can select Google Colab. When you click it, it redirects you to another page which is a fresh notebook for you. So yeah, it's as simple as creating a Google Doc. Is there, does anyone not see Google Colab in their, in that dropdown menu? Everyone's on Colab? Oh, okay, okay, yeah. Okay, yeah, so, yeah, ignore the, yeah. Oh, so can you access Colab? Have you accessed Colab? Okay, okay. Sir, have you accessed, okay, good. So you see something like this, right? Okay, yeah. So I'll give you like a quick run through of Colab. Oh, yeah, so this is like a custom thing, you know, while I'm at it, you know, I can show you. You know, just for, I guess, the banter, you can go to tools, settings, miscellaneous and you can select one of these funky little animations that's gonna start appearing. I will disable this option because it's pretty distracting, but you know, feel free to enable that. All right, so this is like a quick run through for Colab. So this, I guess, black rectangle you see, it's called a cell. But before we even start coding, there's this button called connect. Yeah, there's this button called connect. Can you try connecting? Yeah, so when you click connect, it's gonna say it's, you know, allocating the VM. And after that, it's gonna show some basic, basic metrics like RAM usage. Yeah, it should say connected and it should show you, yeah, something like that. Is everyone connected? Okay, good. Yeah, so that means that our notebook right now, it's now sitting on top of that VM, we can finally start coding. So yeah, so this black rectangle that you see here with display button, it's called a cell. And whatever you type in this cell, you know, you can type, so Colab here, it's a Python notebook, so you can only code in Python here. So if I can type in something like, you know, X equals five, simple Python, and if I do Y equals five, and I do X plus Y, and if I were to click this play button, it's gonna run the Python specifically in that cell. And if you want, you can create more cells and you can do even more with that. Okay, so notice how in my second cell, I do not have, or I haven't initialized the variable X, I only did it in the top cell. Though I guess the best thing about notebooks is that for every cell you create, it has a memory log of all the past variables you declared or all the past actions that you've done in the cells above. So you don't need to keep re-initializing variables. So over here, I only initialize the variable Z, and when I add X plus Z, it takes the value of X from the previous cell and adds it to the value of Z that I initialized here. So there's this button called code. So when you click code, yeah. But also if you hover over the bottom of like a pre-existing cell, you get this thing called code and text. You can click code. And if you want to create notebooks that you want to publicly release for I guess your own notes, or you want to create like a tutorial using a Colab notebook, you can click the text button. And this, it's just a bunch of basic text. And it appears over here. And for example, this text, I could say this is like a description of what I'll be doing in the cell below. And yeah, comments. But at the same time, you can write some good descriptions of what you're going to do in the cell or like the entire project. So yeah, this is Colab in a nutshell. If you are using Mac OS and you want a shortcut, you don't want to keep pressing this run button or display button, you can do command enter and it'll run the cell. If you're on Windows, it's control enter or shift enter. You can try one of them. It should run the specific cell. Yeah. So it should also be this, it depends on the keyboard you have. If you have a command key, then it's command enter. If you have a control key, it's control enter or shift enter. You can try one of them. Is it running? All right. So now that we have created, so yeah, to delete a cell, you can hover over the cell. There's this trash icon you can click. It'll delete the cell. All right, shift enter, shift enter. Okay. All right. Now that we are here, yep. So we've done like a quick tour of Colab. So now, now we're actually gonna be using TensorFlow from scratch. So before we begin building models with TensorFlow, we first need a data set to work with. So TensorFlow data sets are, when you write it in code, TFDS. It comes with a whole bunch of data sets. You know, over 150 data sets that, you know, it's in different categories, like text, text, audio, video, summarization tasks. You can have access to all of that using TFDS or TensorFlow data sets. So I guess when we go back to the notebook, we can import TensorFlow, of course, but before that we can import the numerical computing library NumPy. And then we can import TensorFlow as TF. So we have the as TF or as NP, it's just a shorthand notation in Python. So you don't need to like type in TensorFlow, you know, every time you type, every time you want to use something from it, you can say TF dot and it'll mean the same thing. So yeah, so we have import TensorFlow as TF and now finally let's import, let me just increase this, okay. We can import TensorFlow data sets as TFDS. So in Colab, it also has this autocomplete feature as you would have seen, you know, when I'm typing, there's a dropdown menu of all the different options that the Colab environment provides. So when you're, as you're typing, you can tab complete that specific line. Okay, so now we can run this cell. Actually, before this, the current version of Colab, it's, wait, let me check the TensorFlow version. So yeah, if you type TF dot version and you run that specific cell, if you see 1.15, that's not the version we're using. Can you try typing TF dot version, see what version your notebook is on? Because if it's all on 1.15, we'll have to convert it to the 2.x because that's the most recent configuration or release of Python, the stable release. Do you see something like 1.15? Okay, yeah, so we're not gonna be using 1.15, 1.15 is the, I guess, the older version. The most recent version is 2.21, yeah. So to upgrade TensorFlow in Colab, we'll have to, you know, re, we have to rerun the, we need to restart the VM by, you know, by invoking the 2.x package. So if you want to use TensorFlow 2.x, you need to type the percentage sign, TensorFlow underscore version space 2.x. Oh yeah, when you hover above, so if I have a cell below and I hover on top, there'll be a code button that comes up. If you click it, it'll create a cell above. Or you can create, alternatively you can create another code cell here and you can see this arrow button on the top and it lets you move your cell up and down. Like the arrows here, like I can move the cell up and down, yeah. Any issues so far? Oh, the arrows don't work. Oh, the up one, it's only grayed out if it's at like the top of the notebook. Is everything okay on the set? Yeah, percentage TensorFlow underscore version space 2.x. Yeah, if you want to use the most recent version of TensorFlow because the collab notebooks up until now, they haven't been updated to 2.x. All of them, they're still running 1.15. So if you really want to use like the latest version, you need to specify it on your own at the top. So now that we've kind of found out that we're using the wrong version of TensorFlow, we need to restart this version machine. So if you go to this runtime option at the top and you click restart and run all, it'll give you this popup. When you click yes, it's reconnecting, reallocating space. It's booting up that VM. And when it starts running, finally, it says that 2.x selected. Yeah, everyone good? Okay, on the set? Okay. All right, so now that we are using 2.x, we have imported all of this. We can go back to the slides. All right, so now that we have imported Numpy, the numerical computing library, TensorFlow, which is what we're gonna use for the majority of the neural network construction and the pipeline, and TFDS, TensorFlow data sets, the next thing we'd want to move on in terms of the concepts is TensorFlow models. So to build a model or to build a neural network, as I mentioned earlier, you need to use the sequential API. So sequential, you can think of it as a container. And one by one, you put your layers one on top of each other to create the entire model or neural network. So when Keras was finally under TensorFlow, the models submodule, it came with automatic support for training, testing. It came with support for your loss functions, optimizers. So you no longer have to write all of that from scratch. So using your TensorFlow data sets, TFDS data sets, you can train them, you can train the TensorFlow model on these data sets. So when you have a model, it's basically, it consists of layers. So tf.keras.layers, the layers API module, it comes with all these, some of these common layers, input for taking in an input, taking in that X value, dense, which is basically a synonym for a fully connected layer, activation, which you either have an option to type in the activation function in the dense block, or you can specify a new layer called the activation layer and type in that specific function there. We'll see more of that later. So com2d, it's your 2D convolution layer. Max pool 2D is your maximum pooling in two-dimensional space. Flatten is that thing that takes in an n-dimensional matrix and squashes it into a vector. And reshape is especially a, so when your input layer takes in an n-dimensional matrix, your reshape layer, well, it's pretty self-explanatory, it reshapes that incoming vector into a target shape. So if I have a 28 by 28 image going in, but I only want a 780, so 28 by 28 is 784 when you multiply them. So I have a 28 by 28 pixel image going into my pipeline, right, using the input layer. So immediately when you put a reshape layer under that, and you type in 7084, it'll take that 28 by 28 and squash it. So in a way, you can think of your reshape layer as kind of like a sub, as a subcategory of flatten, because even a flatten takes in something and changes its shape. So yeah, when you're building some basic neural networks, these are the common layers that are used. There are much more. So instead of max pool, you can use your global average pooling layers. You also have 1D convolutions, 3D convolutions, and you have a bunch of other layers, like your attention layer, but we're not gonna be covering that keep this tutorial simple. So as I guess to get ourselves comfortable with the TensorFlow syntax, the TFDS API, we'll be training on this dataset called MNIST. Who's out of MNIST? So MNIST is this 28 by 28 image hand-drawn-digits dataset. So it has hand-drawn digits from zero all the way to nine. So you can see an example of both of them. They're all 28 pixels, 28 by 28 pixels. That's an eight and that's a zero. So since they're grayscale images, you can flip the color map. So you can either present it as the digit is black and the background is white or the other way around, in the case of zero. So this is what we're gonna do now. We're gonna take in this entire MNIST dataset. We're gonna build a really simple neural network to train on this dataset and classify hand-drawn digits. All right. Okay, so now that we have that, I think we're gonna take a seat here. Okay, so now that we imported the numerical computing library, TensorFlow and TensorFlow datasets, we finally wanna go ahead and load our dataset. So I'm just gonna call this dataset train or dstrain as tfds.load and I just need to type in MNIST over here. So earlier I mentioned that TensorFlow datasets has a, I guess, a repository or a collection of like various datasets that are at your disposal. All you need to do is type in the name, the specific name of the dataset and you have it in your hands. So tfds.load, it takes in the entire dataset and it, I guess, dumps it into the dstrain variable. But here, since we're only creating the training set, we can specify the split that we want. So here, the split refers to which part of the dataset we want. Do we want the training set or do we want the testing set? So here we can specify the split to be train. So this just gives, so MNIST, the MNIST dataset, yeah, so the MNIST dataset, it consists of 60,000 training images and 10,000 testing images. So we only want that first 60,000 images in the dataset. So that's why we specify the train split. So I also mentioned that supervised learning is a branch of machine learning or category of machine learning where you give the model both the features and the labels. So as supervised as basically a Boolean, you just need to give, you need to say true and it'll give you both the features which is basically the 28 by 28 pixels and it'll also give you the class label. Does it, you know, zero to nine which category it belongs to. So now that we have this, we can finally run this cell. Let me just minimize this, okay, yeah. So what it's gonna do is it'll download the entire dataset from Google Cloud Storage. So there's a publicly available or accessible URL. So it pulls the entire dataset or the training dataset from that URL. So now that we have the training dataset in our hands, let's visualize what the individual instances look like. So we can say, you know, let's iterate through, I guess, four examples and let's visualize them. But before we iterate, we want to import matplotlib which is the visualization library. So in my auto complete menu, matplotlib.pyplot as PLT, when you, it'll auto complete this. So PLT is the visualization package. It lets you draw graphs, histograms, figures, sketches, anything, images, anything. So now that we have PLT, we can finally iterate through the training dataset, dstrain. So dstrain is, so the class it belongs to is the dataset builder class in the TensorFlow ecosystem or the TensorFlow library. So it comes with this specific function called take. So from my entire 60,000 image dataset, I want to take a part of it and you need to specify n, which is how many you want to take. So say out of six, 60,000, I want to take the first three. So I type in dstrain.take3, right? So now we're going to iterate through the first three training instances. So image.label, sorry, image comma label, it's going to be stored in this example variable over here. Yeah, in this example variable over here. So example is just an array with two elements. Example at the first, at the zeroth index is the image. And example in the first index is your label, as in like, you know, zero to nine, which one it belongs to. So TensorFlow, it has its own custom data type called a tensor. And you can't access tensors on its own. You need to take its numpy representation for both of them. So at the end of example at the zero index, we can type in dot numpy for both of them. And this gives us the numpy version or the numpy representation of the image and the label. And matplotlib or PLT, it's only able to visualize numpy arrays, not tensors. So that's why that's another reason why we want to convert it. So now that we have loaded a single image and label from that example instance, we can finally visualize it. Yeah, so we can say plt.imshow image and we can even add a title to each visualization as the label. So what you'll see here pretty quickly is that when you create a plot, whatever it represents, you can add a title to that. You can even label the x and y axes. So now that we have this, let me zoom out for a bit, you, when you run this cell, it'll give you an error which is 28 by 28 comma one is an invalid shape. So plt, it only takes in two dimensional images and since MNIST is a grayscale image, it should only have three channels. So I'm sure all of you are familiar with the RGB concept. So grayscale images, they only have two dimensions while RGB images, colored images, they have three dimensions, one for each RGB channel, which is why this image, we will have to use the numpy operator dot reshape. So let me zoom back in. So when we say plt.imshow image, we want to add the dot reshape, such that we want to reshape it to 28 by 28. Everyone up to speed? Okay. Or you just need to click the cell that you, oh yeah, so when you're hosted on the CPU runtime, it uses your local CPU. So if your device is slow naturally, Colab may also take a while to run each cell or perform operations. We want to, okay, so originally the numpy representation from that tensor that we get, it's 28 by 28 by one, but plt only takes in two dimensional images. This one is three dimensional. It has one, two, and three dimensions. But over here, we just want the first two dimensions. And since that last one is just one, we can completely cut it off without affecting anything. 28 by 28, yeah. So yeah, when you hover over the reshape, the word reshape, it says that it's a built in method in from numpy. So all these tensors, when you get their numpy representation, anything you can do with numpy, you can do it on them. So yeah, when you run this cell, yeah, it finally gives you this. Does everyone see digits? Yeah, so four is the first training instance in the MNIST dataset. So when you zoom into this, we can see the title. Over here, we wanted to also add the title for each plot. Over here, you can see that for each image, it has its corresponding label, which is the title. Okay, so now that we have our training dataset, we wanna pre-process it such that we can finally feed it into the network that we'll be building soon. So for that, TFDS, TensorFlow datasets, comes with many pre-processing, built-in pre-processing methods and functions that all you need to do is call the function and call that method, and it'll perform that step or that operation on your dataset. So now what we're gonna do is this raw DS train dataset that we have, we want to batch it so that we can train the model in batches, and we also want to shuffle the dataset so that the dataset isn't really predictable. And also we wanna fit the entire thing into the memory of our hosted virtual machine. So we're gonna do all that in this cell now. So DS train, but before that, we first need to normalize the dataset. So normalization, it basically means when your dataset, all the individual features are in this case, all the different pixels, they're gonna be in the zero to two-five-five range, because in the RGB channels, it ranges from zero to two-five-five. Two-five-five being the highest intensity for that channel, zero being the minimum. So here we want to normalize it into a range between zero and one, because if you have, in machine learning or when you're building or training models, when you have too large a number, there's a chance that the dataset will overflow because the numbers are becoming too huge. So the more that you work with decimal numbers, really small numbers, the better. So normalize, it takes in the images or the pixel values, and it also takes in the labels. So what we want to do is first, we want to return back the images, but we want to divide each pixel by two-five-five. But two-five-five, it's an integer value, but the images, they are in another data type representation. So they are in the Uint8 data type representation. We want to convert them into Float32. So to do that, we can say tf.cast. So when you have an integer and you want its string representation, you kind of cast it writing str brackets and that integer inside. It'll give you the same button string format. Similarly, we want to cast the Uint8 representation and we want to change all of that into Float32. So we can say we want to cast all the images that's given to this function into the tf.float32 data type representation. And now that we have converted it into a format that we can work with, we can finally divide it by two-five-five. So when you take a large number, or a number between zero and two-five-five, and you divide it by two-five-five, you squash it into the range zero to one. So here we have casted the images to Float32 and we can now divide by two-five-five and we can also return the labels. And just run the cell. So notice here that when you type, when you write in a function in a colab cell, it doesn't run the function because you never called it. It just exists. So now let's preprocess the dstrn data set. So dstrn equals dstrn.map. So what .map does is it takes in a mapping function and it applies that function to every single instance that exists within the data set. So for all the images, we want to apply this normalized function that we wrote above and we want to apply it to all the images. And at the same time, we want to do it in such a way that it's all inside the memory of your machine. So we want to type in this thing called, this parameter called the num parallel calls. As you're typing, it should autocomplete. Yeah, and you should get this, num underscore parallel underscore calls. So we had to type in this thing called tf.data.experimental.autotune. So this entire thing, it seems like a mouthful, but all it's doing is it's taking that entire huge data set and it's fitting it into your machine so that you don't get any out of memory errors. So all it's doing is it's caching it in your machine so you don't run into any errors downstream. So now that we have normalized the data set, we now want to batch up the data set. So we can say ds train equals ds train dot batch. So all the 60,000 images that are in the data set now, it's gonna convert it into batches of 32. Yeah, so it converts it into blocks of 32 images. So there have been many machine learning papers that have come out a while back on why batching your data set helps the neural network train better. It's, I guess you could call it like a good practice to do. So whenever you are, instead of training on the entire data set, you train on batches of it so that the model can generalize well. So now that we have batched up the data set, we now want to shuffle it so as to make it a bit more random, something that the network can't really, it's not predictable basically. So you keep giving it random instances to train on. So for that, we wanna type in ds train dot shuffle and inside this shuffle function, let me zoom in, yeah, inside this shuffle function, we want to type in the size of the data set. So we type in 60,000 here because we have 60,000 images in the data set. But if you're using this on a data set with 10,000 images, you want to type in 10,000 instead of 60,000. So now what this shuffle function does is it randomly samples from your data set and keeps giving random training instances for the model. So now again, we want to do this in a very memory efficient manner. So we type in ds train dot prefetch. So prefetch is another TensorFlow data set method that when you apply it to your data set, it loads it into memory really efficiently. And we want to use the same strategy that we used up here. So over here, we use the auto-tune. So it does everything automatically. We don't need to do, we don't need to put in any more effort into loading and processing the data set in memory. So in the prefetch method, we again, we can type in tf.data.experimental.autotune. Again, your auto-complete, if it's enabled on your collab notebook, it should auto-complete this. So has everyone reached until this prefetch point? Just give me a thumbs up when you're finished typing. Yeah, we can run this code and we have, we've completed the processing our training data set. And now we have to do the same thing for our testing data set. And we have to do the same thing for our testing data set. And to do that, we can create a new cell. We can say DS test. By the way, if you ever run into any errors, just raise your hand or you just call out. Because in TensorFlow, many errors are usually caused by internal virtual machine errors. Like it's not your fault, it just happens. So yeah, if you run the cell again, it should clear up. Unless it's like a typo or something, which means you need to cross-verify with the code on the screen. So yeah, now that we have finished pre-processing our training data set, we have to do the same thing for our testing data set. And it's a similar process. TFDS.load, again, we want the MNIST data set. But this time, for the split, we want the testing data set. We want the testing data set. We need the test data set. Again, we need it as supervised, as true. So it's going to give us everything. It's going to give us both the features and the labels. So now that we have loaded in our testing data set, we can just go ahead and run this cell. It shouldn't give any errors. And now we can finally start pre-processing our testing set. So it's a similar process. Actually, we can take this cell, copy everything, and paste it down here. And instead of dstrain, we have to change it to dstest everywhere. And so this dstest.shuffle, we can completely remove that line because we don't really need to shuffle our testing data set because it already contains instances the model has not seen before. But yes, we do want to batch it because it's working in batches much faster than processing the entire data set at a shot. Again, you see over here that we're using the map function or the map method provided by TensorFlow data sets. And we apply the normalized function on all the instances. And when you run the cell, it shouldn't give you any errors. Just give me a thumbs up when you've finished the prefetch function. OK, so for the online viewers, the question is, why aren't we shuffling the testing data set? We're not shuffling it. First, let me explain why we want to shuffle our training data set. We shuffle it because we want to be fully random. We want to keep giving it random instances when training so that it doesn't, I guess, find patterns in the data set itself saying that I'm going to get this instance next for training, which is why we shuffle it. So similarly, so if I have a deck of cards and I want to, when I'm playing a game of cards, I shuffle the deck first before giving it out to each of the players. So that way, there's a fully random chance of getting that card. Instead, there's no chance of, I guess, cheating, or in this case, the model cheating, or learning something that it's not supposed to learn. But for testing, we don't really need to shuffle it because the testing data set, it contains the training instances, the images that the model has never seen before. So yeah, that's why we don't need to shuffle it. It's already random enough. Yeah, you can remove shuffle from the testing data set, but you want to keep it for the training data set to keep the training fair. Yeah, just run the code. It shouldn't give you any errors. OK, good. OK, so now that we have loaded in and pre-processed our training and testing data sets, we can finally start building the model. So for this, the tf.caras.layers library, it abstracts away all the math and all the complicated theory that you need to know when you're creating neural networks. You no longer have to initialize your individual neurons. You don't need to instantiate your parameters or hyperparameters. All of it is done in the background. So now let's go ahead and import some more, import a model of the sequential API. And let's go ahead and download some, sorry, call some of the layers that I mentioned. So in this specific MNIST example, we'll be using the input layer. We'll be using the reshape layer and the dense layer and the activation layer. So these four, we can quickly build a neural network in less than 10 lines of code. So to use it, we have to say from TensorFlow.caras.models, import sequential, yeah, it should autocomplete. Yeah, so sequential, again, just to recap sequential, you can think of it as a cardboard box and everything that we're putting inside is basically, I guess, a food packet. So yeah, it's a container that contains the individual layers connected together in the background. So now that we have sequential, we wanna go ahead and import our layers. So for that, from TensorFlow.caras.layers, so this is the layers API that was, that was shown in the slides earlier. So from here, we want to import the input layer. We wanna import reshape. We wanna import dense, which is basically another name for a fully connected layer. And we want to import the activation layer. So here, earlier I mentioned that you don't really need the activation layer. There's a parameter in your dense layer called activation that you can just type in. But for the sake of visualizing the computation graph that we're building, I wanted to add in the activation function for, I guess, ease of understanding as to how TensorFlow goes about building that graph. So let's run the cell, again, shouldn't give any errors. So we can say model equals sequential. So here we're initializing our container, our box that we're gonna dump all the layers into. So yeah, model equals sequential. And to add an individual layer into this box or sequential container, all we have to do is type model.add. It's as simple as that. You just need to go on adding different layers one after the other. So first we're gonna add in my input layer to take in, well, the different, the X values or the pixel values that I'm gonna be feeding it. So of course, input, it needs your target shape. And over here, we notice like we got an error up above here saying that the image that we received, it's 28 by 28 by one. So similarly here, we want to type in the dimensions or the dimensions or size of one single image. So MNIST images, it consists of 28 by 28 images. So yeah, it's gonna take in that matrix over here. Next, we'll be using the reshape layer to take in this 28 by 28 by one matrix. And we're gonna squash everything down into a 784, yeah. So all it does is it takes that square and it just gives you a single row when you reshape it into 784. So this comma I added because, so if you can see here, I have this extra comma with nothing here. Yeah, because when we work with this model, it's kind of a way that we write it in NumPy to create vectors. Otherwise, it's just gonna create another list. We don't want a list, we want a NumPy array. So if you add this comma, it's so much easier to work with NumPy arrays without having to work with really complicated dimensionalities and whatnot. So next, after we do that, we can finally go ahead and start building our neural network. Or our feedforward neural network. So we type in dense, which is just another name for fully connected. And as I mentioned, a fully connected layer is just a stack of neurons. So over here, as the first wearable, we wanna type in the number of neurons in this layer. So we can go ahead and I can type something like 256. So in usually the best practice is working with the neurons is initializing it based on multiples of 16. So 16, 32, 64, 128, 256, 512, 1024, yeah, the usual. It's kind of like a best practice to use one of those values as your neuron, as number of neurons, yeah? It's best practice, like based on experiments, they found out that using that as the number of neurons in a layer, it usually gives the best generalization over dataset. All right, so next after we add our dense layer or fully connected layer, we wanna add our activation layer. And activation over here, we can use something like the value or rectified linear unit activation function. As I mentioned earlier, I'm not really getting into the math or like the individual concepts. You can change this activation function to be anything. It could be the sigmoid activation function. It could be softmax, but usually softmax is reserved for the final output layer because it gives a probability distribution pertaining to which class something would most likely belong to. So again, we wanna add another fully connected layer. So we type in dense again. So over here, when you're going for neural networks, you want to have kind of like a decreasing or tapering number of units all converging to like the number of classes that you have. So over here in this specific MNIST case, we have 10 output classes corresponding to zero till nine. So because of that, we want our final layer to have 10 neurons. So each one corresponding to zero to nine. So 256, next up we have 128. We wanna taper it down. And similarly, we want to have a ReLU activation function on it. And actually we can, you could just copy. We can just copy this block and just paste it down. So let me just add a bit of spaces so you can see. Yeah. So we have, this is a three layer neural network. It consists of three fully connected areas, all of them having their own activation function. So here we can finally put something like 10. And yeah, sorry. Oh yeah, it's 10 now. So depending on what a depth you want your network to have, you know, we can, so in this case, this is a three layer neural network. Suppose I want a five layer neural network, all I need to do is add in two more dense layers or two more fully connected layers each with their activation functions. But when it comes to the output layer, we want to have the soft max activation function because it gives us a final probability distribution pertaining to which class an instance belongs to. So out of all these 10 neurons, it's gonna, so the index that it fires the most at or it activates at, it corresponds to the class it belongs to. So for example, if the third neuron fires, that means that it belongs to the number three or that digit, that handwritten digit, it corresponds to number three. But for example, if the eighth neuron has the highest probability in this distribution, it means that the number in the image is probably an eight. So yeah, we have this. So now we want to do this thing called compiling a model. So compiling a model is this Keras term that you use where you provide the loss function and the optimizer that you're gonna use to train the network. So over here for the loss, we're gonna be using a sparse categorical cross entropy. So all it does is it takes all my predictions, it compares it to all the actual labels and it computes a loss based on that. But additionally, you can also use the mean squared error loss or the mean absolute error or any activation function you can think of. So in the official TensorFlow documentation, there's an entire list of all the loss functions that you can use. So yeah, you can search that up based on if you want to explore. So over here, we want the sparse categorical cross entropy. So this could be anything. I'm just choosing sparse categorical cross entropy as my loss function, but feel free to choose anything. But based on your loss function, it also affects the training. Different loss functions are known to have different, I guess, training results. So for our optimizer, we want to choose the atom optimizer. So atom stands for adaptive momentum. I don't want to get down to the math, but it's one of the best optimizers out there today. So metrics is this thing that all neural networks kind of have. So when you have, so these metrics are basically performance metrics. So over here, we want to maintain the accuracy or we want to check the accuracy of the network as it's training. So later, you'll see when we're actually training on the dataset, when we're actually training on the dataset, the accuracy is also printed side by side. So if all goes well, you should see an increasing accuracy. So just give me a thumbs up after you finish this compile step on the side. Sorry? Yeah, just compile. So model.compile and you provide the loss, optimizer and metrics. So all of these are, you need to specify them because they're all the hyper parameters. Even the choice of loss function, optimizer, they're hyper parameters that you choose beforehand. So yeah, and the metrics, yeah, you need to have at least one metric other than the loss to track during training. So yeah, just click this cell. Shouldn't give you any errors. Yeah. So now what I want to introduce before we start training is this thing called a callback. So a callback is basically this thing that logs my training routine. So all the data associated with training like the loss values, my accuracy values over my training sequence is gonna log all that and kind of store it in like a form of history. And a callback, what it does is you can visualize the callbacks in this thing called tensorboard. So tensorflow, it comes with this this visualization called dashboard as you visualization dashboard called tensorboard as you can see, you know, on the right side. So all these graphs that you see, they're all the different metrics that I'm trying to track. So by default, you track the accuracy and the loss, but you can add in your own custom stuff. So yeah, tensorboard, it allows you to visualize your performance metrics during training, during testing. And it gives you the graphs to kind of see, you know, where I'm going wrong, what else I need to do to, I guess, bring my model back on the right track. So to use a callback or to use the tensorboard callback specifically, we can import the tensorboard callback like so, tensorflow.keras.callbacks, import, oh, yeah. Yeah, it's tensorflow, yeah, my bad. Yeah, yeah, so yeah, tensorflow.keras.callbacks, import the tensorboard callback. So tensorboard, you can think of it as, suppose I'm running my 2.4, 2.4 KM, and my friend is, you know, the guy timing me at the end of every lap. Tensorboard is that guy. So it basically logs everything. So when I'm running around the track, whenever I finish one lap, he's gonna scream out the timing like, okay, you took two minutes to complete one lap. And he keeps screaming out the lap timings, the individual lap timings, whenever I pass that finish line again. So that's what tensorboard does. As my model is training, every epoch when it's training, it logs that, I guess, timing, but in this case, the loss and accuracy, it logs all of that information and it displays that. So it's kind of, I guess, it's synonymous to the timer person when you're running. So yeah, we can run this cell, shouldn't give you any errors. So yeah, let's create our first callback. So TB, or you know, the tensorboard callback, you can call this anything. TB equals tensorboard. So it's asking for this thing called the logdir, which basically stands for my logging directory. So this is basically, it's a directory where I want to store all my log files. So let's just say I wanna store it in this folder called my logs. Yeah, let's just store it in my logs. And when we run this, again, no errors. So now that we have a callback, we can finally fit the model on the dataset or in other terms, we can finally start training. So to train a model on a dataset, we have to do, we have to write a model.fit. And remember the dstrain variable from earlier, we just need to specify our training dataset. Yeah, we have to specify the training dataset. We have to specify the number of epochs that it's gonna train on. So for, I guess we're running low on time, so I'm just gonna specify, well, I guess two epochs. It's not gonna give me much of a performance boost, but just to visualize what usually happens when training. After that, I'm gonna say validation data, it's gonna autocomplete equals ds underscore test. So dstest from earlier, it's my testing dataset. And finally, I want my timer. I want that timer person to be there. So callbacks equals an array of all my callbacks. So I can have as many callbacks as I want, and Keras has support for over 10 different callbacks, but here we're only using one. So we just need to specify an array for callbacks and just type in TB or your callback here into it. Yeah. TensorFlow, you specify your logging directory. I just call this my logs. You can call it anything. And you just need to fit the model. So again, you can just go ahead and click this. If you run into an error, it's probably because of some dimensionality issue. So to fix this, we had to go back all the way here. Yeah, so we had to go back all the way here and reload all the cells from this dstrain onwards. So this one, again, it's more of like a TensorFlow error. It's not an error on our part. All you need to do is reload the dataset. Actually over here, in your dstrain, you can actually say download equals true. So that actually fully caches the entire dataset in memory. So we don't get those weird errors at the bottom. Oh, your model training is taking a long time. Have you clicked that fit cell? Are you still here? So I'm up all the way back where we started. Do you get this error? Or do you see something like a... Oh, so you see something. Oh, the play button, oh, this play button over here. Okay, yeah, that, again, it depends on, I guess, the speed of your machine, yeah. Yeah, but, okay, so we're using the CPU runtime. The CPU uses your local machine, but if you go for your GPU runtime, which we'll be doing in the next segment, the GPU runtime, it uses the hosted VM. So yeah, it connects your machine to a Tesla K8 GPU. Again, since they're giving it out for free, that you can't really complain much. Tesla K8, they're not that fancy, but we had to make do if we want to train free of cost. So from here, all the way from DS train, where we first loaded it, we may need to run all these cells once again. Yep. So again, when you just rerun all the cells, if you run into a dimensionality error, especially when you are about, just about to train, just reload all the cells, restart your VM and run all your cells. You know a tensorboard error? Or you, but do you see anything below that? Yeah, again, it's based on your machine's performance, your local machine's performance. Oh, so on this side, did you see the training happening? Okay, yeah. So in some cases you may see a message, some cases you might not, but do you get something that looks like this? So you see something moving. Okay, great. So technically you can even train up to, well, five epochs. Yeah, just, yeah. So over here in your DS train variable over here, you can set download equals true. So it's gonna pull everything that's there in that repository down to my machine so that I don't need to keep pulling it whenever I need to refer to it. So everything is now finally on my machine. I don't need to rely on anything else. And it's much faster when you download the data set. So yeah, in DS train, just download equals true. You don't need to do the same for testing. Yeah, you don't need to do that for testing because the testing data set is only like 10K images. So it's not that much effort, but 60K, it's a huge number. All right, so. Yeah, you can, exactly, yeah. So notebooks, it'll actually save all your things. It's like creating a Google doc, right? So when you type in something, when I close that browser, when I go back to my drive, it's there. Similarly, when you close a Colab notebook, you can access it in your drive. Yeah, and it'll actually like save all your outputs so you don't need to run everything again because there are some, I guess, training jobs that take 12 hours, right? I don't want to keep running that every day just because I closed my browser. At the same time, I don't want to keep my browser and my Chrome or like my browser or the tab in my browser because it looks messy. So I can close it, come back, it'll retain all that information. So I guess that's another benefit of using Colab. All right, so, you know, just. Yeah, yeah, so you can do command S or control S. Yeah, and it'll save it. It's similar to what you do with Google Docs. It autosaves, yeah, similarly, even Colab's autosave. Yeah, the Colab notebooks, all of them autosave. Yeah, even Docs, it autosaves. Actually, all the sheets, Docs, Google Slides, all of them are autosave by default. But I guess for Brevitys sake, you can keep, yeah. Yeah, but if you want to manually save, you can do command S or control S. It'll like force save again, yeah. Yeah, so I guess I changed this two to a five. So we're just training for five epochs instead of two. So yeah, we just need to wait for this training loop to end, yeah. Oh yeah, so the callbacks in general, not all of them are updated to 2.0 and not all of them are, when you put them on a VM, they can run with the same performance. So that's why some of them may be too slow for your CPU clock. It'll give you the error, but you don't really need to worry. So even to the online viewers, if you have any errors, if you're running this right now, you can ignore any warnings because most of the warnings, they're just because of some internal error with TensorFlow or TensorFlow, so you don't really need to care. You can just ignore all of them and just continue coding. They won't affect anything. It'll just train, yeah. Yeah, so wherever you are, when you close, when you close a tab that contains a callback notebook or if the notebook is inactive for a really long time, like I haven't done anything to it for, I guess like two hours, right? It'll automatically disconnect. Remember when we started, we clicked this connect button that was here. It allocates the VM and it connects everything and then you can start coding. Yeah, so if you don't use it, if it's idle for a long time, it'll disconnect. All you need to do is click that connect button again and you can run all yourselves. So actually if you're lazy to run, keep clicking the play button. If you click runtime in your menu, you can just click run all and it'll just do a full sweep. So you don't need to keep clicking the play button. So you can do whatever you want, but in the background, the code is running. All right, so now that we have trained, now that we have trained our model for FI epochs, if you can see over here, the accuracy, you can see that on our testing set, the DS test, we're reaching a 97.5 accuracy, which is kind of huge. So that means out of all the 10,000 images, it got 97% of them correct. That's already about 9.7K, yeah, so yeah. So this is your testing accuracy or validation accuracy and this is your training accuracy. We got a 99 here and a 97 here. You can always, for most cases, you can always expect your validation or testing accuracy to be slightly lower than your training accuracy because again, these testing instances, we've never really fully seen them or the model hasn't seen them, but it's seen everything in the training dataset, which is like it accounts for the discrepancy. All right, so now that we've finished training for FI epochs and we have these random numbers over here, we can finally visualize this entire thing in TensorBoard. So to invoke TensorBoard and if you wanna visualize it in Colab, you can type in the percent sign load underscore ext, which is basically load extension and you can type in TensorBoard over here, yeah. And you can run this cell. Yeah, shouldn't give any errors. So yeah, percent sign load underscore ext space TensorBoard. So that gives you control over using TensorBoard. So now that we have, I guess, imported TensorBoard, we can finally visualize our training performance, all those metrics in TensorBoard. So again, exclamation sign TensorBoard, it should auto complete now. And for this, it wants a logging directory. So it's gonna search for that logging directory on my machine and it'll dig out all my log files from there. So over here, when in my TensorBoard callback, I said that I want my logging directory to be my logs. So over here, I just need to type in my underscore logs again. So what TensorBoard's gonna do is it's actually gonna go into that folder and just get all my training logs. So now if I can zoom out and, so it'll say launching TensorBoard and that cell, hold on. Yeah, it should be, I think it should, yeah, it's, so over here, it's just one direct word, L-O-G-D-I-R. There's no dash or underscore. All right, so you should see something like this and this is TensorBoard. So does everyone see this? Something like this? Yeah, just give it some time to run. Yeah, can I know what error that is? What's it saying? Oh, can you try loading that extension again? On this side, do you have TensorBoard? Is TensorBoard running? So I'm guessing you finished your training loop successfully. Did you get any errors for training? Yeah, so I think to speed things up, you can set the epochs to two and just quickly run that cell, the model.fit cell. Do you see this? Oh, no worries, yeah. So just to recap, you need to type in percent load underscore ext TensorBoard. And when you run that, it's gonna invoke TensorBoard such that you can display it in your colab notebook. And in your next cell, you can type percent TensorBoard and as a command line parameter, you can give it logdir, L-O-G-D-I-R and specify the logging directory that you typed in over here with your TensorBoard callback. And when you run that cell, it should say launching TensorBoard and it should show everything. So do you see something like this? Okay, yeah, so TensorBoard, it takes a while to load because it needs to build the UI. But I guess we're running low on time here. It's already five. So I'll give you like a quick recap or like a quick run through of TensorBoard on my screen up here. So TensorBoard, as you can see, it logs my training accuracy and my loss values. So over here, you can see a gradual increase in accuracy. We're hitting up to 99% training accuracy and testing accuracy is about 97. And at the same time, when you see our loss values, when you're training, a good thing that or like something that you should really keep an eye out for is if your loss values are decreasing. If your loss values are decreasing, it means that your model is actually training because the number of testing instances that it's getting correct, that's also increasing. So I'm getting more things correct, which is why the loss, which kind of measures how far apart my predictions are from the true labels. So yeah, it should be decreasing. And over here, we can see that the graph, it shows some gradual decay in the loss. And yeah, that's good. So over here, yeah? So yeah, to clear your logs, I should admit, actually, no, you don't need to clear the logs because it automatically, it overwrites whatever's there. Because as you're running these cells, right, it's gonna overwrite all the preexisting variables or whatever it had in its memory. So yeah, you don't need to worry about that. Okay, sorry. Oh yeah, that usually happens when you're running TensorFlow on something like Jupyter, something on a local machine, not a hosted runtime. Because I've gotten that local host with the pot 6006, that usually happens when you're using TensorBoard, when you're not using TensorBoard, the way you're supposed to in Colab. Can you check again whether you've written both of these done? First, you need to load the extension and then you need to call it. Because if you type in TensorBoard on its own, it's gonna assume that you are using TensorBoard using local host, not something that's hosted online, which is why when you're using a local host runtime, you can click another local host, it'll direct you to another port URL, which is why you can see TensorBoard in the way you saw it. But, oh yeah, I think that should be fine. And then after that, you can type in percent TensorBoard and specify the logging directory like so. You can try reloading the extension if it doesn't show up in the first try. I think for now, is it possible to refer to the slides on the screen? I think I can quickly brush through, yeah. So, TensorBoard, this is what it is. It's a user interface, kind of like a visualization dashboard that you can use to visualize your training and testing metrics, performance metrics. It logs everything and contains metadata on your training performance. So, at the same time, earlier I mentioned that TensorFlow, what it does is it creates a computation graph. So, to visualize this computation graph, you need to go to the graphs section in the header and you'll be presented with a bunch of boxes with arrows connecting each other. So, this is called a computation graph. This corresponds to the model that we wrote above here. So, the model that I wrote here with three dense layers, it's the exact same thing that's represented here in the form of a computation graph. So, it takes in my input like so. It feeds it through all the layers in my network. It finds the loss value and, at the same time, it's computing the metrics. So, if you were to double click on one of these computation graph nodes, you can actually, it'll expand and it'll give you a bit more information. So, MATMUL over here stands for Matrix Multiplication. So, earlier I also mentioned that Alia, it computes the W dot X plus B. So, this is exactly what is happening. You multiply the X value. So, in this MATMUL node, it's taking in the inputs, multiplying it with the respective weight and it's adding the bias in this bias add node. So, this happens for all the nodes in your computation graph and that's exactly how, yeah, that's how it's visualized over here. Of course, with TensorFlow, TensorFlow board, you can do much more. Depending on the kinds of metrics that you use, you know, custom metrics, you'll have a lot more graphs to show. Yeah, something like this. You have a lot of graphs over here. It all depends on what kind of metrics you give and what kind of callbacks you're using. It also kind of specifies what exactly you're trying to track during your training. So, now that we have kind of trained of a simple three-layer neural network on MNIST, I guess we can go ahead and do something a bit more advanced. So, a while back, are there any issues still with the TensorFlow board? Let me just plug in my charger. Excuse me. Yeah, on this side, were you able to get TensorFlow? It works, right? Okay. Okay, great. Yeah, so, I guess just to bring everyone up to speed. So, yeah, in the scalars section, you can see all the graphs that we tried to log. So, it has the loss graph and my accuracy graph. So, do you see it? Okay, yeah. So, also, when you go to this graphs header, you should see this computation graph, right? They're all nodes and they have arrows connecting to it or edges connecting to it. This is our computation graph. This exactly corresponds to the model that we wrote in the cells above. Yeah, and you can double click one of them and it'll expand to show you what's happening inside it. So, over here, we see that the matrix multiplication W times X, you're multiplying the incoming input with the weight and we're also adding the bias. Yeah, and you can double click to close it. So, yeah, this is, I guess, like a quick overview with MNIST and TensorBoard. Yeah, TensorBoard, it's been there ever since version one came out. So, it's compatible with everything. Just that for this specific example, I guess to keep up with the most recent developments in TensorFlow, two point X is the stable release. Okay, so like to the online viewer, the question was why has Google changed a lot of the API specs when we moved from version 1.1 X all the way to version two? I guess the answer for that is as Keras became the standard for creating models or basically building data pipelines, machine learning pipelines, it was way easier to use and more convenient. So, TensorFlow, they had to do a full 180 degree flip when changing their all the low level operations and converting it into like a high level abstraction like Keras. That's why they've deprecated most of the underlying, I guess, ugly code and they've made it much more convenient. It's all the sub modules that exist in version two is really convenient to access them and you don't need to bang your head against the wall when trying to make it work. It's pretty simple, yeah. Yeah, they had to rewrite a lot of code to bring it up to the current version, yeah. But I guess it's far like the benefit of the community because you want many people to be able to use it, both advanced users and beginners. So they're kind of cater to both of them, you wanna give them what they want. So for advanced users, you want a lot of customizability. For beginners, if you give them the bare minimum, they'll be able to work with it. So yeah, that's why they had to break down quite a bit, construct the tf.keras, the complete library, yeah. It's approaching 5.30, I guess, like we can speed it up. I have a few demo notebooks that I can, so I have links to all my notebooks that I'll give soon. You can check it after the talk, perhaps, yeah. So I did have another talk lined up for like a workshop hands-on thing for today. So I thought if we had time, we could run through this COVID-19 x-ray classification task. So what it is, it's this dataset consisting of 146 x-ray scans. So from all the past cases in the past two months, one of the professors at the University of Montreal, he compiled all of these x-rays with the diagnosis, and he made it open source on GitHub. So I'll share the notebook where I kind of create a TensorFlow pipeline that takes in all these x-ray images and trains a convolutional neural network so you can see it at your own time. Actually, yeah, you can see it over here. You can access, so even to the online viewers, because of time constraints, and I guess to prepare for like, is there a next talk after this? Yeah, so I guess for time, for lack of time, I think you can speed up and close the session for today. So yeah, these are the two notebooks that I wrote a while back to prepare for this talk. So this is like the complete version of all the code. It has no errors, so you can download the notebook into your own Google Drive and you can connect it and you can run all of them. And for one of them, actually the MNIST tutorial, I have hooked it up to the hosted GPU, so you don't need to run it on your local machine anymore. So if you go to the MNIST version and run all of them, it runs on the GPU. And the same thing for the COVID-19 training dataset task. When you run this notebook, all of it's on the GPU. So no matter which device you're running your code on, you should get some really fast performance upgrades. So yeah, so it's device agnostic, shouldn't really be a problem. All right, so I guess now that we're kind of finished more the coding segment, I think I can wrap it up here. So in this tutorial, what I've covered is the basics of TensorFlow, a bit of history, why it was created, what it provides. I've gone through the model, the models API, the layers API, TensorFlow to visualize all your performance training metrics. So Keras backend, it was supposed to be covered in the second talk, but what it does is it provides you with some low level operations, like the mean, sum, square, absolute, dot product, arg max, it provides you all these tools so that you can write your own loss function, you can write your own optimizer, you can write your own metrics to track. So yeah, you could do this all with the Keras backend in the second notebook, the COVID-19 notebook, it has a segment for custom metrics and custom loss functions. And yeah, so with TensorFlow, what you can do is, you can do data collection, you can do data preprocessing, you can load data sets, your own data sets are the ones provided by TensorFlow. You can build and train test models and you can even visualize their performance in TensorBoard. And once you're happy with its performance, you can actually push it to production, you can put it up on specialized hardware using this thing called TensorFlow Serving, which allows you to take the model, the frozen weights and serve it in the cloud. So all you need to do is give it an API call and you can get back the predictions. So if you are in Singapore and you would want to explore more about TensorFlow, you can consider joining the TensorFlow and Deep Learning Singapore Meetup Group. So when you go to meetup.com, you can search up TensorFlow and Deep Learning Singapore and you can become a member. It's the largest TensorFlow Meetup Group on the planet today. It's growing really quickly. So it holds talks every month. So there, we have lightning talks, those five minute talks where someone comes and presents their projects or we have the hosts or someone from all these big corporations using machine learning, they come over and they kind of give us a glimpse as to what that company is doing or their ML teams are doing. And another thing that the TFDL Meetup does is it covers like cutting edge research papers. So all the fancy research that's taking place at Google Brain, at DeepMind, OpenAI, they write up the code or they take that paper and convert that into like TensorFlow code and they kind of do like a live demo at the talk. So if you're considering joining, there are lots of benefits. First, you get the network. At the same time, you're learning really cool things about TensorFlow. If you're confident enough to like, create your own site project and you want to demo it in front of 200 odd people for a given session, you can if you just become a member. Additionally, if you want something a bit more low level or low key, you can join the Dev Space Singapore at the Google APAC headquarters. So Dev Space is, well, space for all the community events related to developers and programming in general. So almost monthly, they have different talks about a wide range of topics. Some machine learning, some web development, some, I guess app development. So yeah, for all their machine learning and TensorFlow talks, you can try attending them. Other than that, if you do have any doubts or you want to reach out if you have any questions about the talk or machine learning AI in general, you can try connecting with me on any one of these platforms. I'm active on GitHub, LinkedIn, Twitter and Medium.com. When I share the slides URL, you can actually click one of these images and it'll take you to one of the profiles. Well, yeah, other than that, well, thank you for tuning in. Thank you whoever showed up and like to the online viewers. Yep, any questions? And if you want to access the slides as showed up in the front, you can access it at this URL. So if you want to take a picture or you want to come back later or you want to review the content, especially the COVID-19 dataset task, you can come back to this slide show at this URL, FA-TF-2020. Yeah, other than that, thank you for coming. It's kind of like late into the evening, especially on a Friday. Hope you enjoyed. There are some parts that did seem kind of dull because, well, you need to have kind of like a deep intuition of what's going on underneath, like to fully understand what TensorFlow is doing surface level, which is why some parts may seem like, you know, what am I saying? So, yeah, but the more you kind of dwell into TensorFlow, the more you do side projects or the more you explore or read its documentation, the more you'll kind of understand what's going on under the hood, which kind of simplifies the entire thing. So, yeah, thanks for coming.