### Example of Compiling a Model Source: https://keras.io/api/models/model_training_apis Demonstrates how to compile a Keras model with a specific optimizer, loss function, and metrics. This setup is typical for binary classification tasks. ```python model.compile( optimizer=keras.optimizers.Adam(learning_rate=1e-3), loss=keras.losses.BinaryCrossentropy(), metrics=[ keras.metrics.BinaryAccuracy(), keras.metrics.FalseNegatives(), ], ) ``` -------------------------------- ### Initialize Distribution System (Example 1: Direct Arguments) Source: https://keras.io/api/distribution/distribution_utils Example of initializing the distribution system using direct arguments for `job_addresses`, `num_processes`, and `process_id`. ```python keras.distribute.initialize( job_addresses="10.0.0.1:1234,10.0.0.2:2345", num_processes=2, process_id=0) ``` ```python keras.distribute.initialize( job_addresses="10.0.0.1:1234,10.0.0.2:2345", num_processes=2, process_id=1) ``` -------------------------------- ### Initialize Distribution System (Example 2: Environment Variables) Source: https://keras.io/api/distribution/distribution_utils Example of initializing the distribution system by setting environment variables for `KERAS_DISTRIBUTION_JOB_ADDRESSES`, `KERAS_DISTRIBUTION_NUM_PROCESSES`, and `KERAS_DISTRIBUTION_PROCESS_ID`. ```python os.environ[ "KERAS_DISTRIBUTION_JOB_ADDRESSES"] = "10.0.0.1:1234,10.0.0.2:2345" os.environ["KERAS_DISTRIBUTION_NUM_PROCESSES"] = "2" os.environ["KERAS_DISTRIBUTION_PROCESS_ID"] = "0" keras.distribute.initialize() ``` ```python os.environ[ "KERAS_DISTRIBUTION_JOB_ADDRESSES"] = "10.0.0.1:1234,10.0.0.2:2345" os.environ["KERAS_DISTRIBUTION_NUM_PROCESSES"] = "2" os.environ["KERAS_DISTRIBUTION_PROCESS_ID"] = "1" keras.distribute.initialize() ``` -------------------------------- ### Example Usage: CosineDecay Without Warmup Source: https://keras.io/api/optimizers/learning_rate_schedules/cosine_decay Demonstrates how to instantiate the CosineDecay schedule for a standard cosine decay without a warmup phase. This is useful when a gradual increase is not needed at the start of training. ```python decay_steps = 1000 initial_learning_rate = 0.1 lr_decayed_fn = keras.optimizers.schedules.CosineDecay( initial_learning_rate, decay_steps) ``` -------------------------------- ### Example Usage of CosineSimilarity Source: https://keras.io/api/metrics/regression_metrics Demonstrates how to use the CosineSimilarity metric with update_state and result methods. Includes an example with sample weights. ```python >>> # l2_norm(y_true) = [[0., 1.], [1./1.414, 1./1.414]] >>> # l2_norm(y_pred) = [[1., 0.], [1./1.414, 1./1.414]] >>> # l2_norm(y_true) . l2_norm(y_pred) = [[0., 0.], [0.5, 0.5]] >>> # result = mean(sum(l2_norm(y_true) . l2_norm(y_pred), axis=1)) >>> # = ((0. + 0.) + (0.5 + 0.5)) / 2 >>> m = keras.metrics.CosineSimilarity(axis=1) >>> m.update_state([[0., 1.], [1., 1.]], [[1., 0.], [1., 1.]]) >>> m.result() 0.49999997 ``` ```python >>> m.reset_state() >>> m.update_state([[0., 1.], [1., 1.]], [[1., 0.], [1., 1.]], ... sample_weight=[0.3, 0.7]) >>> m.result() 0.6999999 ``` -------------------------------- ### Constructing and Using ModelParallel Source: https://keras.io/api/distribution/model_parallel Example demonstrating how to construct a DeviceMesh, LayoutMap, and ModelParallel distribution, then apply it to a model. ```python devices = list_devices() # Assume there are 8 devices. # Create a mesh with 2 devices for data parallelism and 4 devices for # model parallelism. device_mesh = DeviceMesh(shape=(2, 4), axis_names=('batch', 'model'), devices=devices) # Create a layout map that shard the `Dense` layer and `Conv2D` # layer variables on the last dimension. # Based on the `device_mesh`, this means the variables # will be split across 4 devices. Any other variable that doesn't # match any key in the layout map will be fully replicated. layout_map = LayoutMap(device_mesh) layout_map['.*dense.*kernel'] = (None, 'model') layout_map['.*dense.*bias'] = ('model',) layout_map['.*conv2d.*kernel'] = (None, None, None, 'model') layout_map['.*conv2d.*bias'] = ('model',) distribution = ModelParallel( layout_map=layout_map, batch_dim_name='batch', ) # Set the global distribution, or via `with distribution.scope():` set_distribution(distribution) model = model_creation() model.compile() model.fit(data) ``` -------------------------------- ### Cropping3D Layer Example Source: https://keras.io/api/layers/reshaping_layers/cropping3d Demonstrates how to use the Cropping3D layer to crop a 5D input tensor. The example shows the input shape and the resulting output shape after cropping. ```python >>> input_shape = (2, 28, 28, 10, 3) >>> x = np.arange(np.prod(input_shape)).reshape(input_shape) >>> y = keras.layers.Cropping3D(cropping=(2, 4, 2))(x) >>> y.shape (2, 24, 20, 6, 3) ``` -------------------------------- ### Instantiate Quantizer Class Source: https://keras.io/api/quantizers Example of instantiating the base Quantizer class with a specified output data type. ```python keras.Quantizer(output_dtype="int8") ``` -------------------------------- ### Basic TensorBoard Callback Usage Source: https://keras.io/api/callbacks/tensorboard Example of how to instantiate and use the TensorBoard callback during model training. ```python tensorboard_callback = keras.callbacks.TensorBoard(log_dir="./logs") model.fit(x_train, y_train, epochs=2, callbacks=[tensorboard_callback]) # Then run the tensorboard command to view the visualizations. ``` -------------------------------- ### Directory Structure Example Source: https://keras.io/api/data_loading Illustrates the expected directory structure for image classification datasets when using `image_dataset_from_directory`. ```text training_data/ ...class_a/ ......a_image_1.jpg ......a_image_2.jpg ...class_b/ ......b_image_1.jpg ......b_image_2.jpg etc. ``` -------------------------------- ### RootMeanSquaredError Example Source: https://keras.io/api/metrics/regression_metrics Demonstrates using RootMeanSquaredError with sample weights. ```python >>> m = keras.metrics.RootMeanSquaredError() >>> m.update_state([[0, 1], [0, 0]], [[1, 1], [0, 0]]) >>> m.result() 0.5 ``` ```python >>> m.reset_state() >>> m.update_state([[0, 1], [0, 0]], [[1, 1], [0, 0]], ... sample_weight=[1, 0]) >>> m.result() 0.70710677 ``` -------------------------------- ### Keras Ops logaddexp2 Example Source: https://keras.io/api/ops/numpy Demonstrates the usage of the logaddexp2 function with sample tensors. ```python >>> from keras import ops >>> x1 = ops.array([1, 2, 3]) >>> x2 = ops.array([1, 2, 3]) >>> ops.logaddexp2(x1, x2) array([2., 3., 4.], dtype=float32) ``` -------------------------------- ### Instantiate CosineDecayRestarts Schedule Source: https://keras.io/api/optimizers/learning_rate_schedules/cosine_decay_restarts Example of how to instantiate the CosineDecayRestarts schedule. This schedule can be directly passed to a Keras optimizer. ```python first_decay_steps = 1000 lr_decayed_fn = ( keras.optimizers.schedules.CosineDecayRestarts( initial_learning_rate, first_decay_steps)) ``` -------------------------------- ### Get Keras Version Source: https://keras.io/api/utils/config_utils Retrieves the installed Keras version. ```python keras.version() ``` -------------------------------- ### Example: Training with Interruption and Restore Source: https://keras.io/api/callbacks/backup_and_restore Demonstrates using BackupAndRestore to handle training interruptions. The model is trained, an interruption is simulated, and then training is resumed from the saved state. ```python class InterruptingCallback(keras.callbacks.Callback): def on_epoch_begin(self, epoch, logs=None): if epoch == 4: raise RuntimeError('Interrupting!') callback = keras.callbacks.BackupAndRestore(backup_dir="/tmp/backup") model = keras.models.Sequential([keras.layers.Dense(10)]) model.compile(keras.optimizers.SGD(), loss='mse') model.build(input_shape=(None, 20)) try: model.fit(np.arange(100).reshape(5, 20), np.zeros(5), epochs=10, batch_size=1, callbacks=[callback, InterruptingCallback()], verbose=0) except: pass history = model.fit(np.arange(100).reshape(5, 20), np.zeros(5), epochs=10, batch_size=1, callbacks=[callback], verbose=0) # Only 6 more epochs are run, since first training got interrupted at # zero-indexed epoch 4, second training will continue from 4 to 9. len(history.history['loss']) ``` -------------------------------- ### Instantiate FlaxLayer with Default __call__ Method Source: https://keras.io/api/layers/backend_specific_layers/flax_layer Example of creating a FlaxLayer using a Flax Module with its default __call__ method. No special setup is required for the module's signature. ```python class MyFlaxModule(flax.linen.Module): @flax.linen.compact def __call__(self, inputs): x = inputs x = flax.linen.Conv(features=32, kernel_size=(3, 3))(x) x = flax.linen.relu(x) x = flax.linen.avg_pool(x, window_shape=(2, 2), strides=(2, 2)) x = x.reshape((x.shape[0], -1)) # flatten x = flax.linen.Dense(features=200)(x) x = flax.linen.relu(x) x = flax.linen.Dense(features=10)(x) x = flax.linen.softmax(x) return x flax_module = MyFlaxModule() keras_layer = FlaxLayer(flax_module) ``` -------------------------------- ### Initialize Distribution System (Multi-Host/Process) Source: https://keras.io/api/distribution/distribution_utils Prepares the distribution system for multi-host or multi-process execution on GPUs or TPUs. This should be called before any computations. Parameters can also be set via environment variables. ```python keras.distribution.initialize( job_addresses=None, num_processes=None, process_id=None ) ``` -------------------------------- ### Using StackedRNNCells with RNN Layer Source: https://keras.io/api/layers/recurrent_layers/stacked_rnn_cell Demonstrates how to use StackedRNNCells to create a stacked LSTM layer within a Keras RNN layer. This example shows the setup and application of the stacked cells. ```python batch_size = 3 sentence_length = 5 num_features = 2 new_shape = (batch_size, sentence_length, num_features) x = np.reshape(np.arange(30), new_shape) rnn_cells = [keras.layers.LSTMCell(128) for _ in range(2)] stacked_lstm = keras.layers.StackedRNNCells(rnn_cells) lstm_layer = keras.layers.RNN(stacked_lstm) result = lstm_layer(x) ``` -------------------------------- ### Create and Modify Config Source: https://keras.io/api/utils/experiment_management_utils Demonstrates how to create a `Config` object using constructor arguments and modify it using attribute-style and dict-style access. ```python config = Config("learning_rate"=0.1, "momentum"=0.9) # Then keep adding to it via attribute-style setting config.use_ema = True config.ema_overwrite_frequency = 100 # You can also add attributes via dict-like access config["seed"] = 123 # You can retrieve entries both via attribute-style # access and dict-style access assert config.seed == 100 assert config["learning_rate"] == 0.1 ``` -------------------------------- ### Update and Get Result for BinaryCrossentropy Source: https://keras.io/api/metrics/probabilistic_metrics Updates the state of the BinaryCrossentropy metric with true labels and predictions, then returns the computed metric result. This example shows basic usage without sample weights. ```python >>> m = keras.metrics.BinaryCrossentropy() >>> m.update_state([[0, 1], [0, 0]], [[0.6, 0.4], [0.4, 0.6]]) >>> m.result() 0.81492424 ``` -------------------------------- ### Instantiate and Use a Dense Layer Source: https://keras.io/api/layers Demonstrates how to instantiate a `Dense` layer with a specified number of units and activation function, and then apply it to input data. ```python import keras from keras import layers layer = layers.Dense(32, activation='relu') inputs = keras.random.uniform(shape=(10, 20)) outputs = layer(inputs) ``` -------------------------------- ### Get Reuters Word Index Source: https://keras.io/api/datasets/reuters Retrieves a dictionary mapping words to their corresponding indices in the Reuters dataset. Note that actual word indices in the data are offset by 3 due to reserved indices for padding, start, and out-of-vocabulary characters. ```python keras.datasets.reuters.get_word_index(path="reuters_word_index.json") ``` -------------------------------- ### Instantiate Input and Build a Model Source: https://keras.io/api/layers/core_layers/input Example demonstrating how to use the `Input` object to define the input shape and then build a simple Keras model using `Dense` and `Model`. ```python # This is a logistic regression in Keras x = Input(shape=(32,)) y = Dense(16, activation='softmax')(x) model = Model(x, y) ``` -------------------------------- ### Using TorchModuleWrapper with PyTorch Modules in Keras Source: https://keras.io/api/layers/backend_specific_layers/torch_module_wrapper Demonstrates how to wrap PyTorch nn.Module instances with TorchModuleWrapper to create a Keras Model. This example shows integrating convolutional, pooling, dropout, and linear layers from PyTorch into a Keras classifier. Ensure PyTorch is installed and configured as the backend. ```python import torch import torch.nn as nn import torch.nn.functional as F import keras from keras.layers import TorchModuleWrapper class Classifier(keras.Model): def __init__(self, **kwargs): super().__init__(**kwargs) # Wrap `torch.nn.Module`s with `TorchModuleWrapper` # if they contain parameters self.conv1 = TorchModuleWrapper( nn.Conv2d(in_channels=1, out_channels=32, kernel_size=(3, 3)) ) self.conv2 = TorchModuleWrapper( nn.Conv2d(in_channels=32, out_channels=64, kernel_size=(3, 3)) ) self.pool = nn.MaxPool2d(kernel_size=(2, 2)) self.flatten = nn.Flatten() self.dropout = nn.Dropout(p=0.5) self.fc = TorchModuleWrapper(nn.Linear(1600, 10)) def call(self, inputs): x = F.relu(self.conv1(inputs)) x = self.pool(x) x = F.relu(self.conv2(x)) x = self.pool(x) x = self.flatten(x) x = self.dropout(x) x = self.fc(x) return F.softmax(x, dim=1) model = Classifier() model.build((1, 28, 28)) print("# Output shape", model(torch.ones(1, 1, 28, 28).to("cuda")).shape) model.compile( loss="sparse_categorical_crossentropy", optimizer="adam", metrics=["accuracy"] ) model.fit(train_loader, epochs=5) ``` -------------------------------- ### Distillation with Multiple Losses and Custom Weights Source: https://keras.io/api/models/distillation/distiller Illustrates configuring a Distiller with multiple distillation losses and their corresponding weights. The distiller is then compiled with custom settings. ```python # Multiple distillation losses distiller = Distiller( teacher=teacher, student=student, distillation_losses=[ LogitsDistillation(temperature=3.0), FeatureDistillation( teacher_layer_name="dense_1", student_layer_name="dense_1" ) ], distillation_loss_weights=[1.0, 0.5], ) # Compile with custom settings distiller.compile( optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'] ) ``` -------------------------------- ### MeanAbsolutePercentageError Usage Example Source: https://keras.io/api/losses/regression_losses Example of using the MeanAbsolutePercentageError loss function with sample true labels and predicted values. ```python >>> y_true = keras.ops.array([100.0, 200.0, 300.0]) >>> y_pred = keras.ops.array([90.0, 210.0, 310.0]) >>> loss = keras.losses.MeanAbsolutePercentageError() >>> loss(y_true, y_pred) 6.111111 ``` -------------------------------- ### Basic Metric Usage Example Source: https://keras.io/api/metrics/base_metric Demonstrates the typical lifecycle of a metric: initialization, updating state with data, and retrieving the final result. ```python m = SomeMetric(...) for input in ...: m.update_state(input) print('Final result: ', m.result()) ``` -------------------------------- ### MeanAbsoluteError Usage Example Source: https://keras.io/api/losses/regression_losses Example of using the MeanAbsoluteError loss function with sample true labels and predicted values. ```python >>> y_true = keras.ops.array([1.0, 0.3, 1.0]) >>> y_pred = keras.ops.array([1.9, 0.3, 1.8]) >>> loss = keras.losses.MeanAbsoluteError() >>> loss(y_true, y_pred) 0.5666667 ``` -------------------------------- ### Example: Hashing Layer with FarmHash64 Source: https://keras.io/api/layers/preprocessing_layers/categorical/hashing Demonstrates the basic usage of the Hashing layer with the default FarmHash64 algorithm. It hashes string inputs into integer bins. ```python >>> layer = keras.layers.Hashing(num_bins=3) >>> inp = [['A'], ['B'], ['C'], ['D'], ['E']] >>> layer(inp) array([[1], [0], [1], [1], [2]])> ``` -------------------------------- ### MeanSquaredError Usage Example Source: https://keras.io/api/losses/regression_losses Example of using the MeanSquaredError loss function with sample true labels and predicted values. ```python >>> y_true = keras.ops.array([1.0, 0.0, 1.0]) >>> y_pred = keras.ops.array([0.9, 0.1, 0.8]) >>> loss = keras.losses.MeanSquaredError() >>> loss(y_true, y_pred) 0.02 ``` -------------------------------- ### Example Usage of LogCoshError Source: https://keras.io/api/metrics/regression_metrics Demonstrates how to use the LogCoshError metric with update_state and result methods. Includes an example with sample weights. ```python >>> m = keras.metrics.LogCoshError() >>> m.update_state([[0, 1], [0, 0]], [[1, 1], [0, 0]]) >>> m.result() 0.10844523 ``` ```python >>> m.reset_state() >>> m.update_state([[0, 1], [0, 0]], [[1, 1], [0, 0]], ... sample_weight=[1, 0]) >>> m.result() 0.21689045 ``` -------------------------------- ### Example: Hashing Layer with SipHash64 Salt Source: https://keras.io/api/layers/preprocessing_layers/categorical/hashing Demonstrates using the SipHash64 algorithm with a salt for obfuscated hashing. The salt value is provided as a list of integers. ```python >>> layer = keras.layers.Hashing(num_bins=3, salt=[133, 137]) >>> inp = [['A'], ['B'], ['C'], ['D'], ['E']] >>> layer(inp) array([[1], [2], [1], [0], [2]]) ``` -------------------------------- ### DepthwiseConv1D Layer Example Source: https://keras.io/api/layers/convolution_layers/depthwise_convolution1d Example demonstrating the usage of the DepthwiseConv1D layer with random input data and printing the output shape. ```python >>> x = np.random.rand(4, 10, 12) >>> y = keras.layers.DepthwiseConv1D(3, 3, 2, activation='relu')(x) >>> print(y.shape) (4, 4, 36) ``` -------------------------------- ### Softmax Layer Usage Example Source: https://keras.io/api/layers/activation_layers/softmax Demonstrates how to instantiate and use the Softmax layer with a sample input array. The output represents a probability distribution. ```python >>> softmax_layer = keras.layers.Softmax() >>> input = np.array([1.0, 2.0, 1.0]) >>> result = softmax_layer(input) >>> result [0.21194157, 0.5761169, 0.21194157] ``` -------------------------------- ### RandomGrayscale Layer Example Source: https://keras.io/api/layers/preprocessing_layers/image_augmentation/random_grayscale Example demonstrating the usage of the RandomGrayscale layer with sample images, labels, and segmentation masks. The layer is applied during training. ```python layer = keras.layers.RandomGrayscale(value_range=(0, 255)) images = np.random.randint(0, 255, (8, 224, 224, 3), dtype="uint8") labels = keras.ops.one_hot( np.array([0, 1, 2, 0, 1, 2, 0, 1]), num_classes=3 ) segmentation_masks = np.random.randint(0, 3, (8, 224, 224, 1), dtype="uint8") output = layer( { "images": images, "labels": labels, "segmentation_masks": segmentation_masks }, training=True ) ``` -------------------------------- ### Listing Model Variable Paths Source: https://keras.io/api/distribution/model_parallel Example demonstrating how to list all model variable paths to help define layout mapping rules. ```python model = create_model() for v in model.variables: print(v.path) ``` -------------------------------- ### LayerNormalization Beta and Gamma Shape Example Source: https://keras.io/api/layers/normalization_layers/layer_normalization An example demonstrating how to build a LayerNormalization layer and inspect the shapes of its learned beta and gamma parameters. ```python >>> layer = keras.layers.LayerNormalization(axis=[1, 2, 3]) >>> layer.build([5, 20, 30, 40]) >>> print(layer.beta.shape) (20, 30, 40) >>> print(layer.gamma.shape) (20, 30, 40) ``` -------------------------------- ### IoU Metric Calculation Example Source: https://keras.io/api/metrics/segmentation_metrics Demonstrates how to instantiate and use the IoU metric with update_state and result methods. Assumes sparse labels. ```python >>> # cm = [[1, 1], >>> # [1, 1]] >>> # sum_row = [2, 2], sum_col = [2, 2], true_positives = [1, 1] >>> # iou = true_positives / (sum_row + sum_col - true_positives)) >>> # iou = [0.33, 0.33] >>> m = keras.metrics.IoU(num_classes=2, target_class_ids=[0]) >>> m.update_state([0, 0, 1, 1], [0, 1, 0, 1]) >>> m.result() 0.33333334 ``` -------------------------------- ### Example Usage: CosineDecay With Warmup Source: https://keras.io/api/optimizers/learning_rate_schedules/cosine_decay Shows how to configure the CosineDecay schedule to include a linear warmup phase, gradually increasing the learning rate before applying the cosine decay. This is beneficial for stabilizing early training. ```python decay_steps = 1000 initial_learning_rate = 0 warmup_steps = 1000 target_learning_rate = 0.1 lr_warmup_decayed_fn = keras.optimizers.schedules.CosineDecay( initial_learning_rate, decay_steps, warmup_target=target_learning_rate, warmup_steps=warmup_steps ) ``` -------------------------------- ### Keras Ops logspace Function Source: https://keras.io/api/ops/numpy Generates numbers spaced evenly on a log scale. The sequence starts at base ** start and ends at base ** stop. ```python keras.ops.logspace(start, stop, num=50, endpoint=True, base=10, dtype=None, axis=0) ``` -------------------------------- ### Resuming Training from Weight-Only Checkpoints Source: https://keras.io/api/callbacks/model_checkpoint Demonstrates how to correctly resume training from a weight-only checkpoint. It's crucial to compile the model *before* loading the weights to restore the optimizer state, especially for learning rate schedules. ```python # Define a learning rate schedule initial_learning_rate = 0.1 lr_schedule = keras.optimizers.schedules.ExponentialDecay( initial_learning_rate, decay_steps=100000, decay_rate=0.96, staircase=True, ) # 1. Create a fresh model instance model = get_model() # 2. Compile the model *before* loading weights model.compile( optimizer=keras.optimizers.RMSprop(learning_rate=lr_schedule), loss="sparse_categorical_crossentropy", metrics=["accuracy"], ) # 3. Load weights (optimizer state is restored automatically) model.load_weights(checkpoint_filepath) # 4. Continue training model.fit(x_train, y_train, epochs=10) ``` -------------------------------- ### RandomPosterization Layer Example Source: https://keras.io/api/layers/preprocessing_layers/image_augmentation/random_posterization This example demonstrates how to use the RandomPosterization layer with sample images, labels, and segmentation masks. It shows the layer being called with a dictionary of inputs. ```python layer = keras.layers.RandomPosterization(value_range=(0, 255)) images = np.random.randint(0, 255, (8, 224, 224, 3), dtype="uint8") labels = keras.ops.one_hot( np.array([0, 1, 2, 0, 1, 2, 0, 1]), num_classes=3 ) segmentation_masks = np.random.randint(0, 3, (8, 224, 224, 1), dtype="uint8") output = layer( { "images": images, "labels": labels, "segmentation_masks": segmentation_masks }, training=True ) ``` -------------------------------- ### GlorotUniform Initializer Usage Source: https://keras.io/api/layers/initializers Demonstrates how to use the GlorotUniform initializer standalone to generate weight values. ```python keras.initializers.GlorotUniform(seed=None, input_axes=None, output_axes=None) ``` ```python >>> # Standalone usage: >>> initializer = GlorotUniform() >>> values = initializer(shape=(2, 2)) ``` ```python >>> # Usage in a Keras layer: >>> initializer = GlorotUniform() >>> layer = Dense(3, kernel_initializer=initializer) ``` -------------------------------- ### Load and Verify MNIST Dataset Source: https://keras.io/api/datasets/mnist This example demonstrates how to load the MNIST dataset and verify the shapes of the returned training and testing data arrays. Ensure the dataset is downloaded and accessible. ```python (x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data() assert x_train.shape == (60000, 28, 28) assert x_test.shape == (10000, 28, 28) assert y_train.shape == (60000,) assert y_test.shape == (10000,) ``` -------------------------------- ### LeakyReLU Layer Usage Example Source: https://keras.io/api/layers/activation_layers/leaky_relu This example demonstrates how to instantiate and use the LeakyReLU layer with a custom `negative_slope` and shows the resulting output for a sample input array. ```python leaky_relu_layer = LeakyReLU(negative_slope=0.5) input = np.array([-10, -5, 0.0, 5, 10]) result = leaky_relu_layer(input) # result = [-5. , -2.5, 0. , 5. , 10.] ``` -------------------------------- ### Cropping2D Layer Example Source: https://keras.io/api/layers/reshaping_layers/cropping2d Demonstrates how to use the Cropping2D layer with a sample input tensor. It shows the input shape and the resulting output shape after applying cropping. ```python input_shape = (2, 28, 28, 3) x = np.arange(np.prod(input_shape)).reshape(input_shape) y = keras.layers.Cropping2D(cropping=((2, 2), (4, 4)))(x) y.shape ``` -------------------------------- ### get_word_index Source: https://keras.io/api/datasets/reuters Retrieves a dictionary mapping words to their index in the Reuters dataset. Word indices start from 3, with 3 indices reserved for padding, start, and out-of-vocabulary characters. ```APIDOC ## `get_word_index` function Retrieves a dict mapping words to their index in the Reuters dataset. ### Method ``` keras.datasets.reuters.get_word_index(path="reuters_word_index.json") ``` ### Description Retrieves a dict mapping words to their index in the Reuters dataset. Actual word indices starts from 3, with 3 indices reserved for: 0 (padding), 1 (start), 2 (oov). ### Arguments * **path** : where to cache the data (relative to `~/.keras/dataset`). ### Returns The word index dictionary. Keys are word strings, values are their index. ``` -------------------------------- ### ZeroPadding2D Layer Example Source: https://keras.io/api/layers/reshaping_layers/zero_padding2d Demonstrates how to use the ZeroPadding2D layer with a padding value of 1 on a 4D input tensor. The example shows the input tensor and the resulting tensor after padding. ```python input_shape = (1, 1, 2, 2) x = np.arange(np.prod(input_shape)).reshape(input_shape) y = keras.layers.ZeroPadding2D(padding=1)(x) ``` -------------------------------- ### Prepare Image for Model Prediction Source: https://keras.io/api/data_loading/image This example demonstrates a common workflow: loading an image, converting it to a NumPy array, and then reshaping it into a batch for model prediction. ```python image = keras.utils.load_img(image_path) input_arr = keras.utils.img_to_array(image) input_arr = np.array([input_arr]) # Convert single image to a batch. predictions = model.predict(input_arr) ``` -------------------------------- ### UnitNormalization Layer Example Source: https://keras.io/api/layers/normalization_layers/unit_normalization Demonstrates how to use the UnitNormalization layer to normalize a batch of input data. The example shows that the L2 norm of the first element in the normalized batch is 1.0. ```python >>> data = np.arange(6).reshape(2, 3) >>> normalized_data = keras.layers.UnitNormalization()(data) >>> np.sum(normalized_data[0, :] ** 2) 1.0 ``` -------------------------------- ### Conv1DTranspose Layer Example Source: https://keras.io/api/layers/convolution_layers/convolution1d_transpose Demonstrates how to use the Conv1DTranspose layer to perform a 1D transposed convolution. This example shows input shape, layer configuration, and the resulting output shape. ```python >>> x = np.random.rand(4, 10, 128) >>> y = keras.layers.Conv1DTranspose(32, 3, 2, activation='relu')(x) >>> print(y.shape) (4, 21, 32) ``` -------------------------------- ### Basic Distillation with KerasHub Models Source: https://keras.io/api/models/distillation/distiller Demonstrates setting up and compiling a Distiller with a single distillation loss using KerasHub models. The trained student model can be accessed after training. ```python # Basic distillation with KerasHub models import keras_hub as hub teacher = hub.models.CausalLM.from_preset("gemma_2b_en") student = hub.models.CausalLM.from_preset( "gemma_1.1_2b_en", load_weights=False ) # Single distillation loss distiller = Distiller( teacher=teacher, student=student, distillation_losses=LogitsDistillation(temperature=3.0), ) # Compile the distiller (like any Keras model) distiller.compile( optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'] ) # Train the distiller distiller.fit(x_train, y_train, epochs=10) # Access the trained student model trained_student = distiller.student ``` -------------------------------- ### Launching TensorBoard from Command Line Source: https://keras.io/api/callbacks/tensorboard Command to launch TensorBoard to view saved log files. Ensure TensorFlow is installed. ```bash tensorboard --logdir=path_to_your_logs ``` -------------------------------- ### Usage Example with MobileNet Source: https://keras.io/api/applications/inceptionv3/inception_v3_preprocessing Example demonstrating how to use `keras.applications.mobilenet.preprocess_input` within a Keras model pipeline. This shows input layer definition, type casting, preprocessing, and model construction. ```python i = keras.layers.Input([None, None, 3], dtype="uint8") x = ops.cast(i, "float32") x = keras.applications.mobilenet.preprocess_input(x) core = keras.applications.MobileNet() x = core(x) model = keras.Model(inputs=[i], outputs=[x]) result = model(image) ``` -------------------------------- ### keras.version Source: https://keras.io/api/utils/config_utils Retrieves the installed Keras version. ```APIDOC ## `version` function ### Description Retrieves the installed Keras version. ### Method ``` keras.version() ``` ``` -------------------------------- ### Practical example of keras.device for preprocessing and training Source: https://keras.io/api/scope/context_managers Illustrates a practical use case of `keras.device` for preprocessing data on CPU and then training a model on GPU. ```python # Create dummy data and model x_raw = np.random.rand(128, 784) y_train = np.random.randint(0, 10, size=(128,)) model = keras.Sequential([ keras.Input(shape=(784,)), keras.layers.Dense(10) ]) model.compile( optimizer="adam", loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True) ) # Preprocess data on CPU with keras.device("cpu:0"): x_processed = keras.ops.cast(x_raw, "float32") # Train on GPU (if available) with keras.device("gpu:0"): model.fit(x_processed, y_train, epochs=2) ``` -------------------------------- ### GRU Layer Usage Examples Source: https://keras.io/api/layers/recurrent_layers/gru Demonstrates how to use the GRU layer for sequence processing. The first example shows a basic GRU layer returning only the final output, while the second shows returning both the full sequence output and the final state. ```python >>> inputs = np.random.random((32, 10, 8)) >>> gru = keras.layers.GRU(4) >>> output = gru(inputs) >>> output.shape (32, 4) >>> gru = keras.layers.GRU(4, return_sequences=True, return_state=True) >>> whole_sequence_output, final_state = gru(inputs) >>> whole_sequence_output.shape (32, 10, 4) >>> final_state.shape (32, 4) ``` -------------------------------- ### SimpleRNN Usage Example Source: https://keras.io/api/layers/recurrent_layers/simple_rnn Demonstrates basic and advanced usage of the SimpleRNN layer, including returning the full sequence and final state. ```python inputs = np.random.random((32, 10, 8)) simple_rnn = keras.layers.SimpleRNN(4) output = simple_rnn(inputs) # The output has shape `(32, 4)`. simple_rnn = keras.layers.SimpleRNN( 4, return_sequences=True, return_state=True ) # whole_sequence_output has shape `(32, 10, 4)`. # final_state has shape `(32, 4)`. whole_sequence_output, final_state = simple_rnn(inputs) ``` -------------------------------- ### RandomSharpness Layer Usage Example Source: https://keras.io/api/layers/preprocessing_layers/image_augmentation/random_sharpness Demonstrates how to instantiate and use the RandomSharpness layer with sample images, labels, and segmentation masks. The layer can process multiple input types simultaneously when `training=True`. ```python layer = keras.layers.RandomSharpness(value_range=(0, 255)) images = np.random.randint(0, 255, (8, 224, 224, 3), dtype="uint8") labels = keras.ops.one_hot( np.array([0, 1, 2, 0, 1, 2, 0, 1]), num_classes=3 ) segmentation_masks = np.random.randint(0, 3, (8, 224, 224, 1), dtype="uint8") output = layer( { "images": images, "labels": labels, "segmentation_masks": segmentation_masks }, training=True ) ``` -------------------------------- ### Layer.trainable Source: https://keras.io/api/layers/base_layer Property to get or set the trainability of a layer. ```APIDOC ## Layer.trainable ### Description Settable boolean property that indicates whether this layer should be trainable or not. If `False`, the layer's weights will not be updated during backpropagation. ### Property `keras.layers.Layer.trainable` (boolean) ``` -------------------------------- ### Compile and Fit a Sequential Model Source: https://keras.io/api/models/model_training_apis Demonstrates how to compile a simple Sequential model with SGD optimizer and MSE loss, then fit it to data. Records training history. ```python model = Sequential([layers.Dense(10)]) model.compile(SGD(), loss='mse') history = model.fit(np.arange(100).reshape(5, 20), np.zeros(5), epochs=10, verbose=1) print(history.params) # check the keys of history object print(history.history.keys()) ``` -------------------------------- ### CategoricalCrossentropy with sample_weight Source: https://keras.io/api/losses/probabilistic_losses Example of using CategoricalCrossentropy with sample weights. ```python >>> # Calling with 'sample_weight'. >>> cce(y_true, y_pred, sample_weight=np.array([0.3, 0.7])) 0.814 ``` -------------------------------- ### AdaptiveMaxPooling3D Usage Example Source: https://keras.io/api/layers/pooling_layers/adaptive_max_pooling3d Demonstrates how to use the AdaptiveMaxPooling3D layer with a random 3D input volume and prints the shape of the output volume. ```python import numpy as np input_vol = np.random.rand(1, 32, 32, 32, 3) layer = AdaptiveMaxPooling3D(output_size=16) output_vol = layer(input_vol) output_vol.shape ``` -------------------------------- ### MeanSquaredLogarithmicError Example Source: https://keras.io/api/metrics/regression_metrics Demonstrates using MeanSquaredLogarithmicError with and without sample weights. ```python >>> m = keras.metrics.MeanSquaredLogarithmicError() >>> m.update_state([[0, 1], [0, 0]], [[1, 1], [0, 0]]) >>> m.result() 0.12011322 ``` ```python >>> m.reset_state() >>> m.update_state([[0, 1], [0, 0]], [[1, 1], [0, 0]], ... sample_weight=[1, 0]) >>> m.result() 0.24022643 ```