Keras
Keras is a high-level neural network library written in Python that runs on top of TensorFlow, Microsoft Cognitive Toolkit, Theano, or PlaidML. It was designed to enable fast experimentation with deep neural networks.
Keras began as a library offering a wide range of abstractions to simplify interactions with neural networks. Initially, it supported multiple backends, including TensorFlow, Microsoft Cognitive Toolkit, Theano and Deeplearning4j. However, as Keras evolved, it discontinued support for all these backends except TensorFlow (TF). Currently, Keras can utilize PyTorch, JAX, and TF as its backend engines.
As of 2024, Keras has become one of the de facto interfaces for interacting with TF. It offers simple, easy-to-understand building blocks that enable any developer to construct a neural network, regardless of their chosen backend. While TF is better suited for deep neural network research, Keras excels at rapid prototyping.
Keras offers more than just the fundamental building blocks of neural networks, such as layers and functions. It provides numerous tools that significantly simplify working with text and images. Keras is widely used in various areas of machine learning and artificial intelligence, including:
- natural language processing;
- computer vision;
- time series analysis;
- content generation;
- recommender systems.
Keras key feature: the ability to create neural networks with both simple, predefined topologies and complex ones with equal ease. This flexibility extends to neural networks with multiple inputs and outputs, as well as those using shared layers multiple times. Moreover, Keras allows you to build models that handle non-sequential data streams effectively, without overly complicated development processes.
It isn’t only developer-friendly but also supports distributed training of models across multiple GPUs. While each backend implements this feature slightly differently, the core code remains largely consistent:
- TensorFlow - using tf.distribute API;
- PyTorch - using DistributedDataParallel module;
- JAX - using jax.sharding API.
Keras installation
Installing the latest version of Keras requires just one command:
pip install --upgrade keras
The Python package manager automatically downloads and installs the latest version of Keras. Another effective way to isolate the installation is by creating a separate virtual environment:
python -m venv /home/usergpu/venv
Let’s activate it:
source /home/usergpu/venv/bin/activate
Now you can install Keras using the command mentioned above. This method allows you to experiment with various package versions without affecting the Python installation on the server’s operating system.
Extending features
When using Keras to build neural networks, consider using additional software to help overcome common development challenges.
Hyperparameters
Neural networks are typically trained using two types of values: hyperparameters and weights. Hyperparameters are static values that control the learning process, while weights are dynamic values that change during learning. The choice of hyperparameters is crucial, as learning algorithms use them for internal configuration, significantly impacting the final learning outcome.
For optimal hyperparameter configuration, you can use KerasTuner, a specialized library. It offers flexible hyperparameter management by allowing dynamic definition during model creation.
Computer vision
When developing a neural network for automatic image recognition and description, consider using the KerasCV library. This ready-made library extends the standard Keras API with components that simplify common machine vision tasks, including:
- object detection;
- object classification;
- data augmentation;
- image segmentation;
- image generation;
- and much more.
Natural language processing
Assessing audience reaction to content is a crucial marketing metric. This can be measured in various ways, such as by counting likes and comments. However, comments can be both positive and negative, which presents a challenge. For a single piece of content, like a social media post with few comments, manual evaluation is feasible. But when dealing with hundreds of posts and thousands of comments, the task becomes overwhelming.
This is where natural language processing comes to the rescue. By creating and training a neural network, it’s possible to determine the tone of text fragments. When fed user comments, this network can accurately assess how well the audience received the content. Marketers can then use this data to fine-tune their content plan or even adjust the company’s overall marketing strategy.
Keras, regardless of the backend used, excels at such tasks. For a quick start, the KerasNLP library is a valuable tool. It facilitates feature extraction from text, utilizing pre-trained models like BERT, RoBERTa or GPT2.
KerasHub addresses several challenges in collaborative development:
- It provides a repository for storing models.
- It includes a versioning system, allowing you to track model evolution and roll back to previous versions if needed.
- The platform supports documentation hosting, essential from the earliest stages of development.