tf.keras.StatelessScope
Stay organized with collections Save and categorize content based on your preferences.
Scope to prevent any update to Keras Variables.
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 KerasVariable
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
add_loss( loss )
add_update
View source
add_update( update )
get_current_value
View source
get_current_value( variable )
__enter__
View source
__enter__()
__exit__
View source
__exit__( *args, **kwargs )
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates. Some content is licensed under the numpy license.
Last updated 2024-06-07 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-06-07 UTC."],[],[],null,["# tf.keras.StatelessScope\n\n\u003cbr /\u003e\n\n|-----------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/keras-team/keras/tree/v3.3.3/keras/src/backend/common/stateless_scope.py#L5-L97) |\n\nScope to prevent any update to Keras Variables.\n\n#### View aliases\n\n\n**Compat aliases for migration**\n\nSee\n[Migration guide](https://www.tensorflow.org/guide/migrate) for\nmore details.\n\n[`tf.compat.v1.keras.StatelessScope`](https://www.tensorflow.org/api_docs/python/tf/keras/StatelessScope)\n\n\u003cbr /\u003e\n\n tf.keras.StatelessScope(\n state_mapping=None, collect_losses=False, initialize_variables=True\n )\n\nThe values of variables to be used inside the scope\nshould be passed via the `state_mapping` argument, a\nlist of tuples `(k, v)` where `k` is a `KerasVariable`\nand `v` is the intended value for this variable\n(a backend tensor).\n\nUpdated values can be collected on scope exit via\n`value = scope.get_current_value(variable)`. No updates\nwill be applied in-place to any variables for the duration\nof the scope.\n\n#### Example:\n\n state_mapping = [(k, ops.ones(k.shape, k.dtype)) for k in model.weights]\n with keras.StatelessScope(state_mapping) as scope:\n outputs = model.some_function(inputs)\n\n # All model variables remain unchanged. Their new values can be\n # collected via:\n for k in model.weights:\n new_value = scope.get_current_value(k)\n print(f\"New value for {k}: {new_value})\n\nMethods\n-------\n\n### `add_loss`\n\n[View source](https://github.com/keras-team/keras/tree/v3.3.3/keras/src/backend/common/stateless_scope.py#L75-L76) \n\n add_loss(\n loss\n )\n\n### `add_update`\n\n[View source](https://github.com/keras-team/keras/tree/v3.3.3/keras/src/backend/common/stateless_scope.py#L78-L80) \n\n add_update(\n update\n )\n\n### `get_current_value`\n\n[View source](https://github.com/keras-team/keras/tree/v3.3.3/keras/src/backend/common/stateless_scope.py#L82-L83) \n\n get_current_value(\n variable\n )\n\n### `__enter__`\n\n[View source](https://github.com/keras-team/keras/tree/v3.3.3/keras/src/backend/common/stateless_scope.py#L70-L73) \n\n __enter__()\n\n### `__exit__`\n\n[View source](https://github.com/keras-team/keras/tree/v3.3.3/keras/src/backend/common/stateless_scope.py#L85-L97) \n\n __exit__(\n *args, **kwargs\n )"]]