Keract is a powerful library designed for visualizing activations and gradients in Keras models. Whether you are working with LSTM networks or convolutional neural networks (CNNs), Keract simplifies the process of understanding what’s happening inside your models. In this article, we will guide you step-by-step on how to effectively use Keract and troubleshoot common issues you might encounter along the way.
Getting Started with Keract
Before we dive in, make sure you have installed Keract. You can do this using pip:
bash
pip install keract
Understanding the Core Functionality
Keract provides several key functions to interact with the model’s internals. Think of your neural network as a multi-layer cake, where each layer has its own flavor. Keract allows you to taste each layer individually, revealing the distinct contributions each has to the final taste of the entire cake. Here are the main functions you’ll be using:
- get_activations: Fetch the output of each layer for given input data.
- display_activations: Visualize the activations using heatmaps or other plots.
- get_gradients_of_trainable_weights: Retrieve gradients for the weights in your model.
- persist_to_json_file: Save activations to a JSON file for later analysis.
Fetching Activations
To retrieve the activations for your model, you can utilize the get_activations function. Below is a sample code showing how to get activations from a simple model:
python
import numpy as np
from tensorflow.keras import Input, Model
from tensorflow.keras.layers import Dense, concatenate
from keract import get_activations
# Model definition
i1 = Input(shape=(10,), name='input1')
i2 = Input(shape=(10,), name='input2')
a = Dense(1, name='fc1')(i1)
b = Dense(1, name='fc2')(i2)
c = concatenate([a, b], name='concat')
d = Dense(1, name='output')(c)
model = Model(inputs=[i1, i2], outputs=[d])
# Inputs to the model
x = [np.random.uniform(size=(32, 10)), np.random.uniform(size=(32, 10))]
# Call to fetch the activations of the model
activations = get_activations(model, x, auto_compile=True)
# Print the activations shapes
[print(k, "-", v.shape) for (k, v) in activations.items()]
In this code, we first define a simple Keras model and generate random input data. When we call get_activations, it explores each layer (just like sampling the flavors of our cake) and prints out their output shapes.
Visualizing Activations
After obtaining the activations, it’s time to visualize them. You can use display_activations to plot these activations directly.
python
keract.display_activations(activations, cmap='viridis', save=False, fig_size=(24, 24))
Working with Gradients
Understanding the gradients can provide insights into how the model learns. You can use the get_gradients_of_trainable_weights function to fetch these gradients:
python
gradients = keract.get_gradients_of_trainable_weights(model, x, y)
Here, you’ll need to specify your labels (y) alongside your model’s input (x) to get the gradients related to each trainable weight.
Troubleshooting Common Issues
While using Keract, you may face issues especially with nested models, as recent TensorFlow versions can be tricky. Below are some troubleshooting ideas:
- Ensure that your model is compiled and designed correctly before fetching activations.
- For nested models, try to simplify the architecture by avoiding multiple layers and see if it resolves the issue.
- If activations return empty or incorrect shapes, double-check your input data, ensuring it matches the expected dimensions of the model.
- If you encounter performance lags, consider providing fewer layer names in the
get_activationscall to minimize the computation expense.
For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.
Conclusion
Keract is a versatile library that provides valuable insights into the behavior and learning process of Keras models. By getting activations and gradients, you can better understand how your model operates, which is crucial for debugging and improving performance.
At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.
Happy coding!

