# tff.learning.models.functional_model_from_keras

<br />

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) 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) |

Converts a [`tf.keras.Model`](https://www.tensorflow.org/api_docs/python/tf/keras/Model) to a [`tff.learning.models.FunctionalModel`](https://www.tensorflow.org/federated/api_docs/python/tff/learning/models/FunctionalModel).

    tff.learning.models.functional_model_from_keras(
        keras_model: Union[tf.keras.Model, Callable[[], tf.keras.Model]],
        loss_fn: tf.keras.losses.Loss,
        input_spec: Union[Sequence[Any], Mapping[str, Any]],
        metrics_constructor: Optional[Union[keras_utils.MetricConstructor, keras_utils.
            MetricsConstructor, keras_utils.MetricConstructors]] = None
    ) -> https://www.tensorflow.org/federated/api_docs/python/tff/learning/models/FunctionalModel

### Used in the notebooks

| Used in the tutorials |
|---|
| - [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) |

> [!NOTE]
> **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.

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.

> [!IMPORTANT]
> **Important:** The returned model must only be used in a graph context (for example inside a [`tff.tensorflow.computation`](https://www.tensorflow.org/federated/api_docs/python/tff/tensorflow/computation) decorated callable). It will raise an error otherwise.

<br />

<br />

<br />

| ## Args ||
|---|---|
| `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`. |
| `loss_fn` | A [`tf.keras.losses.Loss`](https://www.tensorflow.org/api_docs/python/tf/keras/Loss) object. |
| `input_spec` | A structure of [`tf.TensorSpec`](https://www.tensorflow.org/api_docs/python/tf/TensorSpec) defining the input to the model. |
| `metrics_constructor` | An optional callable that must be compatible with [`tff.learning.metrics.create_functional_metric_fns`](https://www.tensorflow.org/federated/api_docs/python/tff/learning/metrics/create_functional_metric_fns). |

<br />

<br />

<br />

<br />

| ## Returns ||
|---|---|
| A [`tff.learning.models.FunctionalModel`](https://www.tensorflow.org/federated/api_docs/python/tff/learning/models/FunctionalModel). ||

<br />

<br />

<br />

<br />

| ## Raises ||
|---|---|
| `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`](https://www.tensorflow.org/federated/api_docs/python/tff/tensorflow/computation) decorated callable or a graph context, 6) the Keras model contains a loss function with non-None sample weights. |

<br />