Keras 3 for practical sessions

Keras 3 is a simple, powerful, and flexible API that allows one to quickly learn deep learning concepts.

It is a multi-framework API in python compatible with Tensorflow, PyTorch and JAX backends.

Significant efforts have been made to ensure compatibility and strong performance in terms of memory and computation, whether using tf.data.Dataset, DataLoader (PyTorch), or NumPy arrays.

With Keras, you can go from very high-level functions (as fit()) to very deep customization (i.e., layers, training iterations, …).

To use a specific backend, you have to set the KERAS_BACKEND variable to one of “tensorflow”, “jax” or “torch” before importing keras. To do so, you can do it with a simple command line:

export KERAS_BACKEND="tensorflow"

or, by beginning your Python code with :

import os
os.environ["KERAS_BACKEND"] = "tensorflow"
import keras

Installing Keras3 based on Tensorflow-gpu backend

With Keras, TensorFlow and Jax are faster than PyTorch (see the benchmark here). For Windows users, the installation of recent versions of TensorFlow must be done with WSL (see here).

To install Keras and TensorFlow, you can follow this link. I resume and optimize the procedure in the following :

wget https://raw.githubusercontent.com/keras-team/keras/refs/heads/master/requirements-tensorflow-cuda.txt
wget https://raw.githubusercontent.com/keras-team/keras/refs/heads/master/requirements-common.txt

conda create -y -n Keras3TF python=3.12
conda activate Keras3TF

pip install -r requirements-tensorflow-cuda.txt
pip install --upgrade keras keras-cv keras-hub keras-tuner tqdm jupyterlab jupyter-app-launcher ipywidgets matplotlib

Installing Keras3 based on PyTorch-gpu backend

wget https://raw.githubusercontent.com/keras-team/keras/refs/heads/master/requirements-torch-cuda.txt
wget https://raw.githubusercontent.com/keras-team/keras/refs/heads/master/requirements-common.txt

conda create -y -n Keras3Torch python=3.12
conda activate Keras3Torch 

pip install -r requirements-torch-cuda.txt
pip install --upgrade keras keras-cv keras-hub keras-tuner tqdm jupyterlab jupyter-app-launcher ipywidgets matplotlib

Remember to set the KERAS_BACKEND variable

import os
os.environ["KERAS_BACKEND"] = "torch"
import keras


Tags: