tf.keras.constraints.Constraint
Stay organized with collections
Save and categorize content based on your preferences.
Base class for weight constraints.
A Constraint
instance works like a stateless function.
Users who subclass this
class should override the __call__()
method, which takes a single
weight parameter and return a projected version of that parameter
(e.g. normalized or clipped). Constraints can be used with various Keras
layers via the kernel_constraint
or bias_constraint
arguments.
Here's a simple example of a non-negative weight constraint:
class NonNegative(keras.constraints.Constraint):
def __call__(self, w):
return w * ops.cast(ops.greater_equal(w, 0.), dtype=w.dtype)
weight = ops.convert_to_tensor((-1.0, 1.0))
NonNegative()(weight)
[0., 1.]
Usage in a layer:
keras.layers.Dense(4, kernel_constraint=NonNegative())
Methods
from_config
View source
@classmethod
from_config(
config
)
Instantiates a weight constraint from a configuration dictionary.
Example:
constraint = UnitNorm()
config = constraint.get_config()
constraint = UnitNorm.from_config(config)
Args |
config
|
A Python dictionary, the output of get_config() .
|
get_config
View source
get_config()
Returns a Python dict of the object config.
A constraint config is a Python dictionary (JSON-serializable) that can
be used to reinstantiate the same object.
Returns |
Python dict containing the configuration of the constraint object.
|
__call__
View source
__call__(
w
)
Applies the constraint to the input weight variable.
By default, the inputs weight variable is not modified.
Users should override this method to implement their own projection
function.
Args |
w
|
Input weight variable.
|
Returns |
Projected variable (by default, returns unmodified inputs).
|
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.constraints.Constraint\n\n\u003cbr /\u003e\n\n|----------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/keras-team/keras/tree/v3.3.3/keras/src/constraints/constraints.py#L6-L77) |\n\nBase class for weight constraints.\n\nA `Constraint` instance works like a stateless function.\nUsers who subclass this\nclass should override the `__call__()` method, which takes a single\nweight parameter and return a projected version of that parameter\n(e.g. normalized or clipped). Constraints can be used with various Keras\nlayers via the `kernel_constraint` or `bias_constraint` arguments.\n\nHere's a simple example of a non-negative weight constraint: \n\n class NonNegative(keras.constraints.Constraint):\n\n def __call__(self, w):\n return w * ops.cast(ops.greater_equal(w, 0.), dtype=w.dtype)\n\n weight = ops.convert_to_tensor((-1.0, 1.0))\n NonNegative()(weight)\n [0., 1.]\n\n#### Usage in a layer:\n\n keras.layers.Dense(4, kernel_constraint=NonNegative())\n\nMethods\n-------\n\n### `from_config`\n\n[View source](https://github.com/keras-team/keras/tree/v3.3.3/keras/src/constraints/constraints.py#L59-L77) \n\n @classmethod\n from_config(\n config\n )\n\nInstantiates a weight constraint from a configuration dictionary.\n\n#### Example:\n\n constraint = UnitNorm()\n config = constraint.get_config()\n constraint = UnitNorm.from_config(config)\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ||\n|----------|----------------------------------------------------|\n| `config` | A Python dictionary, the output of `get_config()`. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ||\n|---|---|\n| A [`keras.constraints.Constraint`](../../../tf/keras/constraints/Constraint) instance. ||\n\n\u003cbr /\u003e\n\n### `get_config`\n\n[View source](https://github.com/keras-team/keras/tree/v3.3.3/keras/src/constraints/constraints.py#L48-L57) \n\n get_config()\n\nReturns a Python dict of the object config.\n\nA constraint config is a Python dictionary (JSON-serializable) that can\nbe used to reinstantiate the same object.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ||\n|---|---|\n| Python dict containing the configuration of the constraint object. ||\n\n\u003cbr /\u003e\n\n### `__call__`\n\n[View source](https://github.com/keras-team/keras/tree/v3.3.3/keras/src/constraints/constraints.py#L33-L46) \n\n __call__(\n w\n )\n\nApplies the constraint to the input weight variable.\n\nBy default, the inputs weight variable is not modified.\nUsers should override this method to implement their own projection\nfunction.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ||\n|-----|------------------------|\n| `w` | Input weight variable. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ||\n|---|---|\n| Projected variable (by default, returns unmodified inputs). ||\n\n\u003cbr /\u003e"]]