### Install TensorFlow Docs Source: https://github.com/keras-team/keras-io/blob/master/examples/generative/md/conditional_gan.md Install the necessary TensorFlow documentation library for the example. ```python ! ``` -------------------------------- ### Install TensorFlow Docs Source: https://github.com/keras-team/keras-io/blob/master/examples/generative/ipynb/conditional_gan.ipynb Install the necessary TensorFlow documentation utilities. This is a prerequisite for running the example. ```python !pip install -q git+https://github.com/tensorflow/docs ``` -------------------------------- ### Install Libraries Source: https://github.com/keras-team/keras-io/blob/master/examples/keras_rs/ipynb/scann.ipynb Install the necessary libraries, keras-rs and scann, for the example. The '-q' flag ensures quiet installation. ```bash !pip install -q keras-rs !pip install -q scann ``` -------------------------------- ### Install TensorFlow Addons Source: https://github.com/keras-team/keras-io/blob/master/examples/vision/ipynb/patch_convnet.ipynb Install the TensorFlow Addons library, which is required for this example. Use the '-U' flag to upgrade if already installed. ```shell pip install -U tensorflow-addons ``` -------------------------------- ### Setup and Imports Source: https://github.com/keras-team/keras-io/blob/master/examples/vision/ipynb/image_classification_with_vision_transformer.ipynb Sets up the Keras backend and imports necessary libraries for the example. ```python import os os.environ["KERAS_BACKEND"] = "jax" # @param ["tensorflow", "jax", "torch"] import keras from keras import layers from keras import ops import numpy as np import matplotlib.pyplot as plt ``` -------------------------------- ### Instantiate Model, Optimizer, and Loss Function Source: https://github.com/keras-team/keras-io/blob/master/guides/md/writing_a_custom_training_loop_in_torch.md Get a fresh model and instantiate an optimizer and loss function for training. This setup is crucial before starting any custom training loop. ```python model = get_model() # Instantiate an optimizer to train the model. optimizer = keras.optimizers.Adam(learning_rate=1e-3) # Instantiate a loss function. loss_fn = keras.losses.CategoricalCrossentropy(from_logits=True) ``` -------------------------------- ### Install TensorFlow Addons Source: https://github.com/keras-team/keras-io/blob/master/examples/vision/ipynb/supervised-contrastive-learning.ipynb Install the TensorFlow Addons library, which is required for this example. ```python pip install tensorflow-addons ``` -------------------------------- ### Add Keras examples and guides programmatically Source: https://context7.com/keras-team/keras-io/llms.txt Use KerasIO.add_example and KerasIO.add_guide to process and format tutorial files. This includes converting formats, injecting buttons/banners, and normalizing paths. ```python from autogen import KerasIO from master import MASTER import os from pathlib import Path root = Path(__file__).parent.parent.resolve() keras_io = KerasIO( master=MASTER, url="/", templates_dir=str(root / "templates"), md_sources_dir=str(root / "sources"), site_dir=str(root / "site"), theme_dir=str(root / "theme"), guides_dir=str(root / "guides"), examples_dir=str(root / "examples"), redirects_dir=str(root / "redirects"), ) # Add a single example (relative path within examples/) keras_io.add_example("vision/mnist_convnet") # Creates: # examples/vision/ipynb/mnist_convnet.ipynb # examples/vision/md/mnist_convnet.md (with Colab button injected) # examples/vision/img/mnist_convnet/ (any generated plots) # Add a guide keras_io.add_guide("functional_api") # Creates: # guides/ipynb/functional_api.ipynb # guides/md/functional_api.md ``` -------------------------------- ### Install Keras-RS Source: https://github.com/keras-team/keras-io/blob/master/examples/keras_rs/ipynb/basic_retrieval.ipynb Installs the Keras-RS library. This is a prerequisite for running the example. ```python !pip install -q keras-rs ``` -------------------------------- ### Install KerasHub and Keras Source: https://github.com/keras-team/keras-io/blob/master/examples/generative/ipynb/text_generation_gpt.ipynb Install the necessary libraries for the example. Ensure Keras is upgraded to version 3. ```python !pip install -q --upgrade keras-hub !pip install -q --upgrade keras # Upgrade to Keras 3. ``` -------------------------------- ### Compile and Train Model Setup Source: https://github.com/keras-team/keras-io/blob/master/examples/vision/md/shiftvit.md Prepares the model for training by passing sample data to infer input shapes and calculates total and warmup steps based on dataset size, batch size, and epochs. This setup is crucial before compiling and starting the training process. ```python # pass sample data to the model so that input shape is available at the time of # saving the model sample_ds, _ = next(iter(train_ds)) model(sample_ds, training=False) # Get the total number of steps for training. total_steps = int((len(x_train) / config.batch_size) * config.epochs) # Calculate the number of steps for warmup. warmup_epoch_percentage = 0.15 warmup_steps = int(total_steps * warmup_epoch_percentage) ``` -------------------------------- ### Setup and Imports for Sample Size Estimation Source: https://github.com/keras-team/keras-io/blob/master/examples/keras_recipes/ipynb/sample_size_estimate.ipynb Sets up the environment by defining the backend, importing necessary libraries, and setting a random seed for reproducibility. This code is essential for running the subsequent parts of the example. ```python import os os.environ["KERAS_BACKEND"] = "tensorflow" import matplotlib.pyplot as plt import numpy as np import tensorflow as tf import keras from keras import layers import tensorflow_datasets as tfds # Define seed and fixed variables seed = 42 keras.utils.set_random_seed(seed) AUTO = tf.data.AUTOTUNE ``` -------------------------------- ### Install TensorFlow Addons Source: https://github.com/keras-team/keras-io/blob/master/examples/keras_recipes/md/better_knowledge_distillation.md Install the TensorFlow Addons library, which is required for this example. This command should be run in your environment before executing the rest of the code. ```python !pip install -q tensorflow-addons ``` -------------------------------- ### Install Python Packages and Setup Source: https://github.com/keras-team/keras-io/blob/master/guides/ipynb/keras_hub/function_gemma_with_keras.ipynb Installs necessary Python packages and configures the environment for Keras and JAX backend. Ensure you have Kaggle credentials set up in your environment secrets. ```python import os import datetime import psutil import yfinance as yf from ddgs import DDGS import keras_hub import re import pytz import warnings warnings.filterwarnings("ignore") os.environ["KERAS_BACKEND"] = "jax" ``` -------------------------------- ### Install Dependencies Source: https://github.com/keras-team/keras-io/blob/master/examples/vision/ipynb/nl_image_search.ipynb Installs necessary libraries: TensorFlow Hub, TensorFlow Text, and TensorFlow Addons. Use this command to set up the environment for the example. ```python pip install -q -U tensorflow-hub tensorflow-text tensorflow-addons ``` -------------------------------- ### Add New Code Example or Guide via CLI Source: https://context7.com/keras-team/keras-io/llms.txt Use the 'autogen.py' command-line tool to add new code examples or guides. This command converts tutobook Python files into .ipynb and .md formats and preprocesses Markdown. Specify a working directory to avoid re-running notebooks. ```bash # From the scripts/ directory, add a new vision example python autogen.py add_example vision/my_new_example # With a persistent working directory (avoids re-running the notebook) python autogen.py add_example vision/my_new_example --working_dir=/tmp/my_workdir # Add a new guide python autogen.py add_guide writing_a_custom_training_loop_in_jax ``` -------------------------------- ### Setup Environment and Imports Source: https://github.com/keras-team/keras-io/blob/master/examples/rl/md/actor_critic_cartpole.md Imports necessary libraries and configures the environment for the Actor Critic CartPole example. Ensure Keras backend is set to TensorFlow. ```python import os os.environ["KERAS_BACKEND"] = "tensorflow" import gym import numpy as np import keras from keras import ops from keras import layers import tensorflow as tf # Configuration parameters for the whole setup seed = 42 gamma = 0.99 # Discount factor for past rewards max_steps_per_episode = 10000 # Adding `render_mode='human'` will show the attempts of the agent env = gym.make("CartPole-v0") # Create the environment env.reset(seed=seed) eps = np.finfo(np.float32).eps.item() # Smallest number such that 1.0 + eps != 1.0 ``` -------------------------------- ### Setup Environment and Model/Dataset Functions Source: https://github.com/keras-team/keras-io/blob/master/guides/ipynb/distributed_training_with_jax.ipynb Configures the Keras backend to JAX and defines functions to create a sample Keras model and load the MNIST dataset. Ensure JAX is installed and configured for your hardware. ```python import os os.environ["KERAS_BACKEND"] = "jax" import jax import numpy as np import tensorflow as tf import keras from jax.experimental import mesh_utils from jax.sharding import Mesh from jax.sharding import NamedSharding from jax.sharding import PartitionSpec as P def get_model(): # Make a simple convnet with batch normalization and dropout. inputs = keras.Input(shape=(28, 28, 1)) x = keras.layers.Rescaling(1.0 / 255.0)(inputs) x = keras.layers.Conv2D(filters=12, kernel_size=3, padding="same", use_bias=False)( x ) x = keras.layers.BatchNormalization(scale=False, center=True)(x) x = keras.layers.ReLU()(x) x = keras.layers.Conv2D( filters=24, kernel_size=6, use_bias=False, strides=2, )(x) x = keras.layers.BatchNormalization(scale=False, center=True)(x) x = keras.layers.ReLU()(x) x = keras.layers.Conv2D( filters=32, kernel_size=6, padding="same", strides=2, name="large_k", )(x) x = keras.layers.BatchNormalization(scale=False, center=True)(x) x = keras.layers.ReLU()(x) x = keras.layers.GlobalAveragePooling2D()(x) x = keras.layers.Dense(256, activation="relu")(x) x = keras.layers.Dropout(0.5)(x) outputs = keras.layers.Dense(10)(x) model = keras.Model(inputs, outputs) return model def get_datasets(): # Load the data and split it between train and test sets (x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data() # Scale images to the [0, 1] range x_train = x_train.astype("float32") x_test = x_test.astype("float32") # Make sure images have shape (28, 28, 1) x_train = np.expand_dims(x_train, -1) x_test = np.expand_dims(x_test, -1) print("x_train shape:", x_train.shape) print(x_train.shape[0], "train samples") print(x_test.shape[0], "test samples") # Create TF Datasets train_data = tf.data.Dataset.from_tensor_slices((x_train, y_train)) eval_data = tf.data.Dataset.from_tensor_slices((x_test, y_test)) return train_data, eval_data ``` -------------------------------- ### Install Dependencies and Download Assets Source: https://github.com/keras-team/keras-io/blob/master/guides/ipynb/keras_hub/stable_diffusion_3_in_keras_hub.ipynb Installs necessary libraries and downloads sample images for the demonstration. ```python !pip install -Uq keras !pip install -Uq git+https://github.com/keras-team/keras-hub.git !wget -O mountain_dog.png https://raw.githubusercontent.com/keras-team/keras-io/master/guides/img/stable_diffusion_3_in_keras_hub/mountain_dog.png !wget -O mountain_dog_mask.png https://raw.githubusercontent.com/keras-team/keras-io/master/guides/img/stable_diffusion_3_in_keras_hub/mountain_dog_mask.png ``` -------------------------------- ### Install Dependencies and Build Site Source: https://context7.com/keras-team/keras-io/llms.txt Install necessary Python packages and build the complete Keras documentation site. The build process writes output to the 'site/' directory. Serve the site locally using the 'serve' command. ```bash pip install -r requirements.txt pip install -U keras cd scripts python autogen.py make python autogen.py serve ``` -------------------------------- ### Model Creation, Compilation, and Training Setup Source: https://github.com/keras-team/keras-io/blob/master/examples/audio/ipynb/transformer_asr.ipynb Sets up the Transformer model, loss function, optimizer with a custom learning rate, and initiates the training process with the display callback. ```python batch = next(iter(val_ds)) # The vocabulary to convert predicted indices into characters idx_to_char = vectorizer.get_vocabulary() display_cb = DisplayOutputs( batch, idx_to_char, target_start_token_idx=2, target_end_token_idx=3 ) # set the arguments as per vocabulary index for '<' and '>' model = Transformer( num_hid=200, num_head=2, num_feed_forward=400, target_maxlen=max_target_len, num_layers_enc=4, num_layers_dec=1, num_classes=34, ) loss_fn = keras.losses.CategoricalCrossentropy( from_logits=True, label_smoothing=0.1, ) learning_rate = CustomSchedule( init_lr=0.00001, lr_after_warmup=0.001, final_lr=0.00001, warmup_epochs=15, decay_epochs=85, steps_per_epoch=len(ds), ) optimizer = keras.optimizers.Adam(learning_rate) model.compile(optimizer=optimizer, loss=loss_fn) history = model.fit(ds, validation_data=val_ds, callbacks=[display_cb], epochs=1) ``` -------------------------------- ### Install imgaug library Source: https://github.com/keras-team/keras-io/blob/master/examples/vision/ipynb/keypoint_detection.ipynb Install the imgaug library, which is required for data augmentation in this example. ```python !pip install -q -U imgaug ``` -------------------------------- ### Setup Imports and Parameters Source: https://github.com/keras-team/keras-io/blob/master/examples/vision/ipynb/visualizing_what_convnets_learn.ipynb Imports necessary libraries and sets up environment variables, image dimensions, and the target layer name for visualization. ```python import os os.environ["KERAS_BACKEND"] = "tensorflow" import keras import numpy as np import tensorflow as tf # The dimensions of our input image img_width = 180 img_height = 180 # Our target layer: we will visualize the filters from this layer. # See `model.summary()` for list of layer names, if you want to change this. layer_name = "conv3_block4_out" ``` -------------------------------- ### Install medmnist Package Source: https://github.com/keras-team/keras-io/blob/master/examples/vision/ipynb/vivit.ipynb Installs the medmnist package, which is required for accessing the dataset used in this example. ```bash !pip install -qq medmnist ``` -------------------------------- ### Install Dependencies Source: https://github.com/keras-team/keras-io/blob/master/examples/vision/ipynb/consistency_training.ipynb Installs necessary libraries including TensorFlow Hub and TensorFlow Models for the example. ```python !pip install -q tf-models-official tensorflow-addons ``` -------------------------------- ### Install TensorFlow Addons Source: https://github.com/keras-team/keras-io/blob/master/examples/keras_recipes/ipynb/better_knowledge_distillation.ipynb Install TensorFlow Addons using pip. This is a required dependency for the example. ```python !!pip install -q tensorflow-addons ``` -------------------------------- ### Install Dependencies Source: https://github.com/keras-team/keras-io/blob/master/guides/ipynb/keras_hub/object_detection_retinanet.ipynb Install necessary packages for the tutorial: keras-hub, keras, and opencv-python. Use the '-q' flag for quiet installation. ```bash !pip install -q --upgrade keras-hub !pip install -q --upgrade keras !pip install -q opencv-python ``` -------------------------------- ### Install networkx Source: https://github.com/keras-team/keras-io/blob/master/examples/graph/ipynb/node2vec_movielens.ipynb Install the networkx package using pip. This is a prerequisite for graph manipulation in this example. ```shell pip install networkx ``` -------------------------------- ### Set up environment and imports Source: https://github.com/keras-team/keras-io/blob/master/examples/vision/ipynb/masked_image_modeling.ipynb Configures the Keras backend and imports necessary libraries for the example. Sets a random seed for reproducibility. ```python import os os.environ["KERAS_BACKEND"] = "tensorflow" import keras import matplotlib.pyplot as plt import numpy as np from keras import layers, ops SEED = 42 keras.utils.set_random_seed(SEED) ``` -------------------------------- ### Install TensorFlow Similarity Source: https://github.com/keras-team/keras-io/blob/master/examples/vision/ipynb/metric_learning_tf_similarity.ipynb Install the TensorFlow Similarity library using pip. This is a prerequisite for running the example. ```bash pip -q install tensorflow_similarity ``` -------------------------------- ### Generate Example Renderings Source: https://github.com/keras-team/keras-io/blob/master/README.md Generate the notebook and markdown renderings for a new example script. This command should be run from the root directory of the project. ```bash python autogen.py add_example vision/script_name ``` -------------------------------- ### Install TensorFlow Text Source: https://github.com/keras-team/keras-io/blob/master/examples/nlp/ipynb/multimodal_entailment.ipynb Installs the TensorFlow Text library, which is required for using BERT models in this example. ```python !pip install -q tensorflow_text ``` -------------------------------- ### Preview New Keras Example Locally Source: https://github.com/keras-team/keras-io/blob/master/README.md Navigate to the scripts directory and run this command to preview a new example. Ensure the tutobook file path is correct. This command may error if cells take too long to execute; examples should be lightweight. ```bash cd scripts python autogen.py add_example vision/script_name ``` -------------------------------- ### Install KerasHub and py7zr Source: https://github.com/keras-team/keras-io/blob/master/examples/nlp/ipynb/abstractive_summarization_with_bart.ipynb Installs the KerasHub library and py7zr for handling 7z archives. Use this command in your environment before running the example. ```bash !pip install git+https://github.com/keras-team/keras-hub.git py7zr -q ``` -------------------------------- ### Serve Keras Website Locally Source: https://github.com/keras-team/keras-io/blob/master/README.md After previewing an example, use these commands to build and serve the Keras website locally. Access the website at 0.0.0.0:8000/examples. ```bash python autogen.py make python autogen.py serve ``` -------------------------------- ### Setup Environment and Imports Source: https://github.com/keras-team/keras-io/blob/master/examples/vision/ipynb/image_captioning.ipynb Sets up the environment by configuring the Keras backend and importing necessary libraries for image captioning. ```python import os os.environ["KERAS_BACKEND"] = "tensorflow" import re import numpy as np import matplotlib.pyplot as plt import tensorflow as tf import keras from keras import layers from keras.applications import efficientnet from keras.layers import TextVectorization keras.utils.set_random_seed(111) ``` -------------------------------- ### Tutobook Format for Guides and Examples Source: https://context7.com/keras-team/keras-io/llms.txt Defines the structure for tutobooks, which are Python scripts serving as the source of truth for guides and examples. Docstrings within the script are converted to Markdown cells, and code blocks become code cells. The script must begin with a mandatory header docstring. ```python """ Title: Image Classification with EfficientNet Author: [Jane Doe](https://github.com/janedoe) Date created: 2024/01/15 Last modified: 2024/06/01 Description: Fine-tune EfficientNet for image classification. Accelerator: GPU """ """ ## Introduction We use EfficientNetB0 pre-trained on ImageNet. """ import keras from keras import layers """ ## Build the model """ inputs = keras.Input(shape=(224, 224, 3)) x = keras.applications.EfficientNetB0(include_top=False)(inputs) x = layers.GlobalAveragePooling2D()(x) outputs = layers.Dense(10, activation="softmax")(x) model = keras.Model(inputs, outputs) model.compile( optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"], ) """ ## Train """ ``` -------------------------------- ### Launching TensorBoard Source: https://github.com/keras-team/keras-io/blob/master/examples/keras_recipes/md/sklearn_metric_callbacks.md This example shows how to launch TensorBoard from the command line to visualize training metrics, including custom metrics like Jaccard score, by specifying the log directory. ```bash tensorboard --logdir=logs ``` -------------------------------- ### Install necessary libraries Source: https://github.com/keras-team/keras-io/blob/master/examples/keras_rs/ipynb/distributed_embedding_jax.ipynb Installs required packages for JAX, TPU embeddings, TensorFlow, and Keras-RS. Ensure you have a compatible JAX version and TPU setup. ```python !pip install -q -U jax[tpu]>=0.7.0 !pip install -q jax-tpu-embedding !pip install -q tensorflow-cpu !pip install -q keras-rs ``` -------------------------------- ### Run Training and Resume Source: https://github.com/keras-team/keras-io/blob/master/guides/md/distributed_training_with_tensorflow.md Demonstrates how to initiate training for one epoch and then resume training from a checkpoint. ```python run_training(epochs=1) # Calling the same function again will resume from where we left off run_training(epochs=1) ``` -------------------------------- ### Contrastive Pretraining Model Setup Source: https://github.com/keras-team/keras-io/blob/master/examples/vision/ipynb/semisupervised_simclr.ipynb Initializes and compiles a contrastive learning model for pretraining. Requires a dataset and specifies optimizers for contrastive and probe tasks. ```python pretraining_model = ContrastiveModel() pretraining_model.compile( contrastive_optimizer=keras.optimizers.Adam(), probe_optimizer=keras.optimizers.Adam(), ) pretraining_history = pretraining_model.fit( train_dataset, epochs=num_epochs, validation_data=test_dataset ) print( "Maximal validation accuracy: {:.2f}%".format( max(pretraining_history.history["val_p_acc"]) * 100 ) ) ``` -------------------------------- ### Model Compilation and Setup Source: https://github.com/keras-team/keras-io/blob/master/examples/structured_data/md/classification_with_grn_and_vsn.md Sets up hyperparameters, creates the model using `create_model`, and compiles it with an Adam optimizer, binary crossentropy loss, and binary accuracy metric. This prepares the model for training. ```python learning_rate = 0.001 dropout_rate = 0.15 batch_size = 265 num_epochs = 20 # may be adjusted to a desired value encoding_size = 16 model = create_model(encoding_size) model.compile( optimizer=keras.optimizers.Adam(learning_rate=learning_rate), loss=keras.losses.BinaryCrossentropy(), metrics=[keras.metrics.BinaryAccuracy(name="accuracy")], ) ``` -------------------------------- ### Test Documentation Server with Makefile Source: https://github.com/keras-team/keras-io/blob/master/README.md Use the Makefile to build and run a Docker image containing the documentation server for testing purposes. ```bash make container-test ``` -------------------------------- ### Install gdown Source: https://github.com/keras-team/keras-io/blob/master/examples/vision/ipynb/probing_vits.ipynb Update the gdown library to the latest version. This is required for running the example on Google Colab. ```shell pip install -U gdown -q ``` -------------------------------- ### Model Building and Training Setup Source: https://github.com/keras-team/keras-io/blob/master/examples/vision/ipynb/handwriting_recognition.ipynb Builds the model, creates a prediction model for inference, initializes the edit distance callback, and starts training the model. ```python epochs = 10 # To get good results this should be at least 50. model = build_model() prediction_model = keras.models.Model( model.get_layer(name="image").output, model.get_layer(name="dense2").output ) edit_distance_callback = EditDistanceCallback(prediction_model) # Train the model. history = model.fit( train_ds, validation_data=validation_ds, epochs=epochs, callbacks=[edit_distance_callback], ) ``` -------------------------------- ### Setup Imports for Keras and NLP Source: https://github.com/keras-team/keras-io/blob/master/examples/nlp/ipynb/masked_language_modeling.ipynb Imports necessary libraries for Keras, NLP tasks, data manipulation, and system operations. Ensure tf-nightly is installed. ```python import os os.environ["KERAS_BACKEND"] = "torch" # or jax, or tensorflow import keras_hub import keras from keras import layers from keras.layers import TextVectorization from dataclasses import dataclass import pandas as pd import numpy as np import glob import re from pprint import pprint ```