The IT industry creates tools for humans to communicate with computers. For example, programming languages (like Python) are basically languages that we created to give instructions to machines. To master them, you almost need to start thinking like a machine. No wonder these things seem so difficult!
But there are instruments designed with humans in mind as well. Actually, the one we’re about to discuss is approachable enough for many hobbyists and IT beginners to be able to use it. We are talking about Keras — a tool that can teach your PC to perform intelligence tasks, such as the following:
- Outcome prediction
- Natural language processing
- Image and speech recognition
Keras is an add-on for another program — TensorFlow
Keras is not actually a stand-alone program — it’s an API written in Python. “API” stands for Application Programming Interface ― it’s like a middle-man between you and another software. Developers build APIs when they want to make existing programs more user-friendly.
Basically, imagine that an API is a waiter at a restaurant. Here’s how it works:
- You tell the waiter (API) what you want.
- They go back to the kitchen (software) to explain your order.
- The kitchen cooks your order (or executes your command).
- The waiter (API) comes back to you with the results.
In other words, Keras is useless on its own, because it needs another program in order to work properly. The “kitchen” to our “waiter” is TensorFlow — a Python library widely used for machine learning tasks. A library is a collection of pre-written code that programmers use to avoid writing all of the code from scratch. In that sense, Keras is also a library — just one added on top of another library in order to facilitate AI tasks.
TensorFlow is like a backend or an engine, whereas Keras provides a simplified interface. In professional terms, this means that TensorFlow gives access to low-level functionality, while Keras is high-level. Keras-based building blocks require less coding. However, developers can dive deeper, whenever necessary, and access TensorFlow’s more complex features.
TensorFlow has tools for refined customization of building blocks, so developers can gain more control over their models.
Q: In other articles about Keras, I see mentions of other libraries. Does this mean that it’s not only compatible with TensorFlow?
A: Up until version 2.3, Keras supported other neural network libraries, such as Microsoft Cognitive Toolkit, Deeplearning4j, and Theano. But, starting with 2.3 and up, TensorFlow is the only library that works with Keras. Both TensorFlow and Keras are common tools in data science and artificial intelligence. Now, let’s talk about the fields that apply this software to local tasks.
Keras is a tool for deep learning tasks
You’ve probably heard a lot about deep learning and machine learning by now. At first, these terms might look like synonyms. But there are actually important differences between them.
Before we go deeper let’s explore the relationship between the two fields.
Machine learning belongs to the wider field of artificial intelligence. Deep learning, meanwhile, is a subset of machine learning. So, you can imagine all three industries like a matryoshka doll. The bigger one contains the middle one, which in turn contains the smallest.
The difference between deep learning and machine learning lies in how these algorithms perform two important processes: feature extraction and classification.
At the stage of feature extraction, the algorithm identifies important patterns in data in order to better understand it. For example, if it processes a picture of some object, it breaks it down into its most important parts — such as colors and shapes — and then looks for these parts in other pictures. This way, it identifies whether this object is present in other images.
Here's a simplified reference to help you understand how it works:
Then comes classification. Based on extracted features, the computer sorts data into predefined categories. So, if you feed pictures of cats and dogs to the program, it will deduce: “This picture has cat features, so this must be a cat” or “This picture has dog features, so this must be a dog”. This way, algorithms learn to recognize different objects.
In order for the algorithm to start making connections between data and classes, specialists feed it two sets of data: raw input (e.g. images and texts) and a set of labels that indicate what the correct output should be for each input. Then, the algorithm creates a mapping between the “in” and the “out”. The distinction between machine and deep learning comes in how this mapping is performed.
Machine learning algorithms involve human input at the stage of feature extraction. Prior to the classification stage, a specialist needs to extract features manually by specifying which characteristics are the most important for certain categories. For example, when working with pictures of animals, one might extract features like the animal’s color, ear shape, or tail length.
Deep learning algorithms do not require human supervision at the stage of feature extraction. Instead, the model uses an already labeled set as instructions for data processing. So, when data goes through the model, it detects its features and completes the classification step automatically.
The process of the algorithm creating connections between the two data sets is called training. When this step is complete, the learned mapping can be applied to new, unseen data.
For example, you upload 30,000 pictures of puppies to a neural network and point out: “These are puppies”. Then, you show 30,000 pictures of some other animal and point out: “These are not puppies”. The neural network analyzes what characteristics "puppies" have and searches for them in other photos. As a result, it learns to tell the difference between “puppies” and “not puppies”.
What happens in a deep learning algorithm
This combined process of feature extraction and classification takes place in a special model that is unique to deep learning. It’s called a neural network. The word “neural” is used because the model imitates the structure of the human nervous system.
Much like the human brain, neural networks have neurons. These are connected elements of the model, all arranged into layers. The unique aspect of neural networks is that they all have a hidden layer where the magic happens.
Each neuron is responsible for detecting a certain type of feature (remember the animal ears and tails?). So neurons transmit data between each other, processing and interpreting it along the way. Each new layer adds more and more certainty. Thanks to the hidden layer, deep learning models have the ability to learn — or create a mapping between input and output on their own.
Here’s a rough explanation of how it works:
- Neuron A processes input data.
- Neuron A decides: “I’m 80% certain that this picture has dog feature A.”
- It sends this data to Neuron B, including the prediction.
- Neuron B decides: “I’m 23% certain that this picture has dog feature B.”
- Then it sends this data with a prediction to Neuron C, and so on.
On the final layer, the neural network returns a verdict: “I’m 87.4% certain that this is a dog.”
The most useful Keras features
Modern neural networks are unbelievably complex. They have hundreds of neurons, all arranged in different layers. “Cutting edge” models like ChatGPT might have up to 50 hidden layers and more. It would be madness to code such systems manually! Instead, data scientists use helpful tools that enable them to work faster — including Keras.
Keras has algorithms and functions for typical machine learning processes. Data scientists don’t have to write them from scratch — they just call up these functions, and the software does the rest. This saves both time and coding effort, which hugely benefits developers.
Keras allows to customize model architecture easily
If a neural network were a house, TensorFlow would have been a set of tools and building materials. On top of TensorFlow there’s Keras — a builder who already knows how to use those tools and materials. You can tell Keras how your network should look, and it will build it for you in TensorFlow on a turn-key basis.
It works this way because Keras has building blocks — or modules — with their own functions. Data scientists use them to assemble the model. But they can also add custom blocks. So, if a user requires a unique building block, they can write code for it manually.
This allows developers to make neural networks with a highly customizable configuration and structure. This way, they can design an architecture that is tailored for a specific task. Such flexibility helps adjust models as new techniques and algorithms emerge.
In Keras, you assemble models from pre-built modules (like LEGO!)
Here’s another perk: the builder you’ve hired uses building blocks that are ready-made! They are reliable and time-tested, which minimizes mistakes in the construction. Essentially, the builder only needs to put these blocks together as if building a modular house.
Indeed, all the blocks in Keras use pre-defined layers and functions. They are guaranteed to work well and be compatible with each other. It decreases the chance of errors, which makes your neural networks almost completely bug-free.
For instance, with the Text Preprocessing Module, you can convert a piece of text into a list of words. It even has a dedicated command for this specific action. To do this, use this code.
The output for this command would be: ['hello', 'world', 'how', 'are', 'you']
And with the Callback Module, you can enable the recording of events in a history file (among other things). This action would be coded like this:
There are lots of other modules that allow you to increase a model’s accuracy, control its outputs, customize it at the TensorFlow level, and more.
Keras allows users to quickly get started with their projects
What makes Keras different from TensorFlow is that its code is easy to read — even for developers who are new to the library. Also, you can complete frequent and tedious tasks using very few actions. It saves the developer from unnecessary workload.
For example, you need to build a linear model layer by layer. The code would look like this:
Now, you want to add layers. This line adds a fully connected layer (also known as a dense layer) to the neural network model:
It has ten neurons and uses the ReLU activation function. This function means that it takes the input value and returns either the input value or zero, depending on whether the input is positive or negative. This layer also has an input dimension of five, i.e. the input to the neural network will have five features or variables.
Let’s add another fully connected layer:
This one has five neurons. The softmax activation function outputs a probability distribution over the classes. So the sum of the outputs of this layer is equal to one.
With Keras, users can train their models on multiple servers
Warren Buffett once said: “You can’t produce a baby in one month by getting nine women pregnant.” But, you certainly can train your deep learning models faster if you divide the load between two or more machines.
Thankfully, Keras offers support for distributed training. So users who have access to multiple GPUs or servers can accelerate the process by leveraging them.
But how does distributed training work? The data is divided into smaller chunks and sent to different servers. Each server uses the model to make its own computations. These computations are then collected and averaged to update the parameters of the model. This process is repeated multiple times until the model achieves a certain level of accuracy.
In addition to distributed training, Keras also enables mixed-precision training. It involves using lower precision data types to accelerate the training process. With mixed-precision training, the size of the training data can be reduced. It enables the model to be trained faster while maintaining its accuracy.
Range of Keras applications
When it comes to building deep learning models, Keras is a go-to choice for many developers and researchers. As one Redditor put it:
I have never found a deep learning model that I could not implement with [Keras]. It increases productivity and makes your code more maintainable.
Here are some cool things that Keras-based algorithms can do.
Prediction: Keras can be used for predicting various outcomes, such as patient diagnoses, based on their medical records. By training a model on large datasets, Keras can help medical professionals make more informed decisions.
Natural language processing: After processing text data, Keras-based programs can understand the meaning behind the words. For example, people use them to analyze customer feedback and determine if it’s positive, neutral, or negative.
Image classification: By analyzing images pixel by pixel, models built using Keras classify them into predetermined categories. For example, based on a photo, they can accurately detect whether the person is wearing a face mask.
Speech recognition: Keras is used to create applications that recognize and generate speech (e.g. voice assistants). After training a model on samples of human talk, Keras can build a neural network that recognizes spoken words and responds accordingly.
How to start working with Keras
If you’re interested in learning how to work with Keras, you can use a hosted notebook. It’s a cloud-based platform that allows you to create and run code without installing anything on your computer. Google Colab is a popular free hosted notebook that supports Keras. You can take code samples from here and run them to see how they work.
Another option, conda.io is a tool used for environment management. It allows you to create and manage isolated environments for different projects, each with its own set of libraries. You can switch between projects and environments as needed.
Finally, you can install the Keras library to your PC. This can be done by entering the following command in the Python console:
However, this command will not work unless you’ve already installed the current version of Python and TensorFlow. You can download TensorFlow from the official website or by entering the following command in the console:
Depending on your project requirements, you may also need to install additional libraries:
- cuDNN (for calculations using the GPU)
- HDF5 and h5py (for saving Keras models to disk)
- graphviz and pydot (for visualizing models with graphs)
Once you have Keras installed, it’s important to understand how to use it effectively. The official Keras documentation is an excellent resource for learning how to work with the library. It includes detailed examples and explanations of various Keras modules and functions, as well as suggested best practices.
Finally, once you’re comfortable with the basics of Keras, you might want to start working with some real data. A good data set to use for training is Fashion MNIST. It consists of 60,000 images of clothing that have been labeled with corresponding categories. It also has a testing set of 10,000 items.
And what if self-education seems too overwhelming? In that case, you can always consider taking online courses ― for example, TripleTen’s bootcamp, which teaches Keras, TensorFlow, and other skills needed to become a successful data scientist. With our innovative platform and expert instruction, the learning process feels much more exciting than studying on your own.
Our students work on various projects that later become part of their portfolios. Some of these projects even earned our graduates actual job offers! So, if you want to acquire real-life, hands-on experience that is relevant for the industry, join our Data Science Bootcamp!
Keras is an excellent tool to get started with deep learning
Keras offers a Python API that works with TensorFlow. It is used for building and training deep learning and neural network models. Its easy-to-use interface allows you to build complex neural networks using just a few lines of code. Data scientists apply Keras to artificial intelligence tasks, such as image recognition, natural language processing, and predictive analytics.
When working with Keras, don’t be afraid to experiment with different architectures, activation functions, and optimizers. Remember, practice makes perfect! The more experience you gain, the quicker you’ll become a true master of neural networks.