This method doesn't support loss functions scaled by sample weights at the
current state. Keras models with non-None sample weights will fail because
sample weights aren't supported in model serialization and deserialization.
Args
keras_model
A tf.keras.Model object, should be uncompiled. If compiled,
the metrics, optimizer, and loss function will be ignored. Note: models
that have multiple outputs will send all outputs to the loss_fn.
If the following conditions: 1) the Keras model
contains a batch normalization layer, 2) the Keras model is with
non-trainable variable, 3) error occurs when converting the Keras model, 4)
the Keras model shares variable across layers, 5) the FunctionalModel is
used outside of a tff.tensorflow.computation decorated callable or a graph
context, 6) the Keras model contains a loss function with non-None sample
weights.
[null,null,["Last updated 2024-09-20 UTC."],[],[],null,["# tff.learning.models.functional_model_from_keras\n\n\u003cbr /\u003e\n\n|-------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/federated/blob/v0.87.0 Version 2.0, January 2004 Licensed under the Apache License, Version 2.0 (the) |\n\nConverts a [`tf.keras.Model`](https://www.tensorflow.org/api_docs/python/tf/keras/Model) to a [`tff.learning.models.FunctionalModel`](../../../tff/learning/models/FunctionalModel). \n\n tff.learning.models.functional_model_from_keras(\n keras_model: Union[tf.keras.Model, Callable[[], tf.keras.Model]],\n loss_fn: tf.keras.losses.Loss,\n input_spec: Union[Sequence[Any], Mapping[str, Any]],\n metrics_constructor: Optional[Union[keras_utils.MetricConstructor, keras_utils.\n MetricsConstructor, keras_utils.MetricConstructors]] = None\n ) -\u003e ../../../tff/learning/models/FunctionalModel\n\n### Used in the notebooks\n\n| Used in the tutorials |\n|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| - [Building Your Own Federated Learning Algorithm](https://www.tensorflow.org/federated/tutorials/building_your_own_federated_learning_algorithm) - [Composing Learning Algorithms](https://www.tensorflow.org/federated/tutorials/composing_learning_algorithms) - [High-performance simulations with TFF](https://www.tensorflow.org/federated/tutorials/simulations) - [Working with tff's ClientData.](https://www.tensorflow.org/federated/tutorials/working_with_client_data) |\n\n| **Note:** This method only supports models where calling that model with `training=True` and `training=False` produce the same graph. Keras layers such as batch normalization will fail because they require updating internal state when `training=True` which is not supported.\n\nThis method doesn't support loss functions scaled by sample weights at the\ncurrent state. Keras models with non-None sample weights will fail because\nsample weights aren't supported in model serialization and deserialization.\n| **Important:** The returned model must only be used in a graph context (for example inside a [`tff.tensorflow.computation`](../../../tff/tensorflow/computation) decorated callable). It will raise an error otherwise.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|-----------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `keras_model` | A [`tf.keras.Model`](https://www.tensorflow.org/api_docs/python/tf/keras/Model) object, should be uncompiled. If compiled, the metrics, optimizer, and loss function will be ignored. Note: models that have multiple outputs will send all outputs to the `loss_fn`. |\n| `loss_fn` | A [`tf.keras.losses.Loss`](https://www.tensorflow.org/api_docs/python/tf/keras/Loss) object. |\n| `input_spec` | A structure of [`tf.TensorSpec`](https://www.tensorflow.org/api_docs/python/tf/TensorSpec) defining the input to the model. |\n| `metrics_constructor` | An optional callable that must be compatible with [`tff.learning.metrics.create_functional_metric_fns`](../../../tff/learning/metrics/create_functional_metric_fns). |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| A [`tff.learning.models.FunctionalModel`](../../../tff/learning/models/FunctionalModel). ||\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|-----------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `KerasFunctionalModelError` | If the following conditions: 1) the Keras model contains a batch normalization layer, 2) the Keras model is with non-trainable variable, 3) error occurs when converting the Keras model, 4) the Keras model shares variable across layers, 5) the FunctionalModel is used outside of a [`tff.tensorflow.computation`](../../../tff/tensorflow/computation) decorated callable or a graph context, 6) the Keras model contains a loss function with non-None sample weights. |\n\n\u003cbr /\u003e"]]