Autokeras Tutorial with Python

AutoKeras is a Keras-based machine learning framework. It was developed by DATA Lab at Texas A&M University to provide deep learning for everyone. Simply put, it is an automatic machine learning framework for deep learning. If you want to learn how to use AutoKeras for Deep Learning, this article is for you. In this article, I will present a tutorial on AutoKeras using Python.

What is AutoKeras?

AutoKeras is an AutoML framework for deep learning. If you’ve used deep learning frameworks like PyTorch, TensorFlow, and Keras before, you’ll be able to understand how easily you can build powerful deep learning models using this AutoML framework. Although it takes the same time as other deep learning frameworks, it is easy to use compared to all other deep learning frameworks.

It is built on TensorFlow and Keras, so it is currently only compatible with Python version 3.5 and higher and TensorFlow version 2.3.0 and higher. You do not need to update your version of TensorFlow if you are using a lower version because when installing the AutoKeras library, it will be updated automatically in your system.

AutoKeras Tutorial with Python

Now in this section, I will take you through a tutorial on the AutoKeras library in Python. Here I will build a deep learning model for the task of text classification using Python. If you have never worked using AutoKeras then you can easily install it in your system by using the pip command:

  1. pip install autokeras

I will suggest you use GPU enabled system for this task. If your system does not have GPU then you can use Google Colab for this task. Now let’s start this task by importing the dataset and prepare it to fit into the model:

I will now train a text classification model using the autokeras library with max_trial = 1. If you are working with a much larger dataset, you should set this parameter to more than 1 to 5 depending on how much data you have:

Trial 1 Complete [00h 00m 57s]
val_loss: 0.510387659072876

Best val_loss So Far: 0.510387659072876
Total elapsed time: 00h 00m 57s
INFO:tensorflow:Oracle triggered exit
Epoch 1/2
159/159 [==============================] - 33s 204ms/step - loss: 0.6760 - accuracy: 0.5970
Epoch 2/2
159/159 [==============================] - 33s 210ms/step - loss: 0.4758 - accuracy: 0.7894
INFO:tensorflow:Assets written to: ./text_classifier/best_model/assets

In the code above I also set the epochs to 2, you can increase the number of epochs depending on how much data you have in your training dataset. But if you get good results in fewer epochs, that’s fine, but it usually happens when the dataset is small. Now let’s test the text classification model on the test set and look at the final predictions:

102/102 [==============================] - 6s 55ms/step
         id  target
0         0       1
1         2       0
2         3       1
3         9       1
4        11       1
...     ...     ...
3258  10861       0
3259  10865       1
3260  10868       1
3261  10874       1
3262  10875       0

[3263 rows x 2 columns]

Summary

AutoKeras is an automatic machine learning framework for deep learning. If you’ve used deep learning frameworks like PyTorch, TensorFlow, and Keras before, you’ll be able to understand how easily you can build powerful deep learning models using this AutoML framework. I hope you liked this article on a tutorial on AutoKeras using Python. Feel free to ask your valuable questions in the comments section below.

Aman Kharwal
Aman Kharwal

I'm a writer and data scientist on a mission to educate others about the incredible power of data📈.

Articles: 1498

Leave a Reply