Tensorflow Tutorial for Python in 10 Minutes

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
it is 12 o'clock you may now commence your deep learning exam you have one hour what's happening guys my name is nicolystronaut and in this video we're going to be running through tensorflow in 10 minutes let's take a deeper look as to what we're going to be going through so super quickly what is tensorflow well tensorflow is a flexible and open source library that allows us to build deep learning models it was originally built by the google brain team in 2015 and the reason why a lot of data scientists tend to use tensorflow is that it makes building deep learning models a whole heap easier faster and more reproducible so let's take a look as to what we're going to be covering in this video so first up we're going to be smashing through this so we're going to build a tensorflow churn model then we're going to train it predict and last but not least we're going to be able to save that model and reload it from memory so that we can use it later on ready to do it let's get to it alrighty so in order to run through tensorflow in 10 minutes we're going to be speeding through this but there's five key things that we need to go through so first up what we're going to be doing is importing some data and i've got some pre-written code here now all of this code is going to be available inside of a jupyter notebook in the description below via github so by all means do check that out if you want to reproduce this so once we've imported some data we're then going to import our dependencies we're going to build and compile a model we're then going to fit and predict our model so this is also known as training and then last but not least we're going to save our model down to memory so that we can reload it and use it later on down the track now first up let's quickly start with our data so the data set that we're going to be using is a churn data set so there's a whole bunch of feature columns here so if we go all the way out these are our feature columns so all of these and then the last column which is going to be our target or our y variable is going to be this churn column here so what we're actually going to be doing is using tensorflow to predict whether or not a customer is likely to churn so yes or no so if it's yes then that means they have churned if it's no it means they haven't turned so let's dive right into it so the first thing that we're going to do is import our data now we're just going to be using standard pandas and do a little bit of pre-processing here this isn't a full data science workflow we're just going to be going through tensorflow as a focus so i'm going to bring in our data and this is going to read in our data create an x and a y variable and then we're going to create a trained test split using the trained test flip function from scikit learn and this will give us an x train data frame which we're going to use to train our tensorflow model and a y train data frame so we'll also have a x test and a y test component all right onto the tensorflow bit so the first thing that we need to do is import our dependencies so let's go on ahead and do that all right so those are our dependencies imported so written three lines of code there so the first one is from models import sequential so this sequential model is going to be our core model class then we've also imported load models so this is going to allow us to reload our model from memory later on the next line that we've written is from tensorflow.keras.layers import dense so dense is going to be a fully connected layer inside of our neural network so it's going to allow us to build a whole bunch of hidden layers then the last thing that we've imported is from sklearn.metrics import accuracy score so our accuracy score is going to be the metric that we use to evaluate how well our model is performing so that's our dependencies imported now the next thing that we need to do is actually build up and compile a model so we're going to use the sequential class to do this and add a bunch of dense layers so let's go ahead and do that alrighty so that's our model built so we've written four lines of code there so the first one is model equals sequential so this is instantiating our sequential class then what we're doing is we're adding a bunch of layers to our neural network so whenever you hear somebody talking about hidden layers this is the step that we're actually setting up here so in this case we've added two hidden layers so a dense fully connected layer here and another dense fully connected layer here so if we actually step into this in order to do that we've written model dot add and then we've written dents so this is using our dense layer that we imported up here then we've passed through a couple of keyword parameters so units equals 32. so this means that we're going to have 32 neurons inside of this dense layer and then we've also specified an activation function so in this case our activation function acts as a modifier to the output from our neural network so say for example you pass through a whole bunch of numbers and you didn't pass through an activation function well what you're going to do is get the raw output from that neuron by passing through a relu activation function you're converting the output to a minimum of zero and then an unlimited upward value so it basically looks like a hockey stick then the next keyword parameter that we've passed through is input underscore dim and then to that we've set it as the length of our x train data frame so this basically specifies that our input dim is going to be the same number of dimensions that are available inside of our feature data frame then we've specified another hidden layer so this particular dense layer is going to have 64 neurons and again it's going to have an activation of relu and then the last one that was specified is again model dot add dense this one's going to have one output so remember we only want to predict one or zero yes or no true or false and in this case really what we're trying to do is predict yes or no whether or not a client has churned and then we specify the activation of sigmoid so this is going to take the output of our previous layer and the output of this layer and convert it to a value between one or zero so one representing yes they have turned zero representing no they haven't turned now the next thing that we need to do is compile our model so let's do it alrighty that's our model compiled so when we compile our model what we're basically doing is we're telling tensorflow how we want to train our model what loss metrics we want to use what optimizer we want to use and what metrics we want to focus on so in order to compile our model written model dot compile and then we'll pass through three keyword parameters loss equals binary underscore cross entropy optimizer equals sgd and metrics equals accuracy so the best way to explain this is think about a game of battleship so our loss is basically the sum of how far our estimations are from sinking a battleship our optimizer is how we choose to search through and find those battleships so basically we might choose to use a grid pattern we might choose to use a random so basically our optimizer is getting us closer to our end outcome and ideally helping us reduce our loss and then our metrics allow us to evaluate how well our model is performing alright now that's done the next thing that we need to do is fit and predict our model so let's go and do it alrighty so that's our model fitting or training so in order to train our model we've written model dot fit and then we've passed through our x train data frame so remember this is our training data frame and we've also passed through y trains so this is our x variable and our y variable also known as our features and our target then we've specified how long we want to train for so we've specified our epochs and we've set that to 200 so you can play around with this and then we've also specified our batch size so how large of a batch we want to pass through to tensorflow before actually making an update so let's let that finish training and then we'll be able to make some predictions all righty and that's our model finish training so ideally what you want to see when you're training is that your loss is reducing and ideally your accuracy is increasing so you can see that we started up right up here with an accuracy of about 0.5 and if we scroll all the way down we're now at 0.42 so we could probably train this for longer but in this case we're running through it pretty quickly so this is our model now fit now that that's done we can actually go on ahead and make a prediction so if we add a new cell let's go and do that all righty and those are our predictions done so to run a prediction we've just written model dot predict and then we'll pass through our x test data frame and we'll store this in a variable called y hat now the thing is when you get your result out of tensorflow it's going to be a continuous value between 0 and 1. so what we want to do is convert this to a binary outcome so 0 or 1. so what we've done in the next line is written a list comprehension here so what we're basically doing is setting our output or our outcome to 0 if the value is less than 0.5 or 1 if it's above that so it's the opposite side of it so in this case now we can take a look at our predictions by typing in y hat and you can see we've got a whole bunch of zeros and a whole bunch of ones and we can also calculate our accuracy score so let's take a look and there you go so we've now gone and calculated our accuracy score so to do that we've used the accuracy score method which we imported from scikit learn up here and to that we've passed through y test and y hat now in this case our accuracy isn't all that great so 60 or 0.59 isn't all that great so this is an indication that you might want to train longer you might want to use some regularization you might want to do some additional pre-processing on your data now the last thing that we want to do with tensorflow is save our model and be able to reload it from memory later on so let's go and save our model alrighty so that's our model saved so in order to save our tensorflow model all we need to do is write model dot save and then pass through the name of the folder that we want to save it into so if i actually scroll over now inside of the folder that we've got our jupyter notebook i've now got this tf model folder which includes all of our saved model files now if we wanted to reload it back into memory we can delete our model right so now it's effectively deleted from memory inside of our jupyter notebook so if i actually went in random prediction you can see that it's no longer there so in order to reload it we just use the load model function that we imported up here from our tensorflow.caroustop models class so let's go ahead and load our model and there you go so we've now gone and reloaded our model so we've created a variable called model and then we've used our load model method and passed through the name of our saved model folder so again it's going to be this you can name it whatever you'd like now if we go and run a prediction again just to make sure it is working you can see that no errors and again we're getting predictions so that's tensorflow in a nutshell so basically what we did is we imported our dependencies we built and compiled our model with a number of differing hidden layers then we went and fit our model using the model.fit method and then last but not least we went and saved our model using model.save and reloaded it using load model and that about wraps it up thanks again for tuning in guys hopefully you found this video useful if you did be sure to give it a thumbs up hit subscribe and tick that bell so you get notified of when i release future videos and let me know what you're going to be using tensorflow to build thanks again for tuning in peace
Info
Channel: Nicholas Renotte
Views: 38,319
Rating: 4.8850379 out of 5
Keywords: tensorflow tutorial, deep learning, introduction to tensorflow, tensorflow 2.0 tutorial, machine learning with tensorflow, tensorflow python
Id: 6_2hzRopPbQ
Channel Id: undefined
Length: 11min 32sec (692 seconds)
Published: Wed Dec 23 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.