Keras Tutorial For Beginners | Deep Learning Models Using Keras | Edureka | Deep Learning Rewind - 5

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone this is anirudh from edureka in this session let's have a look at kiras it is a deep learning framework which is very popular and widely used well guys this session is intended to be useful for anyone considering to kickstart the learning about kiras and need some insight about the framework so let's check out the agenda for today well guys let's start out by checking out what kiras actually is then i'm sure you guys are really curious about the contributors who make keras what it is followed by that we can check out the models offered by kiras and once we have understood these models we'll check out the steps needed to implement a neural network lastly we'll be checking out a very interesting use case and we can summarize the entire thing later on so the focus is basically on approaching the framework and to find out what it has to offer for all of us deep learning enthusiasts out here so now that we have established the agenda guys let's begin this session by checking out what kiras actually is well kirasa is a python-based deep learning framework which is actually the high level api of tensorflow and now i have four major highlights for you guys so let's check it out from the start well kira's basically runs on top of theano tensorflow or cntk since it runs on top of any of these frameworks keras is amazingly simple to work with you might be wondering why well building models are as simple as stacking layers and later connecting these graphs guys kiras attracts a lot of attention but why since it is open source it is actively developed by all the contributors across the world and the documentation is nearly endless well we're good with the documentation but how does keras perform guys since it is an api which is actually used to specify and train differentiable programs high performance naturally follows through here so now that we know what keras actually is who makes keras what it is well we need to check out some of the contributors and the backers to the deep learning framework well guys kiras had over 4800 contributors during its launch and the initial stages and now that number has gone up to 250 000 active developers well what amuses me is that there is a 2x growth ever since every year since its launch also it holds really good amount of traction among multiple startups big players like microsoft google nvidia and amazon actively contribute to the development of keras good enough so at this point i'm sure all of you are curious about who uses kiras well keras has an amazing industry traction and it is used in the development of popular firms like netflix uber expedia help and more well now that you know the next time you watch a movie on netflix or book an uber you know keras is being used so guys if keras is getting all this attention what makes it so special what makes kiras stand out among all the top framework here i have for you top 10 snippets that makes keras so special well guys the focus on user experience has always been the major part of keras and next large adoption in the industry definitely we just checked out all of the industry traction it gets and this holds well and next it is multi-backend and supports multi-platform as well this helps all the coders come together and code easily next up the research community present for kiras is amazing along with the production community so this is a win-win for me guys so what do you think and moving on all the concepts are really easy to grasp with kira's guys and next up it supports fast processing which is really good and guys it runs seamlessly on both the cpu and the gpu and it has support for both nvidia and amd as well and the best part for me is the freedom to design on any architecture and then later implement it as an api for your projects guys this definitely is a major advantage for me so next up for all the beginners keras is really simple to get started with and i'm here to help you guys with this tutorial for that so stay tuned and lastly the easy production of models makes keras that much special guys now that we know keras is special guys let's dig in a little bit about one of the major concepts which make keras what it is the user experience well in my opinion this is very important for anyone who wants to know more about keras or better they want to start creating their own neural nets using keras so clearly kiras is an api designed for humans well why so because it follows the best practices for reducing cognitive load which ensures that the models are consistent and the corresponding apis are simple and moving on kiras provides clear feedback upon occurrence of any error and this minimizes the number of user actions required for the majority of the common use cases guys and lastly kiras provides high flexibility to all of its developers well we all love high flexibility right so how is keras doing this guys it's very simple it integrates with lower level deep learning framework languages like tensorflow or theano so guys this ensures that you can implement anything in keras which you actually built in your base language which is amazing so next up we need to talk about how kira supports the claim of being able to support multi-platform and lets us work with multiple back-ends you can develop keras in python as well as r the code can be run with tensorflow cntk theano or mxnet totally based on your requirement this almost feels like a tailor-made api for the framework guys the code can be run on the cpu or the gpu as well support for both the big players being nvidia and amd here so this ensures that producing models with keras is really simple total support to run with tensorflow serving gpu acceleration such as cuda when using modules such as webkiras and kerasjs native support to develop android and ios apps using tensorflow and core ml and yes full-blown support to work with raspberry pi as well so guys moving on we need to check one quick concept which forms the backbone as walking principle of keras so let's check out a computation graph here is an example for you guys just before decoding and working our way through the graph let's look at the features so guys do note that computational graphs are used for expressing complex expression as a combination of simple operations for crash to work with it is mainly useful for calculating the derivatives during the phase of back propagation and hence it makes it easier to implement distributed computing on the whole so all it takes is to specify the inputs outputs and to make sure that the graph is connected throughout i hope you guys know what a connected graph is so let's check out our graph we'll be working our way from the leaf nodes to the top so guys here as you can see equal to c multiplied with d where c is equal to a plus b and d is equal to b plus one so all we're doing is we're making sure we land at e equal to c cross t which is ahead of our tree and we get to this by performing two operations on the leaf nodes so as you can see walking down further e equal to a plus b into b plus one actually makes sense now and in our case a and b are inputs so guys it is as simple as that so next let's dive a little bit deeper and check out the two major models guys so the first model is a sequential model the walking is basically like a linear stack of layers so the first thing that comes into my mind when i think about the layered approach is the sequential model it is majorly useful for building simple classification network and encoder decoder model guys and definitely yes this is the model which we all know and love so here we basically treat every layer as an object that feeds into the next layer and so on and now in the simple code we'll import keras into python we define the model as sequential and with the hidden layers we have 20 neurons and we'll be using relu here relu is rectified linear unit guys it is one of the activation functions we'll be using well model.fit is used to train the network here by epoch i'm sure all of you guys are familiar with it already so it is basically the forward and the backward pass of all of our training examples and batch size is really straightforward as well it is the number of training examples in one forward and backward pass guys so higher the batch size the more the memory you need so next we need to check out the functional model it is widely used and it holds good for about 95 percent of the use cases well imagine the concept of playing with legos because i'm pretty sure most of us have played with legos in our childhood it's pretty much the same here as well well the highlights of the functional model is that it supports multi-input multi-output and arbitrary static graph topology we have branches so whenever we have a complex model the model is formed into two or more branches based on the requirement guys the code which we have here is pretty much similar to the previous one but with subtle changes we first import the models we work on its architecture and lastly we train the network well with functional models we have this concept called as domain adaption so guys what we did until this stage is that we train a model on one domain but test it on the other this definitely results in poor performance on the overall test data set because the data is different for each of the domains right so what's the solution for this well we adapt the model to work on both the domains at the same time and guys we'll be looking at a very interesting use case using the functional models in the upcoming slides so stay tuned for that so moving on we need to understand about the two basic types of execution in keras deferred and eager execution it is also called a symbolic and imperative execution as well well with deferred we use python to build a computation graph first like we previously discussed and then this compiled graph gets executed well with eager execution there is a slight change guys it is here that the python runtime itself becomes the execution runtime for all of the models it is very similar to execution with numpy so if you're familiar with numpy then it's a cakewalk guys so on the whole here is a quick note symbolic tensors don't have a value in the python code as of yet but eager tensors will have a value in the python code and with eager execution we make use of a type of recurring neural network called as trees so guys it is basically a value dependent dynamic topology structure so what are you guys thinking about keras at this point well it is actually really easy to grasp guys well next let's look at the steps needed to implement our own neural network using keras there are five major steps here guys so starting out we need to prepare the inputs for the model we do this by analyzing our requirements and specifying the input dimensions well as you know what the common inputs are images videos text or audio based on your model requirement the next step is to actually define the artificial neural network model here we do everything from defining the model architecture to building the computation graph and also defining the style we'll be using for the model it is as straightforward as that well step three is to specify the optimizer think of it this way a neural network is just a complex function we need to simplify the process of making the machine learn well the optimizer is just for that there are many types of optimizers such as sgd which is stochastic gradient descent we have rms prop which is based on root mean square and adam and so on and the next step is to define the loss function so for every step in our training we'll be checking the accuracy of prediction by comparing the obtained value with the actual one we check for the difference between them and we print out the loss guys well the goal is to actually define a function which we will use to reduce the losses in each pass of the training phase there are many types of loss functions such as mse which is mean squared error and we have cross entropy loss which is also called as the log loss in most cases and so on and the last step is to actually train the network based on the input data which is also called as a training data and after training we will need to test the model based on the trained data to check if the model actually learnt anything so it is as simple as this guys what do you think i would love to know your views on this so head to the comment section let's have an interaction there and now guys let's spice things up a bit i'm sure you guys were curious about the use case so let me walk you through the entire thing well we'll be checking out a wine classifier in this use case so let's begin by checking out our problem statement so we're trying to predict the price of a bottle of wine just by knowing the description and the variety of wine well we can work this out with the keras functional api and tensorflow we'll be building a wide and a deep network using keras to make predictions for us well can we achieve this goal yes we can this is a problem statement suited for wide and deep learning networks as i mentioned well it involves textual input and there isn't any correlation between the wine's description and its price well this is what makes it fun in my opinion guys so next we need to check out the model a lot of keras models are built using the sequential model api as i told you earlier but let's make use of the functional api for our use case well true the sequential api is still the best way to get started with kiras why because it lets you define models easily as a stack of layers like i explained earlier however the functional model allows for more flexibility and is best suited for models with multiple inputs so we need to know a little bit about wide and deep model guys well wide models are models with sparse feature vectors well what i mean by sparse feature vectors is that it consists of mostly zeros and a little bit of ones and deep networks and networks which do really well on tasks like speech and image recognition so now that that's sorted we need to take a look at the data set well for this case we'll be using a wine dataset from kaggle so what's the data well it's basically 12 columns of data and it's as follows here we'll be talking about the country that the wire is from next up is description a few sentences from the sommelier descripting the wines taste smell look and feel a sommelier is a person who is a professional wine taster guys next up is designation the wineyard within the winery where the grapes that the wine has been made from next up is points the number of points that the wine enthusiasts rated the wine on a scale of 1 to 10 however they only say that they post reviews for the wines which cost greater than equal to 80. next up is price the cost of the bottle of the wine obviously followed by province the province or the state where the wine is from next up have something called as region one well with region one it's the wine growing area in a province or a state let's say for example india next i have region two well sometimes there are more specific regions within a specified wine growing area for example we can say bangalore india but this value can sometimes be blank as well next up is taster's name well as it suggests it's the name of the person who tasted and reviewed the wine taster twitter handle twitter handle for the person who tasted and reviewed the wine well the title of the wine review which often contains the vintage if you're interested in extracting that feature variety the type of the grapes that is used to make the wine let's say for example pinot noir that's a type of grape and finally the winery that made the wine guys the overall goal here is to actually create a model that can identify the variety winery and the location of a wine based on the description alone and this data set offers some really great opportunities for sentiment analysis and other text related predictive models as well so now that that's clear we need to take a look at the sample data case so here we have a description for the wine such as sent if it's start firm or needs more decanting etc so this forms our input for the model guys and the output our model provides just from all of this textual information is the pricing that it predicts how cool is that guys so basically we need to check out some of the prerequisites before jumping into the code since you're working with python we'll require pandas will require numpy sky kit learn and jupyter notebook so yes kiras works on top of tensorflow so we'll require both keras and tensorflow to be installed on the machine so now that that's done moving on let's look at a small piece of code here are all the inputs that will require to build the model and lastly we test the presence of tensorflow by printing the installed version well without tensorflow it wouldn't make any sense so we go to kaggle and download the data and end up converting the data to a pandas data frame guys well that's good enough to start let's look at the code for this model i'll quickly open up google collaboratory which is basically a jupiter notebook hosted on their google cloud you can actually do this on your local machine as well by installing all of the frameworks that i've previously mentioned so let me go ahead and open collaboratory and let's begin guys so guys we'll be executing each of these blocks and we'll be going on from there so let's check out the first block so here we import all of the modules that we require so guys let me run it and that's done so next we need to install the latest version of tensorflow well with google collab it doesn't require any extra setup so it's pretty much straightforward so guys that took about two minutes and tensorflow is installed and as i explained before we need to import the models that we'll use to build the model and after that we'll actually run the code to check the version output of the tensorflow that we just installed and the output we're supposed to be expecting is version 1.7 because 1.7 is the tensorflow version that we installed as you can check out the output you have tensorflow version 1.7 so beautiful so moving on we need to download the data which is from a csv file hosted on the cloud so let's go ahead and do that now that it is downloaded and ready let us convert all the data from the cac file into a pandas data frame as i mentioned so now that that is done let's go ahead and mix up the data by shuffling it and yes so let's start printing samples from our data set so we'll be printing the first five rows and as you can see all the columns that we discussed earlier from country price province all the way till a detailed description of the wine is present here so now that that's done next we need to do some pre-processing to limit the number of varieties of wine in the data set so let us go ahead and set a threshold of 500 in our case so anything less than 500 will be removed from analysis in our model we will be replacing it with nan which is not a number instead of letting it to be blank so let me go ahead and run this so now that that's done the next step we need to do is actually split the data into a training data set and the testing data set so let's go ahead and do that and print the size of both the training data set and the testing data set so let me go ahead and run this code for you guys and there it is we have the training data set size and the testing data set size now that we have the size we need to actually extract the testing and the training features and all of the labels so let me go ahead and run the score and we can actually get the labels and so that does it the training and the testing features and the labels are known to our machine by now so now it's very obvious that we're using a test description well instead of looking at every word that we found in our description in our data set let us limit our bag of words to let's say top 12 000 words in our data set so guys don't worry there is actually a built-in keras utility for creating just this vocabulary this is considered as wide because the input to our model for each description will be a 12 k element wide vector with zeros and ones indicating the presence of word from our vocabulary in a particular description well keras has some handy utilities for text preprocessing that we use to convert the text descriptions into a bag of words with the back of words model we'll typically want to use only a subset of all the total words that we found in our data set and in this example i used 12 000 words but this is a hyper parameter that you can tune well you can try out a few values to see what works best on your data set well we can use the keras tokenizer class to create our bag of words vocabulary for us so let me go ahead and run this to create the tokenizer guys so now that that's done we'll be actually using text to matrix function to convert each description to a bag of words vectors so let me go ahead and run it well now that that's done guys in the original kaggle data set there are about 632 total varieties of wine to make it easier for our model to extract the patterns we did a bit of pre-processing to keep only the top 40 varieties well around 65 percent of the original data set or 96k total examples well we use a keras utility to convert each of these varieties to integer representation and then we'll create 40 element wide one hot vectors for each input to indicate the variety so let me go ahead and run it guys so now that that's run guys at this stage we are ready to build our wide model well guys keras has two apis for building the models the sequential api and the functional api the functional api gives us a bit more flexibility in how we define our layers and lets us combine multiple feature inputs into one layer it also makes it easy for our wide and deep models into one when we are ready guys with the functional api we can define our wired model in just a few lines of code as you see well first we need to define our input layer as a 12k element vector well for each word in our vocabulary and then we'll connect this to our dense output layer to generate the price prediction so let me go ahead and run this well now that that's done we'll compile the model so that it is ready to use if we were using the wide model all on its own this is when we'd actually start training it with the fit function and evaluate later with the evaluate function since we are going to combine it with our deep model later on we can hold off on training until the two modes are combined which is done later guys so let me go ahead and execute this so we define our wired model and yep our wide model is done let's go ahead and print out a summary from the wide model well now that we have a summary we can realize the total number of trainable parameters and non-trainable parameters well in our case the non-trainable parameters are zero guys so guys that's the end to the construction of the wide model and it's time to build our deep model so let's go ahead and check that well to create a deep representation of the wine's description we'll represent it as an embedding well there are a lot of resources on word embeddings but the short version is that they can provide a map word to vectors so that the similar words are closer together in the vector space well to convert our text descriptions to an embedding layer we need to first convert each description to a vector of integers corresponding to each word in our vocabulary we can do that with the handy kara's text to sequence method and now we've got the integerized description vectors we need to make sure that they're all the same to feed into our models well keras is fancy and it has a handy method for that too we'll use part underscore sequences to add zeros to each description vector so that they're all the same length well in this case i used 170 as the max length so no descriptions were cut short so let me go ahead and run this well with our descriptions converted to vectors that are all the same length we're ready to create our embedding layer and then feed it into our deep model guys so let's start building our deep model well there are two ways to create an embedding layer we can use weights from the pre-trained embeddings as i previously explained or we can actually learn the embeddings from our vocabulary well it's best to experiment with both and to see which performs better on your particular data set here we'll consider using learned embeddings well firstly we'll define the shape of our input to the deep model then we'll feed it into the embedding layer and here i'm using an embedding layer with eight dimensions well you can experiment this with tweaking the dimensionality of your embedding layer as per your choice and the output of the embedding layer will be a three-dimensional vectors with the following shape well it'll have a batch size a sequence length well in our case the sequence length is 170. it'll have an embedding dimension it is eight in our example and in order to connect our embedding layer to the dense fully connected output layer we actually need to flatten it first so let's go ahead and define our model and then we can flatten our layer well once the embedding layer is flattened it's ready to be fed into the model and to compile it so let's go ahead and compile it and now that the compilation is done as you can see the loss function we've used in this case is msc which is mean squared error the optimizer we'll be using is atoms and the metrics is accuracy and at this point of time we have established the wired model and the deep model so once we have defined both our models combining them is really really easy guys we simply need to create a layer that concatenates the output from each of the model and then merge them into a fully connected dense layer and finally define a combined model that combines the input and the output from each one well obviously since each model is predicting the same thing which is the price the output or the labels from each one will be the same also guys do note that since the output of our model is a numeric value we will not need to do any pre-processing and it's already in the right format as well how cool is that well that that's done guys it's time for the training and the evaluation well you can experiment with the number of training epochs and the batch size that works best for your data set so guys as you can see the training is actually done so each of the epoch took about 100 seconds and we had 10 epochs for the same so guys here's the important thing that you have to notice with every epoch we were actually reducing the loss all the way from 1100 to 130 guys and the accuracy of prediction went from 0.02 all the way to 0.0994 which is almost 0.1 well wow that's definitely a breakthrough for just 10 passes guys and now that the training is done it's time to evaluate it so let me go ahead and run this piece of code for you guys so that was quick that took only about five seconds and we have evaluated the model and now it's time for the most important part guys seeing how our model actually performs on the data that it has never seen before to do this we'll actually call the predict function on our trained model and we'll be passing it our data set so let's go ahead and do just that well now that that's done we'll have to compare the predictions to the actual values for the first 15 winds from our test data set so guys as you can see we have a set of predictions from the description and the predicted value is about twenty four dollars well the actual value is twenty two dollars next up we have thirty four dollars as a predicted one while the average is seventy well this is not a really good case but okay that's tolerable and next up we predicted 11.9 when the actual value is still wow that is actually really close so next up we have 15.7 versus nine well this goes on and on for the first 15 and it's actually really really good well guys pretty well it turns out that there is some relationship between hawaii's description and its price well we might not be able to see it instinctively but our machine learning model certainly can so lastly let's compare the average difference between the actual price and the mall's predicted price well the average prediction difference is about 10 dollars for every wine bottle wow that is really really nice guys well to wrap this up let's head back to our presentation and we can run through what we did in the entire session well we started out to check what keras actually is then we checked out the contributors who support and develop kiras next we checked out the specialty of keras by highlighting some of the really good features and next we checked out the models and next we checked out the easy steps to implement a neural network and lastly we went through this amazing use case and dived right into the heart of keras well keras is lovely and i can't begin to tell you how much i'm liking it well that is it for the small session guys i hope you took away some real good points from here and that this helped you kick start your kira's learning so thank you guys you know what to do if you have any queries leave a comment and subscribe to our channel for more information on the latest technology and the courses offered you
Info
Channel: edureka!
Views: 1,000
Rating: 4.8857141 out of 5
Keywords: yt:cc=on, keras tutorial, keras tutorial for beginners, keras tutorial tensorflow, keras tutorial python, keras tutorial deep learning, keras tutorial image classification, keras neural network tutorial, keras model tutorial, keras layers tutorial, keras explained, keras tensorflow tutorial, keras python, keras example, keras install, keras tutorial cnn, cnn tutorial, convolutional neural network tutorial, deep learning tutorial, deep learning training, edureka
Id: pVc0rEgdM3M
Channel Id: undefined
Length: 28min 47sec (1727 seconds)
Published: Fri Sep 17 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.