### Example: Creating and registering a remote function with tf.distribute.experimental.rpc.Server Source: https://www.tensorflow.org/api_docs/python/tf/distribute/experimental/rpc/Client This example demonstrates how to create a server, register a remote function, and start the server. This setup is necessary before a client can connect and invoke the remote function. ```python >>> # Have server already started. >>> import portpicker >>> @tf.function(input_signature=[ ... tf.TensorSpec([], tf.int32), ... tf.TensorSpec([], tf.int32)]) ... def remote_fn(a, b): ... return tf.add(a, b) ``` ```python port = portpicker.pick_unused_port() address = "localhost:{}".format(port) server = tf.distribute.experimental.rpc.Server.create("grpc", address) server.register("addition", remote_fn) server.start() ``` -------------------------------- ### Creating a Dataset with Start, Stop, and Step Source: https://www.tensorflow.org/api_docs/python/tf/data/experimental/RandomDataset This example shows creating a dataset with a range starting from 1, stopping at 5, with a step of 2. ```Python list(Dataset.range(1, 5, 2).as_numpy_iterator()) ``` -------------------------------- ### Example: Set and Get Memory Growth Source: https://www.tensorflow.org/api_docs/python/tf/config/experimental/get_memory_growth Demonstrates how to enable memory growth for a GPU and then verify the setting using `get_memory_growth`. This example includes error handling for invalid devices. ```Python physical_devices = tf.config.list_physical_devices('GPU') try: tf.config.experimental.set_memory_growth(physical_devices[0], True) assert tf.config.experimental.get_memory_growth(physical_devices[0]) except: # Invalid device or cannot modify virtual devices once initialized. pass ``` -------------------------------- ### Creating a Dataset with a Start and Stop Range Source: https://www.tensorflow.org/api_docs/python/tf/data/experimental/RandomDataset This example demonstrates creating a dataset with a range starting from 2 up to (but not including) 5. ```Python list(Dataset.range(2, 5).as_numpy_iterator()) ``` -------------------------------- ### Get Logical Device Configuration Example Source: https://www.tensorflow.org/api_docs/python/tf/config/get_logical_device_configuration Demonstrates how to get the logical device configuration for a physical CPU. It checks if any configuration exists, attempts to set a new configuration with two logical devices, and then verifies the new configuration. This operation may fail if virtual devices have already been initialized. ```Python physical_devices = tf.config.list_physical_devices('CPU') assert len(physical_devices) == 1, "No CPUs found" configs = tf.config.get_logical_device_configuration( physical_devices[0]) try: assert configs is None tf.config.set_logical_device_configuration( physical_devices[0], [tf.config.LogicalDeviceConfiguration(), tf.config.LogicalDeviceConfiguration()]) configs = tf.config.get_logical_device_configuration( physical_devices[0]) assert len(configs) == 2 except: # Cannot modify virtual devices once initialized. pass ``` -------------------------------- ### start Source: https://www.tensorflow.org/api_docs/python/tf/data/experimental/service/WorkerServer Starts this server. ```APIDOC ## start ### Description Starts this server. ### Method Signature ```python start() -> None ``` ### Raises * `tf.errors.OpError` - Or one of its subclasses if an error occurs while starting the server. ``` -------------------------------- ### Get GPU device details Source: https://www.tensorflow.org/api_docs/python/tf/config/experimental/get_device_details This example demonstrates how to retrieve details for a GPU device. It first lists all available GPU devices and then calls get_device_details on the first one, accessing the 'device_name' if available. ```Python gpu_devices = tf.config.list_physical_devices('GPU') if gpu_devices: details = tf.config.experimental.get_device_details(gpu_devices[0]) details.get('device_name', 'Unknown GPU') ``` -------------------------------- ### TensorFlow Installation Source: https://www.tensorflow.org/api_docs/python/tf Instructions on how to install the TensorFlow library using pip. ```APIDOC ## TensorFlow Installation ### Description Instructions on how to install the TensorFlow library using pip. ### Method ```bash pip install tensorflow ``` ### Endpoint N/A ### Parameters N/A ### Request Example N/A ### Response N/A ``` -------------------------------- ### Example: Distributing a Dataset with MirroredStrategy Source: https://www.tensorflow.org/api_docs/python/tf/distribute/MirroredStrategy This example demonstrates how to create a `MirroredStrategy`, prepare a `tf.data.Dataset`, and then distribute it using `experimental_distribute_dataset` for use with a distributed function. ```python global_batch_size = 2 # Passing the devices is optional. strategy = tf.distribute.MirroredStrategy(devices=["GPU:0", "GPU:1"]) # Create a dataset dataset = tf.data.Dataset.range(4).batch(global_batch_size) # Distribute that dataset dist_dataset = strategy.experimental_distribute_dataset(dataset) @tf.function def replica_fn(input): return input*2 result = [] ``` -------------------------------- ### Creating a Dataset with a Negative Step Source: https://www.tensorflow.org/api_docs/python/tf/data/experimental/RandomDataset This example demonstrates creating a dataset with a range starting from 5, stopping at 1, with a negative step of -2. This results in an empty list as the start is not greater than the stop with a negative step. ```Python list(Dataset.range(5, 1, -2).as_numpy_iterator()) ``` -------------------------------- ### Creating an Empty Dataset with Default Step Source: https://www.tensorflow.org/api_docs/python/tf/data/experimental/RandomDataset This example demonstrates that when creating a range with a start less than the stop and the default positive step, an empty dataset is produced. ```Python list(Dataset.range(1, 5, -2).as_numpy_iterator()) ``` -------------------------------- ### Start a DispatchServer with `start=False` Source: https://www.tensorflow.org/api_docs/python/tf/data/experimental/service/DispatchServer Creates a DispatchServer instance without starting it immediately, allowing for manual control over the server's lifecycle using the `start()` method. ```Python dispatcher = tf.data.experimental.service.DispatchServer(start=False) dispatcher.start() ``` -------------------------------- ### Install TensorFlow Source: https://www.tensorflow.org/api_docs/python/tf Use this command to install the TensorFlow library. Ensure you have Python and pip installed. ```bash pip install tensorflow ``` -------------------------------- ### SparseFeature Example Configuration Source: https://www.tensorflow.org/api_docs/python/tf/io/SparseFeature Illustrates how to configure SparseFeature to represent a 2D SparseTensor from an Example proto. This example uses two index keys for a 2D tensor. ```python SparseFeature(index_key=["ix0", "ix1"], value_key="val", dtype=tf.float32, size=[100, 3]) ``` -------------------------------- ### Enumerate Dataset with Custom Start Source: https://www.tensorflow.org/api_docs/python/tf/data/experimental/enumerate_dataset Demonstrates how to use `tf.data.experimental.enumerate_dataset` to enumerate elements starting from a specified value. ```Python a = { 1, 2, 3 } a.apply(tf.data.experimental.enumerate_dataset(start=5)) => { (5, 1), (6, 2), (7, 3) } ``` -------------------------------- ### Initialize Distribution System via Environment Variables Source: https://www.tensorflow.org/api_docs/python/tf/keras/distribution/initialize Example of initializing the distribution system by setting environment variables for job addresses, number of processes, and 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() 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 with tf.control_dependencies Source: https://www.tensorflow.org/api_docs/python/tf/debugging/assert_none_equal In this example, without `tf.control_dependencies`, errors may not be raised at all. Check `tf.control_dependencies` for more details. ```Python def check_size(x): with tf.control_dependencies([ tf.debugging.assert_none_equal(tf.size(x), 6, message='Bad tensor size')]): return x ``` -------------------------------- ### Get result type with a bfloat16 and int Source: https://www.tensorflow.org/api_docs/python/tf/keras/backend/result_type This example shows how to get the result type when one input is of type bfloat16 and the other is an integer. ```Python x = keras.ops.ones((1,), dtype="bfloat16") keras.backend.result_type(x.dtype, int) "bfloat16" ``` -------------------------------- ### Get Structure of a Dataset with Tuples Source: https://www.tensorflow.org/api_docs/python/tf/data/experimental/get_structure This example shows how to get the structure of a tf.data.Dataset where elements are tuples. It returns a tuple of TensorSpec objects. ```python dataset = tf.data.experimental.from_list([(1, 'a'), (2, 'b'), (3, 'c')]) tf.data.experimental.get_structure(dataset) (TensorSpec(shape=(), dtype=tf.int32, name=None), TensorSpec(shape=(), dtype=tf.string, name=None)) ``` -------------------------------- ### Creating a range dataset with start and stop arguments Source: https://www.tensorflow.org/api_docs/python/tf/data/experimental/CsvDataset Demonstrates creating a dataset of a range of values starting from a specified value up to (but not including) another specified value. ```Python list(Dataset.range(2, 5).as_numpy_iterator()) [2, 3, 4] ``` -------------------------------- ### Example Proto for SparseFeature Source: https://www.tensorflow.org/api_docs/python/tf/io/SparseFeature Shows the structure of an Example proto that can be parsed using a SparseFeature configuration. It includes features for values and indices. ```protobuf features { feature { key: "val" value { float_list { value: [ 0.5, -1.0 ] } } } feature { key: "ix0" value { int64_list { value: [ 3, 20 ] } } } feature { key: "ix1" value { int64_list { value: [ 1, 0 ] } } } } ``` -------------------------------- ### DispatchServer.start Source: https://www.tensorflow.org/api_docs/python/tf/data/experimental/service/DispatchServer Starts this server. ```APIDOC ## DispatchServer.start ### Description Starts this server. ### Method Signature ```python start() ``` ### Raises * `tf.errors.OpError`: Or one of its subclasses if an error occurs while starting the server. ### Example ```python dispatcher = tf.data.experimental.service.DispatchServer(start=False) dispatcher.start() ``` ``` -------------------------------- ### Get Row Starts of RaggedTensor Source: https://www.tensorflow.org/api_docs/python/tf/RaggedTensor Returns the start indices for rows in a RaggedTensor. These indices specify where the values for each row begin in self.values. Equal to rt.row_splits[:-1]. ```Python rt = tf.ragged.constant([[3, 1, 4, 1], [], [5, 9, 2], [6], []]) print(rt.values) tf.Tensor([3 1 4 1 5 9 2 6], shape=(8,), dtype=int32) print(rt.row_starts()) # indices of row starts in rt.values tf.Tensor([0 4 4 7 8], shape=(5,), dtype=int64) ``` -------------------------------- ### Get row start indices in RowPartition Source: https://www.tensorflow.org/api_docs/python/tf/experimental/RowPartition The `row_starts()` method returns the indices where the values for each row begin. These are equivalent to `row_splits()[:-1]` and the first start index is always 0. ```Python row_starts() ``` -------------------------------- ### Example of BackupAndRestore Callback Source: https://www.tensorflow.org/api_docs/python/tf/keras/callbacks/BackupAndRestore Demonstrates how to use the BackupAndRestore callback to save and restore training state. This example simulates an interruption during training and shows how the training resumes from the last 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') 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']) 6 ``` -------------------------------- ### Create a directory Source: https://www.tensorflow.org/api_docs/python/tf/io/gfile/mkdir Use `tf.io.gfile.mkdir` to create a directory. The parent directories must already exist. ```Python tf.io.gfile.mkdir( path ) ``` -------------------------------- ### Get result type with int32 and float32 Source: https://www.tensorflow.org/api_docs/python/tf/keras/backend/result_type This example demonstrates how to determine the result type when combining an int32 and a float32 dtype. ```Python x = keras.ops.ones((1,), dtype="int32") y = keras.ops.ones((1,), dtype="float32") keras.backend.result_type(x.dtype, y.dtype) "float32" ``` -------------------------------- ### Get GPU Memory Usage Source: https://www.tensorflow.org/api_docs/python/tf/config/experimental/get_memory_usage This example demonstrates how to get the memory usage of a GPU device. It first lists available GPU devices and then retrieves the memory usage for the first GPU if one exists. Note that this function does not work for CPU devices. ```python gpu_devices = tf.config.list_physical_devices('GPU') if gpu_devices: tf.config.experimental.get_memory_usage('GPU:0') ``` -------------------------------- ### Initialize Distribution System Source: https://www.tensorflow.org/api_docs/python/tf/keras/distribution/initialize Call this function to prepare the backend for execution on multi-host GPU or TPUs. It should be called before any computations. ```python tf.keras.distribution.initialize( job_addresses=None, num_processes=None, process_id=None ) ``` -------------------------------- ### Retrieve Current Distribution Strategy Source: https://www.tensorflow.org/api_docs/python/tf/keras/distribution/distribution Call this function to get the currently active distribution strategy. No setup is required if you are using the default distribution. ```Python tf.keras.distribution.distribution() ``` -------------------------------- ### Sample Usage of DatasetInitializer with StaticHashTable Source: https://www.tensorflow.org/api_docs/python/tf/data/experimental/DatasetInitializer Demonstrates how to create a dataset, initialize a DatasetInitializer, and use it to populate a StaticHashTable. The table is then used to look up values. ```Python keys = tf.data.Dataset.range(100) values = tf.data.Dataset.range(100).map( lambda x: tf.strings.as_string(x * 2)) ds = tf.data.Dataset.zip((keys, values)) init = tf.data.experimental.DatasetInitializer(ds) table = tf.lookup.StaticHashTable(init, "") table.lookup(tf.constant([0, 1, 2], dtype=tf.int64)).numpy() array([b'0', b'2', b'4'], dtype=object) ``` -------------------------------- ### Creating an Empty Dataset with a Negative Step Source: https://www.tensorflow.org/api_docs/python/tf/data/experimental/RandomDataset This example shows that when creating a range with a start less than the stop and a negative step, an empty dataset is produced. ```Python list(Dataset.range(5, 1).as_numpy_iterator()) ``` -------------------------------- ### parse_example Source: https://www.tensorflow.org/api_docs/python/tf/compat/v1 Parses `Example` protos into a dictionary of tensors. ```APIDOC ## parse_example ### Description Parses `Example` protos into a `dict` of tensors. ### Method Not specified (function call) ### Parameters Not specified ### Request Example Not specified ### Response Not specified ``` -------------------------------- ### Crop and Resize with Nearest Interpolation Source: https://www.tensorflow.org/api_docs/python/tf/image/crop_and_resize Demonstrates using `tf.image.crop_and_resize` with nearest neighbor interpolation. Similar setup to the bilinear example but with a different method specified. ```Python import numpy as np import tensorflow as tf image = np.arange(0, 18, 2).astype('float32').reshape(3, 3) result = tf.image.crop_and_resize( image[None, :, :, None], np.asarray([[0.5,0.5,1,1]]), [0], [3, 3], method='nearest') result[0][:, :, 0] ``` -------------------------------- ### Initialize Distribution System with Job Addresses Source: https://www.tensorflow.org/api_docs/python/tf/keras/distribution/initialize Example of initializing the distribution system with explicit job addresses and process information for a two-process cluster. ```python keras.distribute.initialize( job_addresses="10.0.0.1:1234,10.0.0.2:2345", num_processes=2, process_id=0) keras.distribute.initialize( job_addresses="10.0.0.1:1234,10.0.0.2:2345", num_processes=2, process_id=1) ``` -------------------------------- ### Get Reuters Word Index Source: https://www.tensorflow.org/api_docs/python/tf/keras/datasets/reuters/get_word_index Retrieves the word index dictionary for the Reuters dataset. The actual word indices start from 3, with 0, 1, and 2 reserved for padding, start, and out-of-vocabulary tokens, respectively. Adjustments are needed when translating between word indices and their dataset values. ```python tf.keras.datasets.reuters.get_word_index( path='reuters_word_index.json' ) ``` -------------------------------- ### Manual TPU Connection and Initialization Source: https://www.tensorflow.org/api_docs/python/tf/distribute/cluster_resolver/TPUClusterResolver This code demonstrates the manual steps involved in connecting to a TPU cluster and initializing the TPU system. It's shown as an alternative to the convenient `connect` method. ```Python resolver = tf.distribute.cluster_resolver.TPUClusterResolver(tpu='') tf.config.experimental_connect_to_cluster(resolver) tf.tpu.experimental.initialize_tpu_system(resolver) ``` -------------------------------- ### Launching TensorBoard Source: https://www.tensorflow.org/api_docs/python/tf/keras/callbacks/TensorBoard Launch TensorBoard from the command line to visualize logged data. Ensure TensorFlow is installed. ```bash tensorboard --logdir=path_to_your_logs ``` -------------------------------- ### ConvLSTM1D with return_state=True Source: https://www.tensorflow.org/api_docs/python/tf/keras/layers/ConvLSTM1D This example demonstrates how to get the last hidden state and cell state from the ConvLSTM1D layer by setting return_state=True. This is often used in sequence-to-sequence models. ```Python import tensorflow as tf # Example input shape: (batch_size, timesteps, features) input_data = tf.random.normal((32, 10, 1)) # Instantiate the ConvLSTM1D layer to return the last state conv_lstm_layer_state = tf.keras.layers.ConvLSTM1D( filters=64, kernel_size=3, return_sequences=False, return_state=True # Return the last state ) # Pass the input data through the layer output, state_h, state_c = conv_lstm_layer_state(input_data) # Print the shapes of the output and states print('Output shape:', output.shape) print('Last hidden state shape:', state_h.shape) print('Last cell state shape:', state_c.shape) ``` -------------------------------- ### Instantiating and Calling MyModule Source: https://www.tensorflow.org/api_docs/python/tf/Module Shows the instantiation of the `MyModule` and its subsequent call with sample input. The output demonstrates the resulting tensor shape and the namespaced variable. ```python mod = MyModule() mod(tf.ones([1, 2])) mod.w ``` -------------------------------- ### Split CPU into 2 logical devices Source: https://www.tensorflow.org/api_docs/python/tf/config/set_logical_device_configuration This example demonstrates how to split a physical CPU into two logical devices. It must be run before the runtime is initialized. ```Python physical_devices = tf.config.list_physical_devices('CPU') assert len(physical_devices) == 1, "No CPUs found" # Specify 2 virtual CPUs. Note currently memory limit is not supported. try: tf.config.set_logical_device_configuration( physical_devices[0], [tf.config.LogicalDeviceConfiguration(), tf.config.LogicalDeviceConfiguration()]) logical_devices = tf.config.list_logical_devices('CPU') assert len(logical_devices) == 2 tf.config.set_logical_device_configuration( physical_devices[0], [tf.config.LogicalDeviceConfiguration(), tf.config.LogicalDeviceConfiguration(), tf.config.LogicalDeviceConfiguration(), tf.config.LogicalDeviceConfiguration()]) except: # Cannot modify logical devices once initialized. pass ``` -------------------------------- ### Permute Layer Example Source: https://www.tensorflow.org/api_docs/python/tf/keras/layers/Permute Demonstrates how to use the Permute layer to swap the dimensions of an input tensor. The permutation pattern does not include the batch dimension, and indexing starts at 1. ```Python x = keras.Input(shape=(10, 64)) y = keras.layers.Permute((2, 1))(x) y.shape (None, 64, 10) ``` -------------------------------- ### Using tf.image.non_max_suppression_padded Source: https://www.tensorflow.org/api_docs/python/tf/image/non_max_suppression_padded Example demonstrating how to use tf.image.non_max_suppression_padded to select bounding boxes and then extract the corresponding boxes. It shows how to handle padded output and slice it to get valid indices. ```python selected_indices_padded, num_valid = tf.image.non_max_suppression_padded( boxes, scores, max_output_size, iou_threshold, score_threshold, pad_to_max_output_size=True) selected_indices = tf.slice( selected_indices_padded, tf.constant([0]), num_valid) selected_boxes = tf.gather(boxes, selected_indices) ``` -------------------------------- ### Instantiate CrossDeviceOps Source: https://www.tensorflow.org/api_docs/python/tf/distribute/CrossDeviceOps Demonstrates how to instantiate the base `tf.distribute.CrossDeviceOps` class. This is typically done when configuring a distribution strategy. ```Python tf.distribute.CrossDeviceOps() ``` -------------------------------- ### tf.distribute.experimental.rpc.Server.start Source: https://www.tensorflow.org/api_docs/python/tf/distribute/experimental/rpc/Server Starts the RPC server, enabling it to listen for and process incoming requests from clients. ```APIDOC ## `start` ### Description Starts the RPC server on provided address. Server listens for new requests from client, once it is started. ### Method `start()` ``` -------------------------------- ### tf.io.parse_single_example Source: https://www.tensorflow.org/api_docs/python/tf/io Parses a single `Example` proto. ```APIDOC ## Function: tf.io.parse_single_example ### Description Parses a single `Example` proto. ### Parameters - `serialized`: A string Tensor representing a serialized `Example` proto. - `features`: A `Features` object describing the schema of the `Example` proto. - `name`: A name for the operation (optional). ### Returns A dictionary of tensors, where keys are feature names and values are the parsed tensors. ``` -------------------------------- ### Basic Bidirectional LSTM Model Source: https://www.tensorflow.org/api_docs/python/tf/keras/layers/Bidirectional This example demonstrates how to use the Bidirectional wrapper with LSTM layers to build a sequential model for text classification. It shows a typical setup for sequence processing tasks. ```Python model = Sequential([ Input(shape=(5, 10)), Bidirectional(LSTM(10, return_sequences=True)), Bidirectional(LSTM(10)), Dense(5, activation="softmax"), ]) model.compile(loss='categorical_crossentropy', optimizer='rmsprop') ``` -------------------------------- ### Crossed Column with Numeric and Categorical Features Source: https://www.tensorflow.org/api_docs/python/tf/feature_column/crossed_column This example demonstrates how to create a crossed column involving a numeric feature that has been bucketized, and an integer categorical feature. It shows the setup for use in a linear model. ```Python # vertical_id is an integer categorical feature. vertical_id = tf.feature_column.categorical_column_with_identity('vertical_id', 10000) price = tf.feature_column.numeric_column('price') # bucketized_column converts numerical feature to a categorical one. bucketized_price = tf.feature_column.bucketized_column(price, boundaries=[...]) vertical_id_x_price = tf.feature_column.crossed_column([vertical_id, bucketized_price], 50000) columns = [vertical_id_x_price, ...] features = tf.io.parse_example(..., features=make_parse_example_spec(columns)) linear_prediction = linear_model(features, columns) ``` -------------------------------- ### Create and use a DispatchServer with WorkerServers Source: https://www.tensorflow.org/api_docs/python/tf/data/experimental/service/DispatchServer Demonstrates setting up a DispatchServer and connecting WorkerServers to it. The dataset is then distributed across the workers for processing. ```Python dispatcher = tf.data.experimental.service.DispatchServer() dispatcher_address = dispatcher.target.split("://")[1] worker = tf.data.experimental.service.WorkerServer( tf.data.experimental.service.WorkerConfig( dispatcher_address=dispatcher_address)) dataset = tf.data.Dataset.range(10) dataset = dataset.apply(tf.data.experimental.service.distribute( processing_mode="parallel_epochs", service=dispatcher.target)) print(list(dataset.as_numpy_iterator())) ``` -------------------------------- ### TF_CONFIG environment variable for cluster setup Source: https://www.tensorflow.org/api_docs/python/tf/distribute/experimental/ParameterServerStrategy This example demonstrates setting the `TF_CONFIG` environment variable for a parameter server task. This is crucial for tasks to discover each other's addresses. The `TFConfigClusterResolver` uses this variable. ```Python os.environ['TF_CONFIG'] = ''' { "cluster": { "chief": ["chief.example.com:2222"], "ps": ["ps0.example.com:2222", "ps1.example.com:2222"], "worker": ["worker0.example.com:2222", "worker1.example.com:2222", "worker2.example.com:2222"] }, "task": { "type": "ps", "index": 1 } } ''' ``` -------------------------------- ### EfficientNetV2S Source: https://www.tensorflow.org/api_docs/python/tf/keras/applications/EfficientNetV2S Instantiates the EfficientNetV2S architecture. For image classification use cases, see this page for detailed examples. For transfer learning use cases, make sure to read the guide to transfer learning & fine-tuning. ```APIDOC ## EfficientNetV2S ### Description Instantiates the EfficientNetV2S architecture. This function returns a Keras image classification model, optionally loaded with weights pre-trained on ImageNet. ### Method `tf.keras.applications.EfficientNetV2S` ### Parameters #### Args * `include_top` (Boolean) - Whether to include the fully-connected layer at the top of the network. Defaults to `True`. * `weights` (string) - One of `None` (random initialization), `"imagenet"` (pre-training on ImageNet), or the path to the weights file to be loaded. Defaults to `"imagenet"`. * `input_tensor` (Keras tensor) - Optional Keras tensor (i.e. output of `layers.Input()`) to use as image input for the model. * `input_shape` (tuple) - Optional shape tuple, only to be specified if `include_top` is `False`. It should have exactly 3 inputs channels. * `pooling` (string) - Optional pooling mode for feature extraction when `include_top` is `False`. Defaults to None. Options include `"avg"` for global average pooling and `"max"` for global max pooling. * `classes` (Integer) - Optional number of classes to classify images into, only to be specified if `include_top` is `True`, and if no `weights` argument is specified. Defaults to 1000 (number of ImageNet classes). * `classifier_activation` (string or callable) - A string or callable. The activation function to use on the "top" layer. Ignored unless `include_top=True`. Set `classifier_activation=None` to return the logits of the "top" layer. Defaults to `"softmax"`. When loading pretrained weights, `classifier_activation` can only be `None` or `"softmax"`. * `include_preprocessing` (Boolean) - Whether to include preprocessing as part of the model. Defaults to `True`. ### Returns A model instance. ### Reference * EfficientNetV2: Smaller Models and Faster Training (ICML 2021) ``` -------------------------------- ### List physical GPUs Source: https://www.tensorflow.org/api_docs/python/tf/config/list_physical_devices This example lists the number of visible GPUs on the host. The number of GPUs available to the runtime may change during runtime initialization. ```Python physical_devices = tf.config.list_physical_devices('GPU') print("Num GPUs:", len(physical_devices)) ``` -------------------------------- ### Create and use an index lookup table Source: https://www.tensorflow.org/api_docs/python/tf/data/experimental/index_table_from_dataset This example demonstrates how to create an index lookup table from a dataset of strings and then use it to look up values. Out-of-vocabulary lookups are handled by the table's configuration. ```python ds = tf.data.Dataset.range(100).map(lambda x: tf.strings.as_string(x * 2)) table = tf.data.experimental.index_table_from_dataset( ds, key_dtype=dtypes.int64) table.lookup(tf.constant(['0', '2', '4'], dtype=tf.string)).numpy() ``` -------------------------------- ### EfficientNetB7 Source: https://www.tensorflow.org/api_docs/python/tf/keras/applications/EfficientNetB7 Instantiates the EfficientNetB7 architecture. For image classification use cases, see this page for detailed examples. For transfer learning use cases, make sure to read the guide to transfer learning & fine-tuning. ```APIDOC ## EfficientNetB7 ### Description Instantiates the EfficientNetB7 architecture. This function returns a Keras image classification model, optionally loaded with weights pre-trained on ImageNet. EfficientNet models expect their inputs to be float tensors of pixels with values in the `[0-255]` range. ### Method `tf.keras.applications.EfficientNetB7` ### Parameters #### Args - **`include_top`** (bool) - Optional - Whether to include the fully-connected layer at the top of the network. Defaults to `True`. - **`weights`** (str) - Optional - One of `None` (random initialization), `"imagenet"` (pre-training on ImageNet), or the path to the weights file to be loaded. Defaults to `"imagenet"`. - **`input_tensor`** (keras.Input) - Optional - Optional Keras tensor (i.e. output of `layers.Input()`) to use as image input for the model. - **`input_shape`** (tuple) - Optional - Optional shape tuple, only to be specified if `include_top` is False. It should have exactly 3 inputs channels. - **`pooling`** (str) - Optional - Optional pooling mode for feature extraction when `include_top` is `False`. Defaults to `None`. Options include `"avg"` (global average pooling) or `"max"` (global max pooling). - **`classes`** (int) - Optional - Optional number of classes to classify images into, only to be specified if `include_top` is True, and if no `weights` argument is specified. Defaults to `1000`. - **`classifier_activation`** (str or callable) - Optional - A `str` or callable. The activation function to use on the "top" layer. Ignored unless `include_top=True`. Set `classifier_activation=None` to return the logits of the "top" layer. Defaults to `'softmax'`. When loading pretrained weights, `classifier_activation` can only be `None` or `"softmax"`. ### Returns - A Keras model instance. ``` -------------------------------- ### EfficientNetB3 Source: https://www.tensorflow.org/api_docs/python/tf/keras/applications/EfficientNetB3 Instantiates the EfficientNetB3 architecture. For image classification use cases, see this page for detailed examples. For transfer learning use cases, make sure to read the guide to transfer learning & fine-tuning. ```APIDOC ## EfficientNetB3 ### Description Instantiates the EfficientNetB3 architecture. This function returns a Keras image classification model, optionally loaded with weights pre-trained on ImageNet. EfficientNet models expect their inputs to be float tensors of pixels with values in the `[0-255]` range. ### Method `tf.keras.applications.EfficientNetB3( include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000, classifier_activation='softmax', **kwargs ) ` ### Parameters #### Args - **include_top** (bool) - Whether to include the fully-connected layer at the top of the network. Defaults to `True`. - **weights** (str or None) - One of `None` (random initialization), `"imagenet"` (pre-training on ImageNet), or the path to the weights file to be loaded. Defaults to `"imagenet"`. - **input_tensor** (tf.Tensor, optional) - Optional Keras tensor (i.e. output of `layers.Input()`) to use as image input for the model. - **input_shape** (tuple, optional) - Optional shape tuple, only to be specified if `include_top` is False. It should have exactly 3 inputs channels. - **pooling** (str, optional) - Optional pooling mode for feature extraction when `include_top` is `False`. Defaults to `None`. Options include `"avg"` (global average pooling) or `"max"` (global max pooling). - **classes** (int) - Optional number of classes to classify images into, only to be specified if `include_top` is True, and if no `weights` argument is specified. Defaults to `1000`. - **classifier_activation** (str or callable) - A `str` or callable. The activation function to use on the "top" layer. Ignored unless `include_top=True`. Set `classifier_activation=None` to return the logits of the "top" layer. Defaults to `'softmax'`. When loading pretrained weights, `classifier_activation` can only be `None` or `"softmax"`. ### Returns - A Keras model instance. ``` -------------------------------- ### parse_single_example Source: https://www.tensorflow.org/api_docs/python/tf/compat/v1 Parses a single `Example` proto. ```APIDOC ## parse_single_example ### Description Parses a single `Example` proto. ### Method Not specified (function call) ### Parameters Not specified ### Request Example Not specified ### Response Not specified ``` -------------------------------- ### EfficientNetB6 Source: https://www.tensorflow.org/api_docs/python/tf/keras/applications/EfficientNetB6 Instantiates the EfficientNetB6 architecture. For image classification use cases, see this page for detailed examples. For transfer learning use cases, make sure to read the guide to transfer learning & fine-tuning. ```APIDOC ## EfficientNetB6 ### Description Instantiates the EfficientNetB6 architecture. This function returns a Keras image classification model, optionally loaded with weights pre-trained on ImageNet. ### Method `tf.keras.applications.EfficientNetB6( include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000, classifier_activation='softmax', **kwargs ) ` ### Parameters #### Args - **include_top** (bool) - Whether to include the fully-connected layer at the top of the network. Defaults to `True`. - **weights** (str or None) - One of `None` (random initialization), `"imagenet"` (pre-training on ImageNet), or the path to the weights file to be loaded. Defaults to `"imagenet"`. - **input_tensor** (keras.Input) - Optional Keras tensor (i.e. output of `layers.Input()`) to use as image input for the model. - **input_shape** (tuple) - Optional shape tuple, only to be specified if `include_top` is False. It should have exactly 3 inputs channels. - **pooling** (str or None) - Optional pooling mode for feature extraction when `include_top` is `False`. Defaults to `None`. Options are `None`, `"avg"`, `"max"`. - **classes** (int) - Optional number of classes to classify images into, only to be specified if `include_top` is True, and if no `weights` argument is specified. Defaults to `1000`. - **classifier_activation** (str or callable) - A `str` or callable. The activation function to use on the "top" layer. Ignored unless `include_top=True`. Set `classifier_activation=None` to return the logits of the "top" layer. Defaults to `'softmax'`. ### Returns A model instance. ``` -------------------------------- ### from_config Method Source: https://www.tensorflow.org/api_docs/python/tf/keras/layers/SeparableConv2D Illustrates how to create a SeparableConv2D layer instance from its configuration dictionary. ```Python @classmethod from_config( config ) Creates a layer from its config. This method is the reverse of `get_config`, capable of instantiating the same layer from the config dictionary. It does not handle layer connectivity (handled by Network), nor weights (handled by `set_weights`). Args --- `config` : A Python dictionary, typically the output of get_config. Returns --- A layer instance. ``` -------------------------------- ### Creating a tf.compat.v1.Session with Server Target Source: https://www.tensorflow.org/api_docs/python/tf/distribute/Server Demonstrates how to create a tf.compat.v1.Session that connects to a tf.distribute.Server. This is useful for interacting with the server in a distributed training setup. ```python server = tf.distribute.Server(...) with tf.compat.v1.Session(server.target): # ... ``` -------------------------------- ### ResNet50V2 Constructor Source: https://www.tensorflow.org/api_docs/python/tf/keras/applications/ResNet50V2 Instantiates the ResNet50V2 architecture. For image classification use cases, see this page for detailed examples. For transfer learning use cases, make sure to read the guide to transfer learning & fine-tuning. ```APIDOC ## tf.keras.applications.ResNet50V2 ### Description Instantiates the ResNet50V2 architecture. ### Method Signature ```python tf.keras.applications.ResNet50V2( include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000, classifier_activation='softmax' ) ``` ### Args * `include_top` (bool): whether to include the fully-connected layer at the top of the network. * `weights` (str or None): one of `None` (random initialization), `"imagenet"` (pre-training on ImageNet), or the path to the weights file to be loaded. * `input_tensor` (Keras tensor): optional Keras tensor (i.e. output of `layers.Input()`) to use as image input for the model. * `input_shape` (tuple): optional shape tuple, only to be specified if `include_top` is `False` (otherwise the input shape has to be `(224, 224, 3)` (with `"channels_last"` data format) or `(3, 224, 224)` (with `"channels_first"` data format)). It should have exactly 3 inputs channels, and width and height should be no smaller than 32. E.g. `(200, 200, 3)` would be one valid value. * `pooling` (str or None): Optional pooling mode for feature extraction when `include_top` is `False`. * `None` means that the output of the model will be the 4D tensor output of the last convolutional block. * `avg` means that global average pooling will be applied to the output of the last convolutional block, and thus the output of the model will be a 2D tensor. * `max` means that global max pooling will be applied. * `classes` (int): optional number of classes to classify images into, only to be specified if `include_top` is `True`, and if no `weights` argument is specified. * `classifier_activation` (str or callable): A `str` or callable. The activation function to use on the "top" layer. Ignored unless `include_top=True`. Set `classifier_activation=None` to return the logits of the "top" layer. When loading pretrained weights, `classifier_activation` can only be `None` or `"softmax"`. ### Returns A Model instance. ### Note Each Keras Application expects a specific kind of input preprocessing. For ResNet, call `keras.applications.resnet_v2.preprocess_input` on your inputs before passing them to the model. `resnet_v2.preprocess_input` will scale input pixels between -1 and 1. ``` -------------------------------- ### Create a sequence categorical column with vocabulary list Source: https://www.tensorflow.org/api_docs/python/tf/feature_column/sequence_categorical_column_with_vocabulary_list This example demonstrates how to create a sequence categorical column with a vocabulary list and then use it with an embedding column. It shows the basic setup for processing sequence categorical features. ```Python tf.feature_column.sequence_categorical_column_with_vocabulary_list( key='colors', vocabulary_list=('R', 'G', 'B', 'Y'), num_oov_buckets=2) colors_embedding = embedding_column(colors, dimension=3) columns = [colors_embedding] features = tf.io.parse_example(..., features=make_parse_example_spec(columns)) sequence_feature_layer = SequenceFeatures(columns) sequence_input, sequence_length = sequence_feature_layer(features) sequence_length_mask = tf.sequence_mask(sequence_length) rnn_cell = tf.keras.layers.SimpleRNNCell(hidden_size) rnn_layer = tf.keras.layers.RNN(rnn_cell) outputs, state = rnn_layer(sequence_input, mask=sequence_length_mask) ``` -------------------------------- ### Set TF_CONFIG Environment Variable Source: https://www.tensorflow.org/api_docs/python/tf/distribute/cluster_resolver/TFConfigClusterResolver Example of manually setting the TF_CONFIG environment variable for a distributed training setup. This configuration defines a cluster with a single 'worker' task type and specifies the task's type and index. ```Python os.environ['TF_CONFIG'] = json.dumps({ 'cluster': { 'worker': ["localhost:12345", "localhost:23456"] }, 'task': {'type': 'worker', 'index': 0} }) ``` -------------------------------- ### Resampling Dataset Elements to a Target Distribution (Initial Setup) Source: https://www.tensorflow.org/api_docs/python/tf/data/experimental/CsvDataset Sets up a dataset with an initial distribution of elements for rejection resampling. The initial distribution is estimated from the data. ```Python initial_dist = [0.6, 0.4] n = 1000 elems = np.random.choice(len(initial_dist), size=n, p=initial_dist) dataset = tf.data.Dataset.from_tensor_slices(elems) zero, one = np.bincount(list(dataset.as_numpy_iterator())) / n ``` -------------------------------- ### Get row split indices in RowPartition Source: https://www.tensorflow.org/api_docs/python/tf/experimental/RowPartition The `row_splits()` method returns a 1-D integer Tensor indicating the start and end indices for each row's values. Row `i`'s values are in the slice `values[row_splits[i]:row_splits[i+1]]`. ```Python row_splits() ``` -------------------------------- ### List logical devices Source: https://www.tensorflow.org/api_docs/python/tf/config/list_logical_devices Demonstrates how to list all logical devices or filter them by type. This example shows how to allocate operations to specific logical GPU devices. ```Python logical_devices = tf.config.list_logical_devices('GPU') if len(logical_devices) > 0: # Allocate on GPU:0 with tf.device(logical_devices[0].name): one = tf.constant(1) # Allocate on GPU:1 with tf.device(logical_devices[1].name): two = tf.constant(2) ``` -------------------------------- ### Using SqlDataset with Different Database Drivers Source: https://www.tensorflow.org/api_docs/python/tf/data/experimental/SqlDataset Illustrates how to connect to different SQL databases by changing the driver_name and driver_args. This example shows connection to a PostgreSQL database. ```Python dataset = tf.data.experimental.SqlDataset( driver_name="postgresql", driver_args=("dbname=mydb user=myuser password=mypass host=localhost",), query="SELECT * FROM users") for row in dataset: print(row) ``` -------------------------------- ### Using SwapEMAWeights with ModelCheckpoint for Saving EMA Weights Source: https://www.tensorflow.org/api_docs/python/tf/keras/callbacks/SwapEMAWeights This example shows how to use SwapEMAWeights with swap_on_epoch=True in conjunction with ModelCheckpoint. This setup allows saving model checkpoints that contain the EMA weights, by placing ModelCheckpoint after SwapEMAWeights in the callbacks list. ```Python # If you want to save model checkpoint with EMA weights, you can set # `swap_on_epoch=True` and place ModelCheckpoint after SwapEMAWeights. model.fit( X_train, Y_train, callbacks=[SwapEMAWeights(swap_on_epoch=True), ModelCheckpoint(...)] ) ``` -------------------------------- ### Basic Usage of make_batched_features_dataset Source: https://www.tensorflow.org/api_docs/python/tf/data/experimental/make_batched_features_dataset Demonstrates how to create a dataset of feature dictionaries from serialized Example protos. This snippet shows the structure of input data and the expected output format after parsing. ```python serialized_examples = [ features { feature { key: "age" value { int64_list { value: [ 0 ] } } } feature { key: "gender" value { bytes_list { value: [ "f" ] } } } feature { key: "kws" value { bytes_list { value: [ "code", "art" ] } } } }, features { feature { key: "age" value { int64_list { value: [] } } } feature { key: "gender" value { bytes_list { value: [ "f" ] } } } feature { key: "kws" value { bytes_list { value: [ "sports" ] } } } } ] features: { "age": FixedLenFeature([], dtype=tf.int64, default_value=-1), "gender": FixedLenFeature([], dtype=tf.string), "kws": VarLenFeature(dtype=tf.string), } { "age": [[0], [-1]], "gender": [["f"], ["f"]], "kws": SparseTensor( indices=[[0, 0], [0, 1], [1, 0]], values=["code", "art", "sports"] dense_shape=[2, 2]), } ``` -------------------------------- ### VGG16 Constructor Source: https://www.tensorflow.org/api_docs/python/tf/keras/applications/VGG16 Instantiates the VGG16 model. For image classification use cases, see this page for detailed examples. For transfer learning use cases, make sure to read the guide to transfer learning & fine-tuning. The default input size for this model is 224x224. ```APIDOC ## VGG16 ### Description Instantiates the VGG16 model. ### Method Signature ```python tf.keras.applications.VGG16( include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000, classifier_activation='softmax' ) ``` ### Args * `include_top` (bool): whether to include the 3 fully-connected layers at the top of the network. * `weights` (str): one of `None` (random initialization), `"imagenet"` (pre-training on ImageNet), or the path to the weights file to be loaded. * `input_tensor` (tf.Tensor): optional Keras tensor (i.e. output of `layers.Input()`) to use as image input for the model. * `input_shape` (tuple): optional shape tuple, only to be specified if `include_top` is `False` (otherwise the input shape has to be `(224, 224, 3)` (with `channels_last` data format) or `(3, 224, 224)` (with `"channels_first"` data format)). It should have exactly 3 input channels, and width and height should be no smaller than 32. E.g. `(200, 200, 3)` would be one valid value. * `pooling` (str): Optional pooling mode for feature extraction when `include_top` is `False`. * `None` means that the output of the model will be the 4D tensor output of the last convolutional block. * `avg` means that global average pooling will be applied to the output of the last convolutional block, and thus the output of the model will be a 2D tensor. * `max` means that global max pooling will be applied. * `classes` (int): optional number of classes to classify images into, only to be specified if `include_top` is `True`, and if no `weights` argument is specified. * `classifier_activation` (str or callable): A `str` or callable. The activation function to use on the "top" layer. Ignored unless `include_top=True`. Set `classifier_activation=None` to return the logits of the "top" layer. When loading pretrained weights, `classifier_activation` can only be `None` or `"softmax"`. ### Returns A model instance. ```