Topics: Coronavirus | AI | Data Science | Deep Learning | Machine Learning | Python | R | Statistics

KDnuggets Home » News » 2017 » Sep » Tutorials, Overviews » Keras Cheat Sheet: Deep Learning in Python ( 17:n38 )

Keras Cheat Sheet: Deep Learning in Python


 
  http likes 185

Keras is a Python deep learning library for Theano and TensorFlow. The package is easy to use and powerful, as it provides users with a high-level neural networks API to develop and evaluate deep learning models.



Inspect Model

 
With the model architecture in place, you can quickly inspect your model with the functions summary(), get_config() and get_weights(). If you’d like to check out the output shape, you can simply use the output_shape attribute.

Keras image

 

Compile Model

 

Keras image

Compiling the model is a piece of cake with the help of compile(). Make sure to pass in the optimizer, loss functions and the metrics that you want to be monitoring during training.

Some of the most popular optimization algorithms used are the Stochastic Gradient Descent (SGD), ADAM and RMSprop. The choice for a loss function depends on the task that you have at hand: for example, for a regression problem, you’ll usually use the Mean Squared Error (MSE). As you see in this example, you used binary_crossentropy for the binary classification problem of determining whether a wine is red or white. Lastly, with multi-class classification, you’ll make use of categorical_crossentropy.

 

Model Training

 
Before you can check out how well or badly your model does on your training data, you first need to train it on your data. In this case, you pass the IMDB data to the fit() function, together with the batch size, the number of epochs. Additionally, you can also specify that you want to turn on the verbosity mode to get more output at the end of each epoch and the data on which you would like to evaluate the loss and any model metrics at the end of each epoch.

Keras image

 

Evaluate Your Model’s Performance

 
Now that you have trained your model, it’s time to test your model’s performance. We’ll continue with model3 and test it with the help of the evaluate() function. Pass in the test data and labels and determine the batch size and you’re ready to go. When you execute this line of code, the result will be saved in the score variable. Print out the score variable to take a look at how well your model is doing!

Keras image

 

Prediction

 
Now that you know how well or bad your model is doing, you can also take a look at the predicted labels. To do this, you use the predict() function, to which you pass the test data and the batch size. The results will be immediately outputted or, if you assign the line of code to a variable, saved within a variable.

Keras image

 

Save/Reload Models

 
To save your models to reload them later, you can use the save() and load_model() functions. The latter one you can get from the models module of the Keras library. Just pass in the filename and you’re ready to go!

Keras image

Note that when you save a Keras model into a single HDF5 file, it will contain the architecture of the model, the weights of the model, the training configuration and the state of the optimizer. In short, everything you need to work with your models afterward!

 

Model Fine-Tuning

 
Now that you have the performance results and the predictions, you can start making some changes to your model and finetune it so that you can get even better results! Two techniques that are covered in the cheat sheet are the optimization parameters and early stopping.

Keras image

 

Optimization Parameters

 
You can try to adjust (some of) the parameters of the optimization algorithm that you give to the compile() function. In the example given in the cheat sheet, you use RMSprop() function to adjust the RMSprop optimizer’s learning rate and learning rate decay.  Of course, this is just one example - You can find more examples and information here.

 

Early Stopping

 
You use early stopping to Stop training when a monitored quantity has stopped improving. EarlyStopping() is part of the Keras callbacks module, and allows you to set up a monitor for when you’re fitten the model to the data, like the example in the image above shows.
 
 

Get Started!

 
You’re now ready to go and start experimenting with the Keras library! If you’d like to see more examples, definitely make sure to check out the examples in the Keras Github repository or take a look at the Deep Learning with Python book, written by the author of the Keras package, François Chollet. This book is perfect for those who want to learn how to use deep learning to solve real-world problems.  

PS. Don’t forget to keep your cheat sheet handy, of course. 

 
DataCamp is an online interactive education platform that that focuses on building the best learning experience specifically for Data Science. Our courses on R, Python, SQL, and Data Science are built around a certain topic, and combine video instruction with in-browser coding challenges so that you can learn by doing. You can start every course for free, whenever you want, wherever you want.

Karlijn Willems is a data science journalist and writes for the DataCamp community, focusing on data science education, the latest news and the hottest trends. She holds degrees in Literature and Linguistics and Information Management.

Related:


Sign Up

By subscribing you accept KDnuggets Privacy Policy