<br />

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/keras-team/keras/tree/v3.15.1/keras/src/backend/common/stateless_scope.py#L5-L97) |

Scope to prevent any update to Keras Variables.

#### View aliases


**Compat aliases for migration**

See
[Migration guide](https://www.tensorflow.org/guide/migrate) for
more details.

[`tf.compat.v1.keras.StatelessScope`](https://www.tensorflow.org/api_docs/python/tf/keras/StatelessScope)

<br />

    tf.keras.StatelessScope(
        state_mapping=None, collect_losses=False, initialize_variables=True
    )

The values of variables to be used inside the scope
should be passed via the `state_mapping` argument, a
list of tuples `(k, v)` where `k` is a `Variable`
and `v` is the intended value for this variable
(a backend tensor).

Updated values can be collected on scope exit via
`value = scope.get_current_value(variable)`. No updates
will be applied in-place to any variables for the duration
of the scope.

#### Example:

    state_mapping = [(k, ops.ones(k.shape, k.dtype)) for k in model.weights]
    with keras.StatelessScope(state_mapping) as scope:
        outputs = model.some_function(inputs)

    # All model variables remain unchanged. Their new values can be
    # collected via:
    for k in model.weights:
        new_value = scope.get_current_value(k)
        print(f"New value for {k}: {new_value})

## Methods

### `add_loss`

[View source](https://github.com/keras-team/keras/tree/v3.15.1/keras/src/backend/common/stateless_scope.py#L75-L76)

    add_loss(
        loss
    )

### `add_update`

[View source](https://github.com/keras-team/keras/tree/v3.15.1/keras/src/backend/common/stateless_scope.py#L78-L80)

    add_update(
        update
    )

### `get_current_value`

[View source](https://github.com/keras-team/keras/tree/v3.15.1/keras/src/backend/common/stateless_scope.py#L82-L83)

    get_current_value(
        variable
    )

### `__enter__`

[View source](https://github.com/keras-team/keras/tree/v3.15.1/keras/src/backend/common/stateless_scope.py#L70-L73)

    __enter__()

### `__exit__`

[View source](https://github.com/keras-team/keras/tree/v3.15.1/keras/src/backend/common/stateless_scope.py#L85-L97)

    __exit__(
        *args, **kwargs
    )